环境
  虚拟机:VMware 10
  Linux版本:CentOS-6.5-x86_64
  客户端:Xshell4
  FTP:Xftp4
  jdk8
  elasticsearch-2.2.0

一、Rest简介
Representational State Transfer
一种软件架构风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

REST的操作分为以下几种
  GET:获取对象的当前状态;
  PUT:改变对象的状态;
  POST:创建对象;
  DELETE:删除对象;
  HEAD:获取头信息。

ES内置的REST接口

二、curl-rest命令
curl属于linux命令,可以在命令行下访问url的一个工具,利用URL语法在命令行方式下工作的开源文件传输工具,使用curl可以简单实现常见的get/post请求
curl
  -X 指定http请求的方法
HEAD GET POST PUT DELETE
  -d 指定要传输的数据

1、创建索引库:wjy

[cluster@PCS101 bin]$ curl -XPUT http://196.168.123.101:9200/wjy/
{"acknowledged":true}
[cluster@PCS101 bin]$

2、创建类型(表):employee,插入一行记录,即文档document

#POST不指定ID 会自动生成ID;指定ID,会根据ID,如果存在该ID会更新,不存在就创建

[cluster@PCS101 bin]$ curl -XPOST http://196.168.123.101:9200/wjy/employee -d '
> {
> "first_name" : "bin",
> "age" : ,
> "about" : "I love to go rock climbing",
> "interests": [ "sports", "music" ]
> }'
{"_index":"wjy","_type":"employee","_id":"AWlv8cAl4hD4em0kzO4U","_version":,"_shards":{"total":,"successful":,"failed":},"created":true}
索引库 类型 编号 版本 分片 是否创建成功

#PUT方式 后面必须指定ID编码,如果重复添加相同编码数据 会失败

[cluster@PCS101 bin]$ curl -XPUT http://196.168.123.101:9200/wjy/employee/ -d '
> {
> "first_name" : "god bin",
> "last_name" : "pang",
> "age" : ,
> "about" : "I love to go rock climbing",
> "interests": [ "sports", "music" ]
> }'
{"_index":"wjy","_type":"employee","_id":"","_version":,"_shards":{"total":,"successful":,"failed":},"created":true}

3、增加field:sex

[cluster@PCS101 bin]$ curl -XPOST http://196.168.123.101:9200/wjy/employee -d '
> {
> "first_name" : "pablo2",
> "age" : ,
> "about" : "I love to go rock climbing",
> "interests": [ "sports", "music" ],
> "sex": "man"
> }'
{"_index":"wjy","_type":"employee","_id":"AWlv-tng4hD4em0kzO4W","_version":,"_shards":{"total":,"successful":,"failed":},"created":true}

4、获取数据 GET
(1)根据document的id来获取数据:pretty参数就是用json格式化的方式展示

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/1?pretty
{
"_index" : "wjy",
"_type" : "employee",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"first_name" : "god bin",
"last_name" : "pang",
"age" : ,
"about" : "I love to go rock climbing",
"interests" : [ "sports", "music" ]
}
}

对比:

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/1
{"_index":"wjy","_type":"employee","_id":"","_version":,"found":true,"_source":
{
"first_name" : "god bin",
"last_name" : "pang",
"age" : ,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}}

(2)根据field来查询数据:_search根据索引查找

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?q=first_name="bin"
{"took":,"timed_out":false,"_shards":{"total":,"successful":,"failed":},"hits":{"total":,"max_score":0.014065012,"hits":[{"_index":"wjy","_type":"employee","_id":"AWlv8cAl4hD4em0kzO4U","_score":0.014065012,"_source":
{
"first_name" : "bin",
"age" : ,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}},{"_index":"wjy","_type":"employee","_id":"AWlv91mF4hD4em0kzO4V","_score":0.01125201,"_source":
{
"first_name" : "gob bin",
"age" : ,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}},{"_index":"wjy","_type":"employee","_id":"","_score":0.01125201,"_source":
{
"first_name" : "god bin",
"last_name" : "pang",
"age" : ,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}}]}}

(3)根据field来查询数据,参数封装起来传进去:match 匹配

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
> {
> "query":
> {"match":
> {"first_name":"bin"}
> }
> }'
{
"took" : ,
"timed_out" : false,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"hits" : {
"total" : ,
"max_score" : 0.30685282,
"hits" : [ {
"_index" : "wjy",
"_type" : "employee",
"_id" : "AWlv8cAl4hD4em0kzO4U",
"_score" : 0.30685282,
"_source" : {
"first_name" : "bin",
"age" : ,
"about" : "I love to go rock climbing",
"interests" : [ "sports", "music" ]
}
}, {
"_index" : "wjy",
"_type" : "employee",
"_id" : "AWlv91mF4hD4em0kzO4V",
"_score" : 0.19178301,
"_source" : {
"first_name" : "gob bin",
"age" : ,
"about" : "I love to go rock climbing",
"interests" : [ "sports", "music" ]
}
}, {
"_index" : "wjy",
"_type" : "employee",
"_id" : "",
"_score" : 0.19178301,
"_source" : {
"first_name" : "god bin",
"last_name" : "pang",
"age" : ,
"about" : "I love to go rock climbing",
"interests" : [ "sports", "music" ]
}
} ]
}
}

#对多个field发起查询:multi_match

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
> {
> "query":
> {"multi_match":
> {
> "query":"bin",
> "fields":["last_name","first_name"],
> "operator":"and"
> }
> }
> }'
{
"took" : ,
"timed_out" : false,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"hits" : {
"total" : ,
"max_score" : 0.09415865,
"hits" : [ {
"_index" : "wjy",
"_type" : "employee",
"_id" : "AWlv8cAl4hD4em0kzO4U",
"_score" : 0.09415865,
"_source" : {
"first_name" : "bin",
"age" : ,
"about" : "I love to go rock climbing",
"interests" : [ "sports", "music" ]
}
}, {
"_index" : "wjy",
"_type" : "employee",
"_id" : "AWlv91mF4hD4em0kzO4V",
"_score" : 0.058849156,
"_source" : {
"first_name" : "gob bin",
"age" : ,
"about" : "I love to go rock climbing",
"interests" : [ "sports", "music" ]
}
}, {
"_index" : "wjy",
"_type" : "employee",
"_id" : "",
"_score" : 0.058849156,
"_source" : {
"first_name" : "god bin",
"last_name" : "pang",
"age" : ,
"about" : "I love to go rock climbing",
"interests" : [ "sports", "music" ]
}
} ]
}
}

(4)多个term对多个field发起查询:bool(boolean),组合查询,must,must_not,should
#must + must : 交集

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
> {
> "query":
> {"bool" :
> {
> "must" :
> {"match":
> {"first_name":"bin"}
> },
> "must" :
> {"match":
> {"age":}
> }
> }
> }
> }'
{
"took" : ,
"timed_out" : false,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"hits" : {
"total" : ,
"max_score" : 0.4339554,
"hits" : [ {
"_index" : "wjy",
"_type" : "employee",
"_id" : "AWlv8cAl4hD4em0kzO4U",
"_score" : 0.4339554,
"_source" : {
"first_name" : "bin",
"age" : ,
"about" : "I love to go rock climbing",
"interests" : [ "sports", "music" ]
}
} ]
}
}

#must +must_not :差集

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
> {
> "query":
> {"bool" :
> {
> "must" :
> {"match":
> {"first_name":"bin"}
> },
> "must_not" :
> {"match":
> {"age":}
> }
> }
> }
> }'
{
"took" : ,
"timed_out" : false,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"hits" : {
"total" : ,
"max_score" : 0.19178301,
"hits" : [ {
"_index" : "wjy",
"_type" : "employee",
"_id" : "AWlv91mF4hD4em0kzO4V",
"_score" : 0.19178301,
"_source" : {
"first_name" : "gob bin",
"age" : ,
"about" : "I love to go rock climbing",
"interests" : [ "sports", "music" ]
}
}, {
"_index" : "wjy",
"_type" : "employee",
"_id" : "",
"_score" : 0.19178301,
"_source" : {
"first_name" : "god bin",
"last_name" : "pang",
"age" : ,
"about" : "I love to go rock climbing",
"interests" : [ "sports", "music" ]
}
} ]
}
}

#must+must_not 查询first_name=bin的,或者年龄在20岁到33岁之间的

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
> {
> "query":
> {"bool" :
> {
> "must" :
> {"term" :
> { "first_name" : "bin" }
> }
> ,
> "must_not" :
> {"range":
> {"age" : { "from" : , "to" : }
> }
> }
> }
> }
> }'
{
"took" : ,
"timed_out" : false,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"hits" : {
"total" : ,
"max_score" : 0.19178301,
"hits" : [ {
"_index" : "wjy",
"_type" : "employee",
"_id" : "AWlv91mF4hD4em0kzO4V",
"_score" : 0.19178301,
"_source" : {
"first_name" : "gob bin",
"age" : ,
"about" : "I love to go rock climbing",
"interests" : [ "sports", "music" ]
}
}, {
"_index" : "wjy",
"_type" : "employee",
"_id" : "",
"_score" : 0.19178301,
"_source" : {
"first_name" : "god bin",
"last_name" : "pang",
"age" : ,
"about" : "I love to go rock climbing",
"interests" : [ "sports", "music" ]
}
} ]
}
}

#must_not+must_not  : 并集

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
> {
> "query":
> {"bool" :
> {
> "must_not" :
> {"match":
> {"first_name":"bin"}
> },
> "must_not" :
> {"match":
> {"age":}
> }
> }
> }
> }'
{
"took" : ,
"timed_out" : false,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"hits" : {
"total" : ,
"max_score" : null,
"hits" : [ ]
}
}

(5)删除

curl -XDELETE http://196.168.123.101:9200/test2/

(6)设置

#设置备份数
curl -XPUT 'http://196.168.123.101:9200/test2/' -d'{"settings":{"number_of_replicas":2}}' #设置分片数 备份数
curl -XPUT 'http://196.168.123.101:9200/test3/' -d'{"settings":{"number_of_shards":3,"number_of_replicas":3}}'

【Elasticsearch学习之二】Elasticsearch Rest风格操作的更多相关文章

  1. ElasticSearch实战系列二: ElasticSearch的DSL语句使用教程---图文详解

    前言 在上一篇中介绍了ElasticSearch集群和kinaba的安装教程,本篇文章就来讲解下 ElasticSearch的DSL语句使用. ElasticSearch DSL 介绍 Elastic ...

  2. Docker学习(二)docker镜像操作

    上一篇:docker学习(一)在centos7上安装docker 列出所有docker镜像 docker images 拉取镜像 docker pull 镜像名 我这里一Tomact为例 首先在Doc ...

  3. jquery学习笔记(二):DOM元素操作

    内容来自[汇智网]jquery学习课程 2.1 元素属性操作 1.获取元素的属性 语法:attr(name) 参数name表示属性的名称 2.设置元素的属性 单个属性设置语法:attr(key,val ...

  4. Elasticsearch学习系列二(基础操作)

    本文将分为3块讲解Es的基础操作.分别为:索引(index).映射(mapping).文档(document). 索引操作 创建索引库 语法: PUT /索引名称{ "settings&qu ...

  5. 2018/2/11 ELK技术栈之ElasticSearch学习笔记二

    终于有时间记录一下最近学习的知识了,其实除了写下的这些还有很多很多,但懒得一一写下了: ElasticSearch添加修改删除原理:ElasticSearch的倒排索引和文档一旦生成就不允许修改(其实 ...

  6. Elasticsearch 学习(二):安装和使用

    一.安装 安装 Elasticsearch 之前,需要先安装 Java,并配置好 Java 环境变量. 安装好 Java 环境后,进入 Elasticsearch 官网下载安装包. 解压安装包,进入解 ...

  7. Elasticsearch学习笔记二

    PS:上一篇已经介绍了ES的一些基础概念以及单机版ES的安装,配置,本文主要介绍ES的集群管理,CRUD以及简单聚合查询. 集群管理 ES的集群部署起来也很方便,将单机版SCP复制几分,修改elast ...

  8. ElasticSearch学习笔记-02集群相关操作_cat参数

    _cat参数允许你查看集群的一些相关信息,如集群是否健康,有哪些节点,以及索引的情况等的. 检测集群是否健康 curl localhost:9200/_cat/health?v 说明: curl 是一 ...

  9. Elasticsearch学习(二)————搜索

    Elasticsearch1.query string search1.1.搜索全部// 1. GET http://ip:9200/test/test/_search 结果: { "too ...

随机推荐

  1. java 网络编程(三)简单的即时通讯(UDP传输)

    发送端: package cn.sasa.netDemo2; import java.io.IOException; import java.net.DatagramPacket; import ja ...

  2. centos中文语言安装

    1.查看当前使用的系统语言 #echo LANG 2.查看系统是否安装中文 #locale 如有zh_cn,表示已经安装了中文语言 3.安装中文 #yum groupinstall chinese-s ...

  3. 关于分页Pagination的使用

    在这个例子当中,用的是ssm框架整合,并且用的是Pagination实现分页 先来看一下分页中用到的类的源码 Paginable.java package cn.itcast.common.page; ...

  4. 关于eclipse调试时程序控制台不能自动打开

      对于这个程序,在刚开始的时候,没有敲上22,29,33行的打印语句时,在调试的时候不会自动弹开控制台,所以一直在怀疑代码可能出错了.因此可以自己手动打开,但是如果敲上那些代码,系统可以自动弹开控制 ...

  5. Linux I/O 调度器

    每个块设备或者块设备的分区,都对应有自身的请求队列,  而每个请求队列都可以选择一个I/O调度器来协调所递交的.I/O调度器的基本目的是将请求按照它们对应在块设备上的扇区号进行排列,以减少磁头的移动, ...

  6. 查看Centos内存使用情况linux命令

    我们在使用centos版linux服务器的过程中,有时会出现卡顿的情况,这时我们可以通过查看一下内存的使用来判断发生了什么情况,那么如何查看centos内容使用情况呢?有几个方法可以尝试,跟着ytka ...

  7. c# ThreadPool 判断子线程全部执行完毕的四种方法

    1.先来看看这个 多线程编程 多线程用于数据采集时,速度明显很快,下面是基本方法,把那个auto写成采集数据方法即可. using System; using System.Collections.G ...

  8. boost生成json中的put操作

    ptree中的put操作后可以加<>,指定类型,不加<>采用默认的类型,感觉不加反而更好用.用法见下面例子. #include <iostream> #includ ...

  9. git flow 使用步骤

    Mac安装git-flow:brew install git-flow 克隆新代码:git clone git@gitlab.xxx.cn:abc/test.git 切换到远程的develop分支(很 ...

  10. Day1 Python基础学习——概述、基本数据类型、流程控制

    一.Python基础学习 一.编程语言分类 1.简介 机器语言:站在计算机的角度,说计算机能听懂的语言,那就是直接用二进制编程,直接操作硬件 汇编语言:站在计算机的角度,简写的英文标识符取代二进制去编 ...