高级查询

查询结果中的分值 score,表示匹配度。

1. 查询对象

query 单个属性的查询

  1. GET /users/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "engure"
  6. }
  7. }
  8. }

04-查询(重点) - 图1
image.png
可以看到这里是 等值匹配的。

2. 只查询指定的字段

_source

  1. GET /users/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "engure"
  6. }
  7. },
  8. "_source": ["name","birth"]
  9. }

04-查询(重点) - 图3image.png

3. 排序

sort

  1. # 不使用查询语句,查询所有文档。按年龄倒序排序
  2. GET /users/_search
  3. {
  4. "sort": [
  5. {
  6. "age": {
  7. "order": "desc"
  8. }
  9. }
  10. ]
  11. }

image.png

多个排序条件呢?

  1. GET /users/_search
  2. {
  3. "sort": [
  4. {
  5. "age": {
  6. "order": "desc"
  7. },
  8. "birth": {
  9. "order": "asc"
  10. }
  11. }
  12. ]
  13. }

经测试,符合预期。

4. 分页

分页条件:fromsize

  1. GET /users/_search
  2. {
  3. "sort": [
  4. {
  5. "age": {
  6. "order": "desc"
  7. }
  8. }
  9. ],
  10. "from": 0,
  11. "size": 2
  12. }

注意:from 代表的索引从 0 开始
image.png
04-查询(重点) - 图7

5. 多条件查询(bool 查询)

must 查询,相当于 and,所有条件都满足

  1. ################### name=engure && age=19
  2. GET /users/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must": [
  7. {
  8. "match": {
  9. "name": "engure"
  10. }
  11. },
  12. {
  13. "match": {
  14. "age": "19"
  15. }
  16. }
  17. ]
  18. }
  19. }
  20. }

should 查询,相当于 or,满足其中一个条件即可

  1. ################### name=engure || age=19
  2. GET /users/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "should": [
  7. {
  8. "match": {
  9. "name": "engure"
  10. }
  11. },
  12. {
  13. "match": {
  14. "age": "19"
  15. }
  16. }
  17. ]
  18. }
  19. }
  20. }

must not 查询,相当于 not

  1. ################# age != 18
  2. GET /users/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must_not": [
  7. {
  8. "match": {
  9. "age": "18"
  10. }
  11. }
  12. ]
  13. }
  14. }
  15. }

稍微复杂一点,bool嵌套

  1. ############ age = 18 && name != engure
  2. GET /users/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must": [
  7. {
  8. "match": {
  9. "age": "18"
  10. }
  11. },
  12. {
  13. "bool": {
  14. "must_not": [
  15. {
  16. "match": {
  17. "name": "engure"
  18. }
  19. }
  20. ]
  21. }
  22. }
  23. ]
  24. }
  25. }
  26. }

image.png

6. 过滤器,范围过滤

  1. # bool ---> age != 18
  2. # filter -> age < 18
  3. GET /users/_search
  4. {
  5. "query": {
  6. "bool": {
  7. "must_not": [
  8. {
  9. "match": {
  10. "age": "18"
  11. }
  12. }
  13. ],
  14. "filter": [
  15. {
  16. "range": {
  17. "age": {
  18. "lt": 18
  19. }
  20. }
  21. }
  22. ]
  23. }
  24. }
  25. }

image.png

  • gt >
  • gte >=
  • lt <
  • le <=
  • eq ==
  1. # bool ----> age != 18
  2. # filter --> 10 < age < age

04-查询(重点) - 图10image.png

7. 匹配多个条件

查询多个条件,直接用空格隔开

  1. GET /users/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "engure Tom"
  6. }
  7. }
  8. }

04-查询(重点) - 图12image.png

8. 精确查询

  • term 利用倒排索引实现精确查询。text 类型会被分词器解析,keyword 类型不会分词器解析。
  • match 会使用分词器。先分析文档,再查询。

1) text 与 keyword (重要!)

使用 keyword 分词器拆分: 不会被拆分,而是将输入作为整体匹配查询。

image.png
04-查询(重点) - 图15image.png

使用 standard 分词器进行拆分: 模拟拆分 text 类型数据,会被拆分。

04-查询(重点) - 图17image.png
image.png
04-查询(重点) - 图20
已知文档的 name 字段是 text 类型,对其进行查询,该属性值会被分析器分析,然后进行匹配。

image.png

注:比如中文的“苹果”会被分析(text 类型)为“苹”和“果”,会匹配到“果”。英文的匹配是以单词为单位的,比如“engureguo”,不会被进一步拆分。

如果这里的 name 是 keyword 类型,那末将会整体匹配 name=“果”。

2) 验证

  1. ########## 创建索引,指定文档各属性类型
  2. PUT testidx
  3. {
  4. "mappings": {
  5. "properties": {
  6. "rt": {
  7. "type": "text"
  8. },
  9. "rk": {
  10. "type": "keyword"
  11. }
  12. }
  13. }
  14. }
  15. ################# 添加文档
  16. PUT testidx/_doc/1
  17. {
  18. "rt": "Come on dude1 - text",
  19. "rk": "Come on dude1 - keyword"
  20. }
  21. PUT testidx/_doc/2
  22. {
  23. "rt": "Come on dude2 - text",
  24. "rk": "Come on dude2 - keyword"
  25. }
  26. ############## 查询
  27. GET testidx/_search
  28. {
  29. "query": {
  30. "match": {
  31. "rt": "on"
  32. }
  33. }
  34. }

text 类型的字段的匹配,查询出来所有 name 包含 on 的文档,说明已有文档的 name 字段经过了分析。
image.png
04-查询(重点) - 图23

查询另一个字段(rk,keyword 类型),结果为空:
04-查询(重点) - 图24image.png

更换一个已知的属性值,得到一条结果,是整体匹配。

image.png

结论:keyword 字段类型不会被分词器解析。

3) 使用 term 精确匹配多个值

查询结果高亮

  1. # 查询内容高亮显示
  2. GET users/_search
  3. {
  4. "query": {
  5. "match": {
  6. "name": "果"
  7. }
  8. },
  9. "highlight": {
  10. "fields": {
  11. "name": {}
  12. }
  13. }
  14. }

04-查询(重点) - 图27image.png

如何定制样式?使用提供的属性

  1. GET users/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "果"
  6. }
  7. },
  8. "highlight": {
  9. "pre_tags": "<span class='hlword' style='color:red;'>",
  10. "post_tags": "</span>",
  11. "fields": {
  12. "name": {}
  13. }
  14. }
  15. }

04-查询(重点) - 图29