返回介绍

Elasticsearch 查询 DSL

发布于 2024-06-23 18:57:05 字数 28169 浏览 0 评论 0 收藏 0

Elasticsearch中,通过使用基于JSON的查询进行搜索。 查询由两个子句组成 -

  • 叶查询子句 - 这些子句是匹配,项或范围的,它们在特定字段中查找特定值。
  • 复合查询子句 - 这些查询是叶查询子句和其他复合查询的组合,用于提取所需的信息。

Elasticsearch支持大量查询。 查询从查询关键字开始,然后以JSON对象的形式在其中包含条件和过滤器。以下描述了不同类型的查询 -

匹配所有查询

这是最基本的查询; 它返回所有内容,并为每个对象的分数为1.0。 例如,
POST http://localhost:9200/schools*/_search

请求正文

  1. {
  2. "query":{
  3. "match_all":{}
  4. }
  5. }

响应

  1. {
  2. "took":1, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
  3. "hits":{
  4. "total":5, "max_score":1.0, "hits":[
  5. {
  6. "_index":"schools", "_type":"school", "_id":"2", "_score":1.0,
  7. "_source":{
  8. "name":"Saint Paul School", "description":"ICSE Affiliation",
  9. "street":"Dawarka", "city":"Delhi", "state":"Delhi",
  10. "zip":"110075", "location":[28.5733056, 77.0122136], "fees":5000,
  11. "tags":["Good Faculty", "Great Sports"], "rating":"4.5"
  12. }
  13. },
  14. {
  15. "_index":"schools_gov", "_type":"school", "_id":"2", "_score":1.0,
  16. "_source":{
  17. "name":"Government School", "description":"State Board Affiliation",
  18. "street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
  19. "location":[18.599752, 73.6821995], "fees":500, "tags":["Great Sports"],
  20. "rating":"4"
  21. }
  22. },
  23. {
  24. "_index":"schools", "_type":"school", "_id":"1", "_score":1.0,
  25. "_source":{
  26. "name":"Central School", "description":"CBSE Affiliation",
  27. "street":"Nagan", "city":"paprola", "state":"HP",
  28. "zip":"176115", "location":[31.8955385, 76.8380405],
  29. "fees":2200, "tags":["Senior Secondary", "beautiful campus"],
  30. "rating":"3.3"
  31. }
  32. },
  33. {
  34. "_index":"schools_gov", "_type":"school", "_id":"1", "_score":1.0,
  35. "_source":{
  36. "name":"Model School", "description":"CBSE Affiliation",
  37. "street":"silk city", "city":"Hyderabad", "state":"AP",
  38. "zip":"500030", "location":[17.3903703, 78.4752129], "fees":700,
  39. "tags":["Senior Secondary", "beautiful campus"], "rating":"3"
  40. }
  41. },
  42. {
  43. "_index":"schools", "_type":"school", "_id":"3", "_score":1.0,
  44. "_source":{
  45. "name":"Crescent School", "description":"State Board Affiliation",
  46. "street":"Tonk Road", "city":"Jaipur", "state":"RJ", "zip":"176114",
  47. "location":[26.8535922, 75.7923988], "fees":2500,
  48. "tags":["Well equipped labs"], "rating":"4.5"
  49. }
  50. }
  51. ]
  52. }
  53. }

全文查询

这些查询用于搜索整个文本,如章节或新闻文章。 此查询根据与特定索引或文档相关联的分析器一起工作。 在本节中,我们将讨论不同类型的全文查询。

匹配查询

此查询将文本或短语与一个或多个字段的值匹配。 例如,
POST http://localhost:9200/schools*/_search

请求正文

  1. {
  2. "query":{
  3. "match" : {
  4. "city":"pune"
  5. }
  6. }
  7. }

响应

  1. {
  2. "took":1, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
  3. "hits":{
  4. "total":1, "max_score":0.30685282, "hits":[{
  5. "_index":"schools_gov", "_type":"school", "_id":"2", "_score":0.30685282,
  6. "_source":{
  7. "name":"Government School", "description":"State Board Afiliation",
  8. "street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
  9. "location":[18.599752, 73.6821995], "fees":500,
  10. "tags":["Great Sports"], "rating":"4"
  11. }
  12. }]
  13. }
  14. }

multi_match查询

此查询将文本或短语与多个字段匹配。 例如,
POST http://localhost:9200/schools*/_search

请求正文

  1. {
  2. "query":{
  3. "multi_match" : {
  4. "query": "hyderabad",
  5. "fields": [ "city", "state" ]
  6. }
  7. }
  8. }

响应

  1. {
  2. "took":16, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
  3. "hits":{
  4. "total":1, "max_score":0.09415865, "hits":[{
  5. "_index":"schools_gov", "_type":"school", "_id":"1", "_score":0.09415865,
  6. "_source":{
  7. "name":"Model School", " description":"CBSE Affiliation",
  8. "street":"silk city", "city":"Hyderabad", "state":"AP",
  9. "zip":"500030", "location":[17.3903703, 78.4752129], "fees":700,
  10. "tags":["Senior Secondary", "beautiful campus"], "rating":"3"
  11. }
  12. }]
  13. }
  14. }

查询字符串查询

此查询使用查询解析器和query_string关键字。 例如,
POST http://localhost:9200/schools/_search

请求正文

  1. {
  2. "query":{
  3. "query_string":{
  4. "query":"good faculty"
  5. }
  6. }
  7. }

响应

  1. {
  2. "took":16, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
  3. "hits":{
  4. "total":1, "max_score":0.09492774, "hits":[{
  5. "_index":"schools", "_type":"school", "_id":"2", "_score":0.09492774,
  6. "_source":{
  7. "name":"Saint Paul School", "description":"ICSE Affiliation",
  8. "street":"Dawarka", "city":"Delhi", "state":"Delhi",
  9. "zip":"110075", "location":[28.5733056, 77.0122136],
  10. "fees":5000, "tags":["Good Faculty", "Great Sports"],
  11. "rating":"4.5"
  12. }
  13. }]
  14. }
  15. }

期限等级查询

这些查询主要处理结构化数据,如数字,日期和枚举。 例如,
POST http://localhost:9200/schools/_search

请求正文

  1. {
  2. "query":{
  3. "term":{"zip":"176115"}
  4. }
  5. }

响应

  1. {
  2. "took":1, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
  3. "hits":{
  4. "total":1, "max_score":0.30685282, "hits":[{
  5. "_index":"schools", "_type":"school", "_id":"1", "_score":0.30685282,
  6. "_source":{
  7. "name":"Central School", "description":"CBSE Affiliation",
  8. "street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
  9. "location":[31.8955385, 76.8380405], "fees":2200,
  10. "tags":["Senior Secondary", "beautiful campus"], "rating":"3.3"
  11. }
  12. }]
  13. }
  14. }

范围查询

此查询用于查找值的范围之间的值的对象。 为此,需要使用类似 -

  • gte − 大于和等于
  • gt − 大于
  • lte − 小于和等于
  • lt − 小于
    例如 -
    POST http://localhost:9200/schools*/_search

请求正文

  1. {
  2. "query":{
  3. "range":{
  4. "rating":{
  5. "gte":3.5
  6. }
  7. }
  8. }
  9. }

响应

  1. {
  2. "took":31, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
  3. "hits":{
  4. "total":3, "max_score":1.0, "hits":[
  5. {
  6. "_index":"schools", "_type":"school", "_id":"2", "_score":1.0,
  7. "_source":{
  8. "name":"Saint Paul School", "description":"ICSE Affiliation",
  9. "street":"Dawarka", "city":"Delhi", "state":"Delhi",
  10. "zip":"110075", "location":[28.5733056, 77.0122136], "fees":5000,
  11. "tags":["Good Faculty", "Great Sports"], "rating":"4.5"
  12. }
  13. },
  14. {
  15. "_index":"schools_gov", "_type":"school", "_id":"2", "_score":1.0,
  16. "_source":{
  17. "name":"Government School", "description":"State Board Affiliation",
  18. "street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
  19. "location":[18.599752, 73.6821995] "fees":500,
  20. "tags":["Great Sports"], "rating":"4"
  21. }
  22. },
  23. {
  24. "_index":"schools", "_type":"school", "_id":"3", "_score":1.0,
  25. "_source":{
  26. "name":"Crescent School", "description":"State Board Affiliation",
  27. "street":"Tonk Road", "city":"Jaipur", "state":"RJ", "zip":"176114",
  28. "location":[26.8535922, 75.7923988], "fees":2500,
  29. "tags":["Well equipped labs"], "rating":"4.5"
  30. }
  31. }
  32. ]
  33. }
  34. }

其他类型的期限级查询是 -

  • 存在的查询 - 如果某一个字段有非空值。
  • 缺失的查询 - 这与存在查询完全相反,此查询搜索没有特定字段的对象或有空值的字段。
  • 通配符或正则表达式查询 - 此查询使用正则表达式来查找对象中的模式。

类型查询 - 具有特定类型的文档。 例如,
POST http://localhost:9200/schools*/_search

请求正文

  1. {
  2. "query":{
  3. "type" : {
  4. "value" : "school"
  5. }
  6. }
  7. }

响应
存在于指定的索引中的所有学校的JSON对象。

复合查询

这些查询是通过使用如和,或,非和或等,用于不同索引或具有函数调用等的布尔运算符彼此合并的不同查询的集合。例如,
POST http://localhost:9200/schools*/_search

请求正文

  1. {
  2. "query":{
  3. "filtered":{
  4. "query":{
  5. "match":{
  6. "state":"UP"
  7. }
  8. },
  9. "filter":{
  10. "range":{
  11. "rating":{
  12. "gte":4.0
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

响应

  1. {
  2. "took":16, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
  3. "hits":{"total":0, "max_score":null, "hits":[]}
  4. }

连接查询

这些查询用于包含多个映射或文档的位置。 连接查询有两种类型 -
嵌套查询
这些查询处理嵌套映射(将在下一章阅读了解更多内容)。
has_child和has_parent查询
这些查询用于检索在查询中匹配的文档的子文档或父文档。 例如,
POST http://localhost:9200/tutorials/_search

请求正文

  1. {
  2. "query":
  3. {
  4. "has_child" : {
  5. "type" : "article", "query" : {
  6. "match" : {
  7. "Text" : "This is article 1 of chapter 1"
  8. }
  9. }
  10. }
  11. }
  12. }

响应

  1. {
  2. "took":21, "timed_out":false, "_shards":{"total":5, "successful":5, "failed":0},
  3. "hits":{
  4. "total":1, "max_score":1.0, "hits":[{
  5. "_index":"tutorials", "_type":"chapter", "_id":"1", "_score":1.0,
  6. "_source":{
  7. "Text":"this is chapter one"
  8. }
  9. }]
  10. }
  11. }

地理查询

这些查询处理地理位置和地理点。这些查询有助于查找任何位置附近的学校或任何其他地理对象。需要使用地理位置数据类型。 例如,
POST http://localhost:9200/schools*/_search

请求正文

  1. {
  2. "query":{
  3. "filtered":{
  4. "filter":{
  5. "geo_distance":{
  6. "distance":"100km",
  7. "location":[32.052098, 76.649294]
  8. }
  9. }
  10. }
  11. }
  12. }

响应

  1. {
  2. "took":6, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
  3. "hits":{
  4. "total":2, "max_score":1.0, "hits":[
  5. {
  6. "_index":"schools", "_type":"school", "_id":"2", "_score":1.0,
  7. "_source":{
  8. "name":"Saint Paul School", "description":"ICSE Affiliation",
  9. "street":"Dawarka", "city":"Delhi", "state":"Delhi", "zip":"110075",
  10. "location":[28.5733056, 77.0122136], "fees":5000,
  11. "tags":["Good Faculty", "Great Sports"], "rating":"4.5"
  12. }
  13. },
  14. {
  15. "_index":"schools", "_type":"school", "_id":"1", "_score":1.0,
  16. "_source":{
  17. "name":"Central School", "description":"CBSE Affiliation",
  18. "street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
  19. "location":[31.8955385, 76.8380405], "fees":2000,
  20. "tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"
  21. }
  22. }
  23. ]
  24. }
  25. }

注意 - 如果在执行上述示例时遇到异常,请将以下映射添加到索引。

  1. {
  2. "mappings":{
  3. "school":{
  4. "_all":{
  5. "enabled":true
  6. },
  7. "properties":{
  8. "location":{
  9. "type":"geo_point"
  10. }
  11. }
  12. }
  13. }
  14. }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文