ES查询指南

  1. 我们通常用用_cat API检测集群是否健康。 确保9200端口号可用:

  curl 'localhost:9200/_cat/health?v'

  绿色表示一切正常, 黄色表示所有的数据可用但是部分副本还没有分配,红色表示部分数据因为某些原因不可用.

  2.通过如下语句,我们可以获取集群的节点列表:

  curl 'localhost:9200/_cat/nodes?v'

  3。通过如下语句,列出所有索引:

  curl 'localhost:9200/_cat/indices?v'

 4.创建索引

  现在我们创建一个名为“customer”的索引,然后再查看所有的索引:

 curl -XPUT 'localhost:9200/customer?pretty'

5.插入和获取

  现在我么插入一些数据到集群索引。我们必须给ES指定所以的类型。如下语句:"external" type, ID:1:

  主体为JSON格式的语句: { "name": "John Doe" }

  curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
  {
           "name": "John Doe"
  }'

6.删除索引 DELETE

  curl -XDELETE 'localhost:9200/customer?pretty'

 7.通过以上命令语句的学习,我们发现索引的增删改查有一个类似的格式,总结如下:

  curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>

  <REST Verb>:REST风格的语法谓词

  <Node>:节点ip

  <port>:节点端口号,默认9200

  <Index>:索引名

  <Type>:索引类型

  <ID>:操作对象的ID号

  8 修改数据

  curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
  {
    "name": "John Doe"
  }'
  curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
  {
    "name": "Jane Doe"
  }'

  上述命令语句是:先新增id为1,name为John Doe的数据,然后将id为1的name修改为Jane Doe。

  9.更新数据

  9.1 这个例子展示如何将id为1文档的name字段更新为Jane Doe:

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
  {
    "doc": { "name": "Jane Doe" }
  }'

  9.2 这个例子展示如何将id为1数据的name字段更新为Jane Doe同时增加字段age为20:

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
  {
    "doc": { "name": "Jane Doe", "age": 20 }
  }'

  9.3  也可以通过一些简单的scripts来执行更新。一下语句通过使用script将年龄增加5:

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
  {
    "script" : "ctx._source.age += 5"
  }'

  10 删除数据

  删除数据那是相当的直接. 下面的语句将执行删除Customer中ID为2的数据:

  curl -XDELETE 'localhost:9200/customer/external/2?pretty'

  11 批处理

  举例:

  下面语句将在一个批量操作中执行创建索引:

  curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
  {"index":{"_id":"1"}}
  {"name": "John Doe" }
  {"index":{"_id":"2"}}
  {"name": "Jane Doe" }
  '

  下面语句批处理执行更新id为1的数据然后执行删除id为2的数据

  curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
  {"update":{"_id":"1"}}
  {"doc": { "name": "John Doe becomes Jane Doe" } }
  {"delete":{"_id":"2"}}

12.导入数据集 

  导入示例数据集:

  curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"

13.查询

  Sample:

  curl 'localhost:9200/bank/_search?q=*&pretty'

14 查询语言

  匹配所有数据,但只返回1个:

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '

  {

    "query": { "match_all": {} },

    "size": 1

  }'

  注意:如果siez不指定,则默认返回10条数据。

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '

  {

    "query": { "match_all": {} },

    "from": 10,

    "size": 10

  }'

  返回从11到20的数据。(索引下标从0开始)

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '

  {

    "query": { "match_all": {} },

    "sort": { "balance": { "order": "desc" } }

  }'

  上述示例匹配所有的索引中的数据,按照balance字段降序排序,并且返回前10条(如果不指定size,默认最多返回10条)。

15.执行搜索

  下面例子展示如何返回两个字段(account_number balance)

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '

  {

    "query": { "match_all": {} },

    "_source": ["account_number", "balance"]

  }'

 16.过滤filter(查询条件设置)

  下面这个例子使用了布尔查询返回balance在20000到30000之间的所有数据。

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '

  {

      "query": {

        "bool": {

          "must": { "match_all": {} },

          "filter": {

            "range": {

            "balance": {

              "gte": 20000,

              "lte": 30000

            }

          }

        }

      }

    }

  }'

  17 聚合 Aggregations

  下面这个例子: 将所有的数据按照state分组(group),然后按照分组记录数从大到小排序,返回前十条(默认):

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '

  {

    "size": 0,

    "aggs": {

      "group_by_state": {

        "terms": {

           "field": "state"

        }

      }

    }

  }'

  注意:我们设置size=0,不显示查询hits,因为我们只想看返回的聚合结果。

  18 查询某个type的Mapping

   curl -XGET 'http://local:9200/my_index/_mapping/my_type'

猜你喜欢

转载自www.cnblogs.com/szss/p/9877506.html