使用CURL命令操作ES

  • 2019 年 10 月 5 日
  • 筆記

使用CURL命令操作ES

当前文档所用ES版本 6.4.3

ElasticSearch 提供了一系列的Restful风格的API,我们可以使用curl命令进行使用,也可以在kibana中使用。

Restful风格

它是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。 RESTFUL适用于移动互联网厂商作为业务使能接口的场景,实现第三方OTT调用移动网络资源的功能,动作类型为新增、变更、删除所调用资源。通过不同的请求方法来实现不同的功能。

GET 获取数据 POST 添加数据 PUT 添加数据 DELETE 删除数据

ElasticSearch的核心概念与关系数据库对比

集群常用命令

  • 查看版本
curl  -XGET 'http://hadoop137:9200'
  • 查看集群状态
curl -XGET 'http://hadoop137:9200/_cluster/state?pretty'    #这里在url后面添加了pretty是为了让其在控制台上输出的结果是一个优美的json格式

索引库常用命令

  • 查看所有索引信息
curl -XGET 'http://hadoop137:9200/_cat/indices?pretty&v'
  • 创建索引
curl -XPUT 'http://hadoop137:9200/upuptop?pretty'
  • 删除索引
curl -XDELETE 'http://hadoop137:9200/upuptop?pretty'

文档常用命令

  • 创建文档
# 9200/索引库名/文档类型/id/  -d 文档内容  # id可以忽略,ES会自动生成id,如果id存在,那么就是更新数据,字段可以增加    curl -XPOST 'http://hadoop137:9200/upuptop/stu/1?pretty' -H 'Content-Type: application/json' -d '  {  "name":"shaofei",  "age":22,  "sex":1  }'
  • 修改文档
# 给id为1的学生修改姓名为upuptop,年龄修改为25,添加学号字段  curl -XPOST 'http://hadoop137:9200/upuptop/stu/1?pretty' -H 'Content-Type: application/json' -d '  {  "name":"upuptop",  "age":25,  "sex":0,  "number":"1501260222"  }'
  • 查看所有文档
curl -XGET 'http://hadoop137:9200/upuptop/stu/_search?pretty'
  • 根据id查看文档
curl -XGET 'http://hadoop137:9200/upuptop/stu/1?pretty'
  • 删除文档
curl -XDELETE 'http://hadoop137:9200/upuptop/stu/1?pretty'

查询命令

ES最主要的功能,搜索,也是就是查询文档。下面我们来看看主要的查询命令吧。

首先搞点数据到ElasticSearch中

这里使用logstash工具将mysql数据库中的数据导入到ES中

对于LogStash的介绍请查看这篇文章:《LogStash的安装部署与应用》。

使用查询命令对数据进行查询。

  1. 根据field来查询数据:

查询name含有‘upuptop'字符串的数据、模糊查询

curl -XGET http://hadoop137:9200/upuptop/stu/_search?q=name="upuptop"
  1. 根据field来查询数据:match

查询name含有‘upuptop'字符串的数据、模糊查询

curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty  -H 'Content-Type: application/json' -d '  {   "query":    {"match":     {"name":"upuptop"}    }  }'
  1. 对多个field发起查询:multi_match

查询lastname、firstname含有‘upuptop'字符串的数据

curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -d '  {   "query":    {"multi_match":     {      "query":"upuptop",      "fields":["last_name","first_name"],      "operator":"and"     }    }  }'
  1. 多个term对多个field发起查询:bool(boolean)
# 组合查询,must,must_not,should  #  must + must : 交集  #  must +must_not :差集  #  should+should  : 并集    # 查询 first_name为upuptop并且年龄是33的数据  curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -d '  {   "query":    {"bool" :     {      "must" :       {"match":        {"first_name":"upuptop"}       },      "must" :       {"match":        {"age":33}       }     }    }  }'    # 查询 first_name为upuptop并且年龄不是33的数据  curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -d '  {   "query":    {"bool" :     {      "must" :       {"match":        {"first_name":"upuptop"}       },      "must_not" :       {"match":        {"age":33}       }     }    }  }'    # 查询 first_name不为upuptop并且年龄不是33的数据  curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -d '  {   "query":    {"bool" :     {      "must_not" :       {"match":        {"first_name":"upuptop"}       },      "must_not" :       {"match":        {"age":33}       }     }    }  }'
  1. 查询first_name=upuptop的,或者年龄在20岁到33岁之间的
curl -XGET http://hadoop137:9200/upuptop/stu/_search -d '  {   "query":    {"bool" :     {     "must" :      {"term" :       { "first_name" : "upuptop" }      }     ,     "must_not" :      {"range":       {"age" : { "from" : 20, "to" : 33 }      }     }     }    }  }'
  1. 修改配置
## 创建索引库并设置副本数为2个,默认5个主分片,每个分片有两个副本,一共15个片    curl -XPUT 'http://hadoop137:9200/test2/' -d'{"settings":{"number_of_replicas":2}}'    ## 设置3个主分片、3个从分片  curl -XPUT 'http://hadoop137:9200/test3/' -d'{"settings":{"number_of_shards":3,"number_of_replicas":3}}'    curl -XPUT 'http://hadoop137:9200/test4/' -d'{"settings":{"number_of_shards":6,"number_of_replicas":4}}'    ## 设置索引的mapping,设置数据类型,分词规则等    curl -XPOST http://192.168.9.11:9200/upuptop/person/_mapping -d'  {      "person": {          "properties": {              "content": {                  "type": "string",                  "store": "no",                  "term_vector": "with_positions_offsets",                  "analyzer": "ik_max_word",                  "search_analyzer": "ik_max_word",                  "include_in_all": "true",                  "boost": 8              }          }      }  }'