一、es特性


二、使用命令

  1. 查看集群健康状况

    curl 127.0.0.1:9200/_cat/health?v

  2. 列出所有node节点

    curl 127.0.0.1:9200/_cat/nodes?v

  3. 查看索引信息

    curl 127.0.0.1:9200/_cat/indices?v

  4. 创建索引

    curl -X PUT 127.0.0.1:9200/customer?pretty

  5. 添加文档

    curl -X PUT "localhost:9200/customer/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'

    {

      "name": "John Doe"

    }

  6. 检索索引的文档

    curl -X GET "localhost:9200/customer/_doc/1?pretty&pretty"

  7. 删除索引

    curl -X DELETE "localhost:9200/customer?pretty"

  8. 更新文档

    curl -X PUT "localhost:9200/customer/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'

    {

    "name": "John Doe"

    }'

  9. 索引一个新文档

    curl -X PUT "localhost:9200/customer/_doc/2?pretty&pretty" -H 'Content-Type: application/json' -d'

    {

      "name": "Jane Doe"

    }'


  10. 索引一个没有显式id的文档


  11. curl -X POST "localhost:9200/customer/_doc?pretty&pretty" -H 'Content-Type: application/json' -d'

    {

      "name": "Jane Doe"

    }'

  12. 更新文档

    curl -X POST "localhost:9200/customer/_update/1?pretty&pretty" -H 'Content-Type: application/json' -d'

    {

      "doc": { "name": "Jane Doe" }

    }'

  13. 更新文档并添加字段

    curl -X POST "localhost:9200/customer/_update/1?pretty&pretty" -H 'Content-Type: application/json' -d'

    {

      "doc": { "name": "Jane Doe", "age": 20 }

    }'

  14. 使用脚本执行更新年龄到25岁

    curl -X POST "localhost:9200/customer/_update/1?pretty&pretty" -H 'Content-Type: application/json' -d'

    {

     "script" : "ctx._source.age += 5"

    }'

  15. 查看文档

    curl -X GET "localhost:9200/customer/_doc/2?pretty"

  16. 删除文档

    curl -X DELETE "localhost:9200/customer/_doc/2?pretty&pretty"

  17. 批量创建文档

    curl -X POST "localhost:9200/customer/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'

    {"index":{"_id":"1"}}

    {"name": "John Doe" }

    {"index":{"_id":"2"}}

    {"name": "Jane Doe" }'

  18. 更新第一个文档并删除第二个文档

    curl -X POST "localhost:9200/customer/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'

    {"update":{"_id":"1"}}

    {"doc": { "name": "John Doe becomes Jane Doe" } }

    {"delete":{"_id":"2"}}'

  19. 搜索文档 "*"参数匹配索引中的所有文档, account_number按照文档的这个字段升序排列

    curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty&pretty"

    注意命中的准确性由请求参数track_total_hits控制,当设置为true时,请求将精确地跟踪总命中(“relationship”:“eq”)。默认值为10,000,这意味着总命中数可以精确地跟踪到10,000个文档。通过显式地将track_total_hits设置为true,可以强制进行准确的计数。有关详细信息,请参阅请求主体文档

  20. 同一效果一样,用json风格查询请求体

    curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

    {

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

      "sort": [

        { "account_number": "asc" }

      ]

    }'

三、DSL查询语言

1.

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

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

}

'

2.

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

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

  "size": 1

}

'

size: 返回文档数

3.返回第10到第19个文档

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

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

  "from": 10,

  "size": 10

}

'

from:从哪个索引文档开始,默认为0

4. 降序排列并返回前10个文档

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

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

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

}

'

5.默认返回搜索文档的所有字段,可以指定返回字段

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

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

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

}

'

只返回["account_number", "balance"]字段

6.match匹配查询

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

  "query": { "match": { "account_number": 20 } }

}

'

只返回account_number是20的文档

7.匹配address包含"mill"的文档,不区分大小写

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

  "query": { "match": { "address": "mill" } }

}

'

8.返回address 包含"mill" 或者"lane"的文档

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

  "query": { "match": { "address": "mill lane" } }

}

'

9.match的变体,返回address包含"mill lane"字段的文档

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

  "query": { "match_phrase": { "address": "mill lane" } }

}

'

10.bool查询返回匹配到{ "match": { "address": "mill" } },和{ "match": { "address": "lane" } }的文档,must指定必须同时满足

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

  "query": {

    "bool": {

      "must": [

        { "match": { "address": "mill" } },

        { "match": { "address": "lane" } }

      ]

    }

  }

}

'

11.bool或查询满足{ "match": { "address": "mill" } },或{ "match": { "address": "lane" } }的文档,should指定满足其中一个条件即可

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

  "query": {

    "bool": {

      "should": [

        { "match": { "address": "mill" } },

        { "match": { "address": "lane" } }

      ]

    }

  }

}'

12,bool反向查询 查询既不满足{ "match": { "address": "mill" } },也不满足{ "match": { "address": "lane" } }的文档,must_not跟mast相反 

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

  "query": {

    "bool": {

      "must_not": [

        { "match": { "address": "mill" } },

        { "match": { "address": "lane" } }

      ]

    }

  }

}

'

13.bool联合查询,must,must_not同时使用

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

  "query": {

    "bool": {

      "must": [

        { "match": { "age": "40" } }

      ],

      "must_not": [

        { "match": { "state": "ID" } }

      ]

    }

  }

}

'

_score字段值越大,查询的文档越相关

四、filter过滤

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

  "query": {

    "bool": {

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

      "filter": {

        "range": {

          "balance": {

            "gte": 20000,

            "lte": 30000

          }

        }

      }

    }

  }

}

五、聚合查询

1.按状态对所有账户分组,然后返回按count降序排列的前10个(默认)状态(也是默认)

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

  "size": 0,

  "aggs": {

    "group_by_state": {

      "terms": {

        "field": "state.keyword"

      }

    }

  }

}

'

相当于sql语句:

SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC LIMIT 10;

注意:将size=0设置为不显示搜索结果,因为我们只想看到响应中的聚合结果,group_by_state以state字段聚合



2.聚合求平均值

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

  "size": 0,

  "aggs": {

    "group_by_state": {

      "terms": {

        "field": "state.keyword"

      },

      "aggs": {

        "average_balance": {

          "avg": {

            "field": "balance"

          }

        }

      }

    }

  }

}

'

本示例按状态计算平均帐户余额(同样只针对按count降序排列的前10个状态)


3.平均值降序聚合

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

  "size": 0,

  "aggs": {

    "group_by_state": {

      "terms": {

        "field": "state.keyword",

        "order": {

          "average_balance": "desc"

        }

      },

      "aggs": {

        "average_balance": {

          "avg": {

            "field": "balance"

          }

        }

      }

    }

  }

}

'

4.多个聚合一起使用

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

{

  "size": 0,

  "aggs": {

    "group_by_age": {

      "range": {

        "field": "age",

        "ranges": [

          {

            "from": 20,

            "to": 30

          },

          {

            "from": 30,

            "to": 40

          },

          {

            "from": 40,

            "to": 50

          }

        ]

      },

      "aggs": {

        "group_by_gender": {

          "terms": {

            "field": "gender.keyword"

          },

          "aggs": {

            "average_balance": {

              "avg": {

                "field": "balance"

              }

            }

          }

        }

      }

    }

  }

}

'

这个例子展示了我们如何按照年龄等级(20-29岁,30-39岁,40-49岁)分组,然后按性别分组,最后得到每个年龄等级,每个性别的平均账户余额