环境
  虚拟机: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

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

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

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

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

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

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

3、增加field:sex

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

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

  1. [cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/1?pretty
  2. {
  3. "_index" : "wjy",
  4. "_type" : "employee",
  5. "_id" : "",
  6. "_version" : ,
  7. "found" : true,
  8. "_source" : {
  9. "first_name" : "god bin",
  10. "last_name" : "pang",
  11. "age" : ,
  12. "about" : "I love to go rock climbing",
  13. "interests" : [ "sports", "music" ]
  14. }
  15. }

对比:

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

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

  1. [cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?q=first_name="bin"
  2. {"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":
  3. {
  4. "first_name" : "bin",
  5. "age" : ,
  6. "about" : "I love to go rock climbing",
  7. "interests": [ "sports", "music" ]
  8. }},{"_index":"wjy","_type":"employee","_id":"AWlv91mF4hD4em0kzO4V","_score":0.01125201,"_source":
  9. {
  10. "first_name" : "gob bin",
  11. "age" : ,
  12. "about" : "I love to go rock climbing",
  13. "interests": [ "sports", "music" ]
  14. }},{"_index":"wjy","_type":"employee","_id":"","_score":0.01125201,"_source":
  15. {
  16. "first_name" : "god bin",
  17. "last_name" : "pang",
  18. "age" : ,
  19. "about" : "I love to go rock climbing",
  20. "interests": [ "sports", "music" ]
  21. }}]}}

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

  1. [cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
  2. > {
  3. > "query":
  4. > {"match":
  5. > {"first_name":"bin"}
  6. > }
  7. > }'
  8. {
  9. "took" : ,
  10. "timed_out" : false,
  11. "_shards" : {
  12. "total" : ,
  13. "successful" : ,
  14. "failed" :
  15. },
  16. "hits" : {
  17. "total" : ,
  18. "max_score" : 0.30685282,
  19. "hits" : [ {
  20. "_index" : "wjy",
  21. "_type" : "employee",
  22. "_id" : "AWlv8cAl4hD4em0kzO4U",
  23. "_score" : 0.30685282,
  24. "_source" : {
  25. "first_name" : "bin",
  26. "age" : ,
  27. "about" : "I love to go rock climbing",
  28. "interests" : [ "sports", "music" ]
  29. }
  30. }, {
  31. "_index" : "wjy",
  32. "_type" : "employee",
  33. "_id" : "AWlv91mF4hD4em0kzO4V",
  34. "_score" : 0.19178301,
  35. "_source" : {
  36. "first_name" : "gob bin",
  37. "age" : ,
  38. "about" : "I love to go rock climbing",
  39. "interests" : [ "sports", "music" ]
  40. }
  41. }, {
  42. "_index" : "wjy",
  43. "_type" : "employee",
  44. "_id" : "",
  45. "_score" : 0.19178301,
  46. "_source" : {
  47. "first_name" : "god bin",
  48. "last_name" : "pang",
  49. "age" : ,
  50. "about" : "I love to go rock climbing",
  51. "interests" : [ "sports", "music" ]
  52. }
  53. } ]
  54. }
  55. }

#对多个field发起查询:multi_match

  1. [cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
  2. > {
  3. > "query":
  4. > {"multi_match":
  5. > {
  6. > "query":"bin",
  7. > "fields":["last_name","first_name"],
  8. > "operator":"and"
  9. > }
  10. > }
  11. > }'
  12. {
  13. "took" : ,
  14. "timed_out" : false,
  15. "_shards" : {
  16. "total" : ,
  17. "successful" : ,
  18. "failed" :
  19. },
  20. "hits" : {
  21. "total" : ,
  22. "max_score" : 0.09415865,
  23. "hits" : [ {
  24. "_index" : "wjy",
  25. "_type" : "employee",
  26. "_id" : "AWlv8cAl4hD4em0kzO4U",
  27. "_score" : 0.09415865,
  28. "_source" : {
  29. "first_name" : "bin",
  30. "age" : ,
  31. "about" : "I love to go rock climbing",
  32. "interests" : [ "sports", "music" ]
  33. }
  34. }, {
  35. "_index" : "wjy",
  36. "_type" : "employee",
  37. "_id" : "AWlv91mF4hD4em0kzO4V",
  38. "_score" : 0.058849156,
  39. "_source" : {
  40. "first_name" : "gob bin",
  41. "age" : ,
  42. "about" : "I love to go rock climbing",
  43. "interests" : [ "sports", "music" ]
  44. }
  45. }, {
  46. "_index" : "wjy",
  47. "_type" : "employee",
  48. "_id" : "",
  49. "_score" : 0.058849156,
  50. "_source" : {
  51. "first_name" : "god bin",
  52. "last_name" : "pang",
  53. "age" : ,
  54. "about" : "I love to go rock climbing",
  55. "interests" : [ "sports", "music" ]
  56. }
  57. } ]
  58. }
  59. }

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

  1. [cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
  2. > {
  3. > "query":
  4. > {"bool" :
  5. > {
  6. > "must" :
  7. > {"match":
  8. > {"first_name":"bin"}
  9. > },
  10. > "must" :
  11. > {"match":
  12. > {"age":}
  13. > }
  14. > }
  15. > }
  16. > }'
  17. {
  18. "took" : ,
  19. "timed_out" : false,
  20. "_shards" : {
  21. "total" : ,
  22. "successful" : ,
  23. "failed" :
  24. },
  25. "hits" : {
  26. "total" : ,
  27. "max_score" : 0.4339554,
  28. "hits" : [ {
  29. "_index" : "wjy",
  30. "_type" : "employee",
  31. "_id" : "AWlv8cAl4hD4em0kzO4U",
  32. "_score" : 0.4339554,
  33. "_source" : {
  34. "first_name" : "bin",
  35. "age" : ,
  36. "about" : "I love to go rock climbing",
  37. "interests" : [ "sports", "music" ]
  38. }
  39. } ]
  40. }
  41. }

#must +must_not :差集

  1. [cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
  2. > {
  3. > "query":
  4. > {"bool" :
  5. > {
  6. > "must" :
  7. > {"match":
  8. > {"first_name":"bin"}
  9. > },
  10. > "must_not" :
  11. > {"match":
  12. > {"age":}
  13. > }
  14. > }
  15. > }
  16. > }'
  17. {
  18. "took" : ,
  19. "timed_out" : false,
  20. "_shards" : {
  21. "total" : ,
  22. "successful" : ,
  23. "failed" :
  24. },
  25. "hits" : {
  26. "total" : ,
  27. "max_score" : 0.19178301,
  28. "hits" : [ {
  29. "_index" : "wjy",
  30. "_type" : "employee",
  31. "_id" : "AWlv91mF4hD4em0kzO4V",
  32. "_score" : 0.19178301,
  33. "_source" : {
  34. "first_name" : "gob bin",
  35. "age" : ,
  36. "about" : "I love to go rock climbing",
  37. "interests" : [ "sports", "music" ]
  38. }
  39. }, {
  40. "_index" : "wjy",
  41. "_type" : "employee",
  42. "_id" : "",
  43. "_score" : 0.19178301,
  44. "_source" : {
  45. "first_name" : "god bin",
  46. "last_name" : "pang",
  47. "age" : ,
  48. "about" : "I love to go rock climbing",
  49. "interests" : [ "sports", "music" ]
  50. }
  51. } ]
  52. }
  53. }

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

  1. [cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
  2. > {
  3. > "query":
  4. > {"bool" :
  5. > {
  6. > "must" :
  7. > {"term" :
  8. > { "first_name" : "bin" }
  9. > }
  10. > ,
  11. > "must_not" :
  12. > {"range":
  13. > {"age" : { "from" : , "to" : }
  14. > }
  15. > }
  16. > }
  17. > }
  18. > }'
  19. {
  20. "took" : ,
  21. "timed_out" : false,
  22. "_shards" : {
  23. "total" : ,
  24. "successful" : ,
  25. "failed" :
  26. },
  27. "hits" : {
  28. "total" : ,
  29. "max_score" : 0.19178301,
  30. "hits" : [ {
  31. "_index" : "wjy",
  32. "_type" : "employee",
  33. "_id" : "AWlv91mF4hD4em0kzO4V",
  34. "_score" : 0.19178301,
  35. "_source" : {
  36. "first_name" : "gob bin",
  37. "age" : ,
  38. "about" : "I love to go rock climbing",
  39. "interests" : [ "sports", "music" ]
  40. }
  41. }, {
  42. "_index" : "wjy",
  43. "_type" : "employee",
  44. "_id" : "",
  45. "_score" : 0.19178301,
  46. "_source" : {
  47. "first_name" : "god bin",
  48. "last_name" : "pang",
  49. "age" : ,
  50. "about" : "I love to go rock climbing",
  51. "interests" : [ "sports", "music" ]
  52. }
  53. } ]
  54. }
  55. }

#must_not+must_not  : 并集

  1. [cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
  2. > {
  3. > "query":
  4. > {"bool" :
  5. > {
  6. > "must_not" :
  7. > {"match":
  8. > {"first_name":"bin"}
  9. > },
  10. > "must_not" :
  11. > {"match":
  12. > {"age":}
  13. > }
  14. > }
  15. > }
  16. > }'
  17. {
  18. "took" : ,
  19. "timed_out" : false,
  20. "_shards" : {
  21. "total" : ,
  22. "successful" : ,
  23. "failed" :
  24. },
  25. "hits" : {
  26. "total" : ,
  27. "max_score" : null,
  28. "hits" : [ ]
  29. }
  30. }

(5)删除

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

(6)设置

  1. #设置备份数
  2. curl -XPUT 'http://196.168.123.101:9200/test2/' -d'{"settings":{"number_of_replicas":2}}'
  3.  
  4. #设置分片数 备份数
  5. 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 Arrays工具

    package cn.sasa.demo4; import java.util.Arrays; public class ArrayDemo { public static void main(Str ...

  2. pyqt5-day1

    pyqt5做为Python的一个模块,它有620多个类和6000个函数和方法.这是一个跨平台的工具包,它可以运行在所有主要的操作系统,包括UNIX,Windows,Mac OS.pyqt5是双重许可. ...

  3. oracle创建表空间 授权

    --创建表空间 临时表空间 create temporary tablespace xiaodai_temp tempfile '/main/app/oracle/oradata/devdb/xiao ...

  4. JavaScript学习(七)

  5. 代码控如何实现配置fiddler

    很多小哥哥总觉得测试点点点很low,总想码代码.那么fiddler除了一些手动设置外,还可以进行丰富的代码编写,用以完成任务. 打开fiddler,工具栏选择Rules->Customize R ...

  6. JetBrains 2017/2018全系列产品激活工具

    可谓是工欲善其事,必先利其器,相信作为优秀开发工程师的你都想拥有一套快捷高效的编码工具,而JetBrains这家公司的产品,不管是那种编程语言,其开发工具确实让开发者们着迷,JetBrains的产品博 ...

  7. NYOJ 方案数量

    1.递归求解(直接递归会超时,要用备忘录法) # include<iostream> # include<stdio.h> #include <map> using ...

  8. 解决ios10以上点击缩放的问题

    禁止ios10以上点击缩放,代码如下: <script> window.onload=function () { document.addEventListener('touchstart ...

  9. Py之zip方法【转载】

    转自:http://www.runoob.com/python/python-func-zip.html zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些 ...

  10. [LeetCode] 490. The Maze_Medium tag: BFS/DFS

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...