• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

ES常见查询展示

武飞扬头像
guaotianxia
帮助1

环境:es(7.14.0) kibana(7.14.2)

一、ES查询示例

1、查看es信息

GET /

学新通

2、创建索引

PUT demo_person

学新通

3、删除索引

DELETE demo_person

说明:

DELETE /index_one,index_two  --删除两个索引

DELETE /index_*  --删除index_k开头的索引

DELETE /_all  --删除全部索引

DELETE /*  --删除全部索引

4、创建索引包含setting和mapping

  1.  
    PUT demo_person
  2.  
    {
  3.  
    "settings": {
  4.  
    "number_of_shards": 5,
  5.  
    "number_of_replicas": 1
  6.  
    },
  7.  
    "mappings": {
  8.  
    "properties": {
  9.  
    "about": {
  10.  
    "type": "text",
  11.  
    "fields": {
  12.  
    "keyword": {
  13.  
    "type": "keyword",
  14.  
    "ignore_above": 256
  15.  
    }
  16.  
    }
  17.  
    },
  18.  
    "age": {
  19.  
    "type": "long"
  20.  
    },
  21.  
    "first_name": {
  22.  
    "type": "text",
  23.  
    "fields": {
  24.  
    "keyword": {
  25.  
    "type": "keyword",
  26.  
    "ignore_above": 256
  27.  
    }
  28.  
    }
  29.  
    },
  30.  
    "interests": {
  31.  
    "type": "text",
  32.  
    "fields": {
  33.  
    "keyword": {
  34.  
    "type": "keyword",
  35.  
    "ignore_above": 256
  36.  
    }
  37.  
    },
  38.  
    "fielddata": true
  39.  
    },
  40.  
    "last_name": {
  41.  
    "type": "keyword"
  42.  
    }
  43.  
    }
  44.  
    }
  45.  
    }
学新通

学新通

5、修改setting

  1.  
    PUT demo_person/_settings
  2.  
    {
  3.  
    "number_of_replicas" : 2
  4.  
    }

学新通

只能修改副本分片,主分片在创建索引时确定,后续不可以再次修改

6、修改mapping

  1.  
    PUT demo_person/_mapping/_doc?include_type_name=true
  2.  
    {
  3.  
    "properties":{
  4.  
    "interests":{
  5.  
    "type":"text",
  6.  
    "fielddata":true
  7.  
    }
  8.  
    }
  9.  
    }

学新通

7、查询总数

  1.  
    GET demo_person/_count
  2.  
    {
  3.  
    "query": {
  4.  
    "match_all": {}
  5.  
    }
  6.  
    }

学新通

8、添加/修改数据

  1.  
    PUT /demo_person/_doc/1
  2.  
    {
  3.  
    "first_name" : "John",
  4.  
    "last_name" : "Smith",
  5.  
    "age" : 25,
  6.  
    "about" : "I love to go rock climbing",
  7.  
    "interests": [ "sports", "music" ]
  8.  
    }

学新通

9、批量插入数据

  1.  
    POST demo_person/_bulk
  2.  
    {"index":{}}
  3.  
    {"first_name":"zhang","last_name":"san","age" :44,"about":"I like to collect hehe albums","interests":["music"]}
  4.  
    {"index":{}}
  5.  
    {"first_name":"li","last_name":"si","age":12,"about":"I like to drink","interests":["drink"]}

学新通

10、查询所有数据

  1.  
    GET demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "match_all": {}
  5.  
    }
  6.  
    }

学新通

11、查询指定条数

  1.  
    GET demo_person/_search
  2.  
    { "size": 20,
  3.  
    "query": {
  4.  
    "match_all": {}
  5.  
    }
  6.  
    }

学新通

12、根据ID查询

GET /demo_person/_doc/1?pretty

学新通

13、一个查询字符串搜索

GET /demo_person/_search?q=last_name:Smith

学新通

14、match搜索

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query" : {
  4.  
    "match" : {
  5.  
    "last_name" : "Smith"
  6.  
    }
  7.  
    }
  8.  
    }

学新通

15、term搜索

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "term": {
  5.  
    "last_name": {
  6.  
    "value": "Smith"
  7.  
    }
  8.  
    }
  9.  
    }
  10.  
    }

学新通

基于词项的查询

如 term 或 fuzzy 这样的底层查询不需要分析阶段,它们对单个词项进行操作。用 term 查询词项 Foo 只要在倒排索引中查找 准确词项 ,并且用 TF/IDF 算法为每个包含该词项的文档计算相关度评分 _score 。

记住 term 查询只对倒排索引的词项精确匹配,这点很重要,它不会对词的多样性进行处理(如, foo 或 FOO )。这里,无须考虑词项是如何存入索引的。如果是将 ["Foo","Bar"] 索引存入一个不分析的( not_analyzed )包含精确值的字段,或者将 Foo Bar 索引到一个带有 whitespace 空格分析器的字段,两者的结果都会是在倒排索引中有 Foo 和 Bar 这两个词。

基于全文的查询

像 match 或 query_string 这样的查询是高层查询,它们了解字段映射的信息:

如果查询 日期(date) 或 整数(integer) 字段,它们会将查询字符串分别作为日期或整数对待。

如果查询一个( not_analyzed )未分析的精确值字符串字段,它们会将整个查询字符串作为单个词项对待。

但如果要查询一个( analyzed )已分析的全文字段,它们会先将查询字符串传递到一个合适的分析器,然后生成一个供查询的词项列表。

16、bool搜索

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "bool": {
  5.  
    "must": [
  6.  
    {"match": {
  7.  
    "last_name": "Smith"
  8.  
    }}
  9.  
    ],
  10.  
    "filter": [
  11.  
    {"range": {
  12.  
    "age": {
  13.  
    "gte": 30
  14.  
    }
  15.  
    }}
  16.  
    ]
  17.  
    }
  18.  
    }
  19.  
    }
学新通

学新通

说明:

must:   完全匹配条件      相当于sql中的and

should: 至少满足一个条件     相当于sql中的 or

must_not: 文档必须不匹配条件     相当于sql中的!=

17、must多条件匹配查询

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "bool": {
  5.  
    "must": [
  6.  
    {
  7.  
    "match": {
  8.  
    "last_name": "Smith"
  9.  
    }
  10.  
    },
  11.  
    {
  12.  
    "match": {
  13.  
    "age": 32
  14.  
    }
  15.  
    }
  16.  
    ]
  17.  
    }
  18.  
    }
  19.  
    }
学新通

学新通

18、Should满足一个条件查询

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "bool": {
  5.  
    "should": [
  6.  
    {
  7.  
    "match": {
  8.  
    "last_name": "Fir"
  9.  
    }
  10.  
    },
  11.  
    {
  12.  
    "match": {
  13.  
    "age": 32
  14.  
    }
  15.  
    }
  16.  
    ]
  17.  
    }
  18.  
    }
  19.  
    }
学新通

学新通

19、must_not必须不匹配查询

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "bool": {
  5.  
    "must_not": [
  6.  
    {
  7.  
    "match": {
  8.  
    "last_name": "Fir"
  9.  
    }
  10.  
    },
  11.  
    {
  12.  
    "match": {
  13.  
    "age": 32
  14.  
    }
  15.  
    }
  16.  
    ]
  17.  
    }
  18.  
    }
  19.  
    }
学新通

学新通

20、多个字段查询内容

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "multi_match": {
  5.  
    "query": "collect rock",
  6.  
    "fields": ["last_name","about"]
  7.  
    }
  8.  
    }
  9.  
    }

学新通

21、一个字段查询多个内容

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "terms": {
  5.  
    "about": [
  6.  
    "rock",
  7.  
    "hehe"
  8.  
    ]
  9.  
    }
  10.  
    }
  11.  
    }

学新通

22、通配符和正则匹配

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "bool": {
  5.  
    "filter": [
  6.  
    {
  7.  
    "wildcard":{
  8.  
    "last_name":"*mi*"
  9.  
    }
  10.  
    }]
  11.  
    }
  12.  
    }
  13.  
    }

学新通

23、前缀查询

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "prefix": {
  5.  
    "last_name": {
  6.  
    "value": "Smi"
  7.  
    }
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
     

学新通

24、短语匹配

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query" : {
  4.  
    "match_phrase" : {
  5.  
    "about" : "rock climbing"
  6.  
    }
  7.  
    }
  8.  
    }

学新通

25、输入即搜索

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "match_phrase_prefix": {
  5.  
    "about": "I like to collect"
  6.  
    }
  7.  
    }
  8.  
    }

学新通

26、高亮搜索

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query" : {
  4.  
    "match_phrase" : {
  5.  
    "about" : "rock climbing"
  6.  
    }
  7.  
    },
  8.  
    "highlight": {
  9.  
    "fields" : {
  10.  
    "about" : {}
  11.  
    }
  12.  
    }
  13.  
    }

学新通

27、统计

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "aggs": {
  4.  
    "all_result": {
  5.  
    "terms": { "field": "interests" }
  6.  
    }
  7.  
    }
  8.  
    }

学新通

28、根据条件统计

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "match": {
  5.  
    "last_name": "smith"
  6.  
    }
  7.  
    },
  8.  
    "aggs": {
  9.  
    "all_interests": {
  10.  
    "terms": {
  11.  
    "field": "interests"
  12.  
    }
  13.  
    }
  14.  
    }
  15.  
    }
学新通

学新通

29、获取平均数

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "aggs" : {
  4.  
    "all_interests" : {
  5.  
    "terms" : { "field" : "interests" },
  6.  
    "aggs" : {
  7.  
    "avg_age" : {
  8.  
    "avg" : { "field" : "age" }
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
    }
  13.  
    }

学新通

30、多个字段匹配查询

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "multi_match": {
  5.  
    "query": "Smith",
  6.  
    "fields": ["last_name","about"]
  7.  
    }
  8.  
    }
  9.  
    }

学新通

31、范围查询

  1.  
    GET demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "range": {
  5.  
    "age": {
  6.  
    "gte": 30,
  7.  
    "lt": 35
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
    }

学新通

32、排序

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "bool": {
  5.  
    "must": [
  6.  
    {"term": {
  7.  
    "last_name.keyword": {
  8.  
    "value": "Smith"
  9.  
    }
  10.  
    }},
  11.  
    {
  12.  
    "term": {
  13.  
    "about": {
  14.  
    "value": "climbing"
  15.  
    }
  16.  
    }
  17.  
    }
  18.  
    ]
  19.  
    }
  20.  
    },
  21.  
    "sort": [
  22.  
    {
  23.  
    "last_name": {
  24.  
    "order": "desc"
  25.  
    }
  26.  
    }
  27.  
    ]
  28.  
    }
学新通

学新通

33、删除一条数据

DELETE /demo_person/_doc/TC4cJ4ABPbcGgBnacj_w

学新通

34、批量删除

  1.  
    POST /_bulk
  2.  
    {"delete":{"_index":"demo_person","_id":"1"}}
  3.  
    {"delete":{"_index":"demo_person","_id":"12"}}

学新通

35、分页查询

  1.  
    GET /demo_person/_search?from=1&size=10
  2.  
    或者
  3.  
    GET /demo_person/_search
  4.  
    {
  5.  
    "from": 1,
  6.  
    "size": 10
  7.  
    }

学新通

Size:显示应该返回的结果数量,默认是 10

From:显示应该跳过的初始结果数量,默认是 0

36、游标查询

  1.  
    GET /demo_person/_search?scroll=5m
  2.  
    {
  3.  
    "query": {
  4.  
    "match_all": {}
  5.  
    },
  6.  
    "sort": [
  7.  
    {
  8.  
    "_doc": {
  9.  
    "order": "desc"
  10.  
    }
  11.  
    }
  12.  
    ],
  13.  
    "size": 1
  14.  
    }

学新通

然后获取scroll_id继续查询,如下

  1.  
    GET _search/scroll
  2.  
    {
  3.  
    "scroll":"5m",
  4.  
    "scroll_id":"FGluY2x1ZGVfY29udGV4dF91dWlkDnF1ZXJ5VGhlbkZldGNoBRZybzdMSjJ2NVNiV0poWVhwbFEtYmdnAAAAAAEj8FgWV0QzQ0pVQm9URnVBVFpmOGpDSC05QRZybzdMSjJ2NVNiV0poWVhwbFEtYmdnAAAAAAEj8FkWV0QzQ0pVQm9URnVBVFpmOGpDSC05QRZybzdMSjJ2NVNiV0poWVhwbFEtYmdnAAAAAAEj8FoWV0QzQ0pVQm9URnVBVFpmOGpDSC05QRZybzdMSjJ2NVNiV0poWVhwbFEtYmdnAAAAAAEj8FsWV0QzQ0pVQm9URnVBVFpmOGpDSC05QRZybzdMSjJ2NVNiV0poWVhwbFEtYmdnAAAAAAEj8FwWV0QzQ0pVQm9URnVBVFpmOGpDSC05QQ=="
  5.  
    }

学新通

37、字段存在查询

  1.  
    GET /demo_person/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "exists": {"field": "aa"}
  5.  
    }
  6.  
    }

学新通

38、复杂查询demo

Demo1

  1.  
    {
  2.  
    "bool": {
  3.  
    "must": { "match": { "tweet": "elasticsearch" }},
  4.  
    "must_not": { "match": { "name": "mary" }},
  5.  
    "should": { "match": { "tweet": "full text" }},
  6.  
    "filter": { "range": { "age" : { "gt" : 30 }} }
  7.  
    }
  8.  
    }

demo2

  1.  
    {
  2.  
    "bool": {
  3.  
    "must": { "match": { "email": "business opportunity" }},
  4.  
    "should": [
  5.  
    { "match": { "starred": true }},
  6.  
    { "bool": {
  7.  
    "must": { "match": { "folder": "inbox" }},
  8.  
    "must_not": { "match": { "spam": true }}
  9.  
    }}
  10.  
    ],
  11.  
    "minimum_should_match": 1
  12.  
    }
  13.  
    }

                                                            

Demo3

  1.  
    GET /my_store/products/_search
  2.  
    {
  3.  
    "query" : {
  4.  
    "filtered" : {
  5.  
    "filter" : {
  6.  
    "bool" : {
  7.  
    "should" : [
  8.  
    { "term" : {"productID" : "KDKE-B-9947-#kL5"}},
  9.  
    { "bool" : {
  10.  
    "must" : [
  11.  
    { "term" : {"productID" : "JODL-X-1937-#pV7"}},
  12.  
    { "term" : {"price" : 30}}
  13.  
    ]
  14.  
    }}
  15.  
    ]
  16.  
    }
  17.  
    }
  18.  
    }
  19.  
    }
  20.  
    }
学新通

39、拷贝索引

  1.  
    POST _reindex
  2.  
    {
  3.  
    "source": {
  4.  
    "index": "twitter"
  5.  
    },
  6.  
    "dest": {
  7.  
    "index": "new_twitter"
  8.  
    }
  9.  
    }

40、索引别名

PUT /my_index_v1 –创建索引

PUT /my_index_v1/_alias/my_index  --创建my_index_v1别名为my_index

GET /*/_alias/my_index –-查看别名指向哪个索引

GET /my_index_v1/_alias/*  --查看哪些别名指向此索引

41、查询集群健康

GET /_cluster/health

学新通

status 字段指示着当前集群在总体上是否工作正常。它的三种颜色含义如下:

green:所有的主分片和副本分片都正常运行。

Yellow:所有的主分片都正常运行,但不是所有的副本分片都正常运行。

Red:有主分片没能正常运行。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgjijcj
系列文章
更多 icon
同类精品
更多 icon
继续加载