当前位置:   article > 正文

Elasticsearch 查询规则现已正式发布 - query rules

Elasticsearch 查询规则现已正式发布 - query rules

作者:来自 Elastic Kathleen DeRusso

查询规则(query rules)允许使用细粒度、上下文特定的解决方案来更改特定查询或搜索用例的搜索结果。这对于需要将品牌或赞助结果固定在特定关键字的搜索结果列表顶部的广告系列很有帮助,但对于只需要 “修复” 顶部结果的热门查询也很有帮助。

简介

我们很高兴地宣布,查询规则已在我们的 serverless 产品中正式发布,并且将从堆栈版本 8.15.0 开始普遍可用。查询规则最初在 8.10.0 中作为技术预览功能引入,并允许索引维护者根据上下文查询输入的条件整理特定文档以将其固定在结果顶部。

查询规则如何工作?

查询规则是你根据特定查询元数据定义的规则。首先,你需要定义一个查询规则集,该规则集会识别要针对查询中发送的某些元数据进行推广的文档。然后在搜索时,你将该元数据与规则查询一起发送。如果规则查询中发送的元数据与任何规则匹配,则这些规则将应用于你的结果集。

新的查询规则功能

我们在向正式版迈进的过程中为查询规则添加了一些功能。

这些变化的简要概述:

  • 我们将规则查询从 rule_query 重命名为rule,以与我们的其他API调用更加一致
  • 我们现在支持在单个规则查询中指定多个规则集
  • 我们扩展了 query ruleset management APIs 以支持管理单个查询规则

这是什么样子的?

假设我们有一个包含狗品种信息的索引,其中包含两个字段:dog_breed 和 advert。我们想要设置规则,将品种和混合品种固定在同一个规则中。这是一个包含两个规则的示例规则集:

  1. PUT _query_rules/dog-breed-ruleset
  2. {
  3. "ruleset_id": "dog-breed-ruleset",
  4. "rules": [
  5. {
  6. "rule_id": "pug-mixes",
  7. "type": "pinned",
  8. "criteria": [
  9. {
  10. "type": "exact",
  11. "metadata": "breed",
  12. "values": [
  13. "pug"
  14. ]
  15. }
  16. ],
  17. "actions": {
  18. "ids": [
  19. "pug",
  20. "puggle",
  21. "chug",
  22. "pugshire"
  23. ]
  24. },
  25. "priority": 5
  26. },
  27. {
  28. "rule_id": "chi-mixes",
  29. "type": "pinned",
  30. "criteria": [
  31. {
  32. "type": "exact",
  33. "metadata": "breed",
  34. "values": [
  35. "chihauhua"
  36. ]
  37. }
  38. ],
  39. "actions": {
  40. "ids": [
  41. "chihauhua",
  42. "chiweenie",
  43. "chug"
  44. ]
  45. },
  46. "priority": 10
  47. }
  48. ]
  49. }

解析此规则集的作用:

  1. 如果规则查询发送品种:pug,则按顺序显示的第一个结果将是:pug、puggle、chug 和 pugshire。任何自然结果都将在这些固定结果之后返回。
  2. 如果规则查询发送品种:chihuahua,则按顺序显示的第一个结果将是:chihuahua、chiweenie 和 chug。任何自然结果都将在这些固定结果之后返回。

需要注意的一点是规则集中每个规则的优先级。这是每个规则的可选部分,但它可以帮助你定义单个规则插入规则集的位置。规则集按优先级升序排序,但正如你在此示例中所见,它们不必连续。将优先级为 4 或更低的新规则插入此规则集会将新规则插入规则集的开头,而优先级介于 6 和 9 之间会将新规则插入规则集中间两个现有规则之间。如果将单个规则添加到规则集中但不包含优先级,则它将被简单地附加到规则集的末尾。

查询规则的另一个有用功能是我们现在支持将多个规则集传递到规则查询中。这允许你组织和定义更多规则;以前,你只能在单个规则集中容纳有限的规则数量(默认情况下为 100 条,或可配置为每个规则集最多 1000 条)。

让我们通过专门为 7 月促销活动创建一个新规则集来看看它是什么样子。此规则的类型为 “always”,因此无论如何它都会始终返回:

  1. PUT _query_rules/promo-ruleset
  2. {
  3. "rules": [
  4. {
  5. "rule_id": "july-promo",
  6. "type": "pinned",
  7. "criteria": [
  8. {
  9. "type": "always"
  10. }
  11. ],
  12. "actions": {
  13. "ids": [
  14. "july-promotion"
  15. ]
  16. }
  17. }
  18. ]
  19. }

现在,我们可以使用以下查询一起查询这些规则集:

  1. GET query-rules-test/_search
  2. {
  3. "query": {
  4. "rule": {
  5. "organic": {
  6. "query_string": {
  7. "query": "chihauhua mixes"
  8. }
  9. },
  10. "ruleset_ids": [
  11. "promo-ruleset",
  12. "dog-breed-ruleset"
  13. ],
  14. "match_criteria": {
  15. "breed": "chihauhua"
  16. }
  17. }
  18. }
  19. }

由于指定了两个规则集,我们将按照请求中指定的顺序处理每个规则集。这意味着七月促销规则将始终作为第一个结果返回,匹配的狗品种将随后被置顶,结果如下所示:

  1. "hits": [
  2. {
  3. "_index": "query-rules-test",
  4. "_id": "july-promotion",
  5. "_score": 1.7014128e+38,
  6. "_source": {
  7. "advert": "EVERYTHING ON SALE!"
  8. }
  9. },
  10. {
  11. "_index": "query-rules-test",
  12. "_id": "chihauhua",
  13. "_score": 1.7014126e+38,
  14. "_source": {
  15. "dog_breed": "chihauhua"
  16. }
  17. },
  18. {
  19. "_index": "query-rules-test",
  20. "_id": "chiweenie",
  21. "_score": 1.7014124e+38,
  22. "_source": {
  23. "dog_breed": "chiweenie"
  24. }
  25. },
  26. {
  27. "_index": "query-rules-test",
  28. "_id": "chug",
  29. "_score": 1.7014122e+38,
  30. "_source": {
  31. "dog_breed": "chug"
  32. }
  33. },
  34. ... // organic results follow...
  35. ]

这些都是简单的例子,那么一些更复杂的例子呢?

我们可以提供帮助!
​​
让我们继续我们的狗主题,但扩展测试索引中的映射:

  1. PUT query-rules-test
  2. {
  3. "mappings": {
  4. "properties": {
  5. "breed": {
  6. "type": "keyword"
  7. },
  8. "age": {
  9. "type": "integer"
  10. },
  11. "sex": {
  12. "type": "keyword"
  13. },
  14. "name": {
  15. "type": "keyword"
  16. },
  17. "bio": {
  18. "type": "text"
  19. },
  20. "good_with_animals": {
  21. "type": "boolean"
  22. },
  23. "good_with_kids": {
  24. "type": "boolean"
  25. }
  26. }
  27. }
  28. }

我们可以将以下 json 索引到此索引中以获取一些示例数据。为了保持一致性,我们假设 _id 字段与 json id 字段匹配:

  1. [
  2. {"id":"buddy_pug","breed":"pug","sex":"Male","age":3,"name":"Buddy","bio":"Buddy is a charming pug who loves to play and snuggle. He is looking for a loving home.","good_with_animals":true,"good_with_kids":true},
  3. {"id":"lucy_beagle","breed":"beagle","sex":"Female","age":7,"name":"Lucy","bio":"Lucy is a friendly beagle who enjoys long walks and is very affectionate.","good_with_animals":true,"good_with_kids":true},
  4. {"id":"rocky_chihuahua","breed":"chihuahua","sex":"Male","age":2,"name":"Rocky","bio":"Rocky is a tiny chihuahua with a big personality. He is very playful and loves attention.","good_with_animals":false,"good_with_kids":false},
  5. {"id":"zoe_dachshund","breed":"dachshund","sex":"Female","age":5,"name":"Zoe","bio":"Zoe is a sweet dachshund who loves to burrow under blankets and is very loyal.","good_with_animals":true,"good_with_kids":true},
  6. {"id":"max_poodle","breed":"poodle","sex":"Male","age":6,"name":"Max","bio":"Max is a smart and active poodle who loves learning new tricks and is very friendly.","good_with_animals":true,"good_with_kids":true},
  7. {"id":"bella_yorkie","breed":"yorkie","sex":"Female","age":4,"name":"Bella","bio":"Bella is a cute yorkie who loves to be pampered and is very affectionate.","good_with_animals":true,"good_with_kids":true},
  8. {"id":"jack_puggle","breed":"puggle","sex":"Male","age":8,"name":"Jack","bio":"Jack is a friendly puggle who loves to play and is very social.","good_with_animals":true,"good_with_kids":true},
  9. {"id":"lola_chiweenie","breed":"chiweenie","sex":"Female","age":9,"name":"Lola","bio":"Lola is a playful chiweenie who loves to chase toys and is very affectionate.","good_with_animals":true,"good_with_kids":true},
  10. {"id":"charlie_chug","breed":"chug","sex":"Male","age":3,"name":"Charlie","bio":"Charlie is an energetic chug who loves to play and is very curious.","good_with_animals":false,"good_with_kids":true},
  11. {"id":"daisy_pugshire","breed":"pugshire","sex":"Female","age":7,"name":"Daisy","bio":"Daisy is a gentle pugshire who loves to cuddle and is very sweet.","good_with_animals":true,"good_with_kids":true},
  12. {"id":"buster_pug","breed":"pug","sex":"Male","age":10,"name":"Buster","bio":"Buster is a calm pug who loves to lounge around and is very loyal.","good_with_animals":true,"good_with_kids":true},
  13. {"id":"molly_beagle","breed":"beagle","sex":"Female","age":12,"name":"Molly","bio":"Molly is a sweet beagle who loves to sniff around and is very friendly.","good_with_animals":true,"good_with_kids":true},
  14. {"id":"toby_chihuahua","breed":"chihuahua","sex":"Male","age":4,"name":"Toby","bio":"Toby is a lively chihuahua who loves to run around and is very playful.","good_with_animals":false,"good_with_kids":false},
  15. {"id":"luna_dachshund","breed":"dachshund","sex":"Female","age":1,"name":"Luna","bio":"Luna is a young dachshund who loves to explore and is very curious.","good_with_animals":true,"good_with_kids":true},
  16. {"id":"oliver_poodle","breed":"poodle","sex":"Male","age":17,"name":"Oliver","bio":"Oliver is a wise poodle who loves to relax and is very gentle.","good_with_animals":true,"good_with_kids":true}]

我们使用如下的方法进行写入:

  1. PUT _bulk
  2. {"index":{"_index":"query-rules-test","_id":"buddy_pug"}}
  3. {"id":"buddy_pug","breed":"pug","sex":"Male","age":3,"name":"Buddy","bio":"Buddy is a charming pug who loves to play and snuggle. He is looking for a loving home.","good_with_animals":true,"good_with_kids":true}
  4. {"index":{"_index":"query-rules-test","_id":"lucy_beagle"}}
  5. {"id":"lucy_beagle","breed":"beagle","sex":"Female","age":7,"name":"Lucy","bio":"Lucy is a friendly beagle who enjoys long walks and is very affectionate.","good_with_animals":true,"good_with_kids":true}
  6. {"index":{"_index":"query-rules-test","_id":"rocky_chihuahua"}}
  7. {"id":"rocky_chihuahua","breed":"chihuahua","sex":"Male","age":2,"name":"Rocky","bio":"Rocky is a tiny chihuahua with a big personality. He is very playful and loves attention.","good_with_animals":false,"good_with_kids":false}
  8. {"index":{"_index":"query-rules-test","_id":"zoe_dachshund"}}
  9. {"id":"zoe_dachshund","breed":"dachshund","sex":"Female","age":5,"name":"Zoe","bio":"Zoe is a sweet dachshund who loves to burrow under blankets and is very loyal.","good_with_animals":true,"good_with_kids":true}
  10. {"index":{"_index":"query-rules-test","_id":"max_poodle"}}
  11. {"id":"max_poodle","breed":"poodle","sex":"Male","age":6,"name":"Max","bio":"Max is a smart and active poodle who loves learning new tricks and is very friendly.","good_with_animals":true,"good_with_kids":true}
  12. {"index":{"_index":"query-rules-test","_id":"bella_yorkie"}}
  13. {"id":"bella_yorkie","breed":"yorkie","sex":"Female","age":4,"name":"Bella","bio":"Bella is a cute yorkie who loves to be pampered and is very affectionate.","good_with_animals":true,"good_with_kids":true}
  14. {"index":{"_index":"query-rules-test","_id":"jack_puggle"}}
  15. {"id":"jack_puggle","breed":"puggle","sex":"Male","age":8,"name":"Jack","bio":"Jack is a friendly puggle who loves to play and is very social.","good_with_animals":true,"good_with_kids":true}
  16. {"index":{"_index":"query-rules-test","_id":"lola_chiweenie"}}
  17. {"id":"lola_chiweenie","breed":"chiweenie","sex":"Female","age":9,"name":"Lola","bio":"Lola is a playful chiweenie who loves to chase toys and is very affectionate.","good_with_animals":true,"good_with_kids":true}
  18. {"index":{"_index":"query-rules-test","_id":"charlie_chug"}}
  19. {"id":"charlie_chug","breed":"chug","sex":"Male","age":3,"name":"Charlie","bio":"Charlie is an energetic chug who loves to play and is very curious.","good_with_animals":false,"good_with_kids":true}
  20. {"index":{"_index":"query-rules-test","_id":"daisy_pugshire"}}
  21. {"id":"daisy_pugshire","breed":"pugshire","sex":"Female","age":7,"name":"Daisy","bio":"Daisy is a gentle pugshire who loves to cuddle and is very sweet.","good_with_animals":true,"good_with_kids":true}
  22. {"index":{"_index":"query-rules-test","_id":"buster_pug"}}
  23. {"id":"buster_pug","breed":"pug","sex":"Male","age":10,"name":"Buster","bio":"Buster is a calm pug who loves to lounge around and is very loyal.","good_with_animals":true,"good_with_kids":true}
  24. {"index":{"_index":"query-rules-test","_id":"molly_beagle"}}
  25. {"id":"molly_beagle","breed":"beagle","sex":"Female","age":12,"name":"Molly","bio":"Molly is a sweet beagle who loves to sniff around and is very friendly.","good_with_animals":true,"good_with_kids":true}
  26. {"index":{"_index":"query-rules-test","_id":"toby_chihuahua"}}
  27. {"id":"toby_chihuahua","breed":"chihuahua","sex":"Male","age":4,"name":"Toby","bio":"Toby is a lively chihuahua who loves to run around and is very playful.","good_with_animals":false,"good_with_kids":false}
  28. {"index":{"_index":"query-rules-test","_id":"luna_dachshund"}}
  29. {"id":"luna_dachshund","breed":"dachshund","sex":"Female","age":1,"name":"Luna","bio":"Luna is a young dachshund who loves to explore and is very curious.","good_with_animals":true,"good_with_kids":true}
  30. {"index":{"_index":"query-rules-test","_id":"oliver_poodle"}}
  31. {"id":"oliver_poodle","breed":"poodle","sex":"Male","age":17,"name":"Oliver","bio":"Oliver is a wise poodle who loves to relax and is very gentle.","good_with_animals":true,"good_with_kids":true}

接下来,让我们创建一个规则集:

  1. PUT _query_rules/rescue-dog-search-ruleset
  2. {
  3. "rules": [
  4. {
  5. "rule_id": "pugs_and_pug_mixes",
  6. "type": "pinned",
  7. "criteria": [
  8. {
  9. "type": "contains",
  10. "metadata": "query_string",
  11. "values": [
  12. "pug"
  13. ]
  14. }
  15. ],
  16. "actions": {
  17. "ids": [
  18. "buddy_pug",
  19. "buster_pug",
  20. "jack_puggle",
  21. "daisy_pugshire"
  22. ]
  23. }
  24. },
  25. {
  26. "rule_id": "puppies",
  27. "type": "pinned",
  28. "criteria": [
  29. {
  30. "type": "contains",
  31. "metadata": "query_string",
  32. "values": [
  33. "puppy",
  34. "puppies"
  35. ]
  36. }
  37. ],
  38. "actions": {
  39. "ids": [
  40. "luna_dachsund"
  41. ]
  42. }
  43. },
  44. {
  45. "rule_id": "puppies2",
  46. "type": "pinned",
  47. "criteria": [
  48. {
  49. "type": "lte",
  50. "metadata": "preferred_age",
  51. "values": [
  52. 2
  53. ]
  54. }
  55. ],
  56. "actions": {
  57. "ids": [
  58. "luna_dachshund"
  59. ]
  60. }
  61. },
  62. {
  63. "rule_id": "adult",
  64. "type": "pinned",
  65. "criteria": [
  66. {
  67. "type": "gt",
  68. "metadata": "preferred_age",
  69. "values": [
  70. 2
  71. ]
  72. },
  73. {
  74. "type": "lt",
  75. "metadata": "preferred_age",
  76. "values": [
  77. 10
  78. ]
  79. }
  80. ],
  81. "actions": {
  82. "ids": [
  83. "lucy_beagle"
  84. ]
  85. }
  86. },
  87. {
  88. "rule_id": "special_needs",
  89. "type": "pinned",
  90. "criteria": [
  91. {
  92. "type": "exact",
  93. "metadata": "work_from_home",
  94. "values": [
  95. "true"
  96. ]
  97. },
  98. {
  99. "type": "exact",
  100. "metadata": "fenced_in_yard",
  101. "values": [
  102. "true"
  103. ]
  104. },
  105. {
  106. "type": "exact",
  107. "metadata": "kids_in_house",
  108. "values": [
  109. "false"
  110. ]
  111. },
  112. {
  113. "type": "exact",
  114. "metadata": "cats_in_house",
  115. "values": [
  116. "false"
  117. ]
  118. }
  119. ],
  120. "actions": {
  121. "ids": [
  122. "toby_chihuahua"
  123. ]
  124. }
  125. }
  126. ]
  127. }

解析此规则集:

  • 有一个 pugs_and_pug_mixes 规则,如果查询字符串包含单词 “pug”,它将固定所有哈巴狗(pugs)和哈巴狗混种(pug mixes)
  • 如果查询字符串包含 “puppy” 或 “puppies”,则 puppies 规则将返回索引中最年轻的狗 luna_dachshund
  • 如果首选年龄小于或等于 2 岁,另一个 puppies2 规则将返回同一只狗(也即 luna_dachshund )
  • 如果首选年龄在 2 到 10 岁之间,adult 规则将固定特定的比格犬  lucy_beagle
  • 我们有一个复杂的 special_needs 规则,只有当准主人在家工作(work_from_home 为 true)、院子有围栏(fenced_in_yard 为 true)并且家里没有孩子(kids_in_house 为 false )并且猫在家里(cats_in_house 为 false),该规则才会生效。

让我们看一些示例查询 - 这些示例使用 match_none 作为有机查询,因此返回的结果仅针对规则本身。

  1. GET query-rules-test/_search
  2. {
  3. "query": {
  4. "rule": {
  5. "organic": {
  6. "match_none": {}
  7. },
  8. "ruleset_ids": [
  9. "rescue-dog-search-ruleset"
  10. ],
  11. "match_criteria": {
  12. "query_string": "I like pugs and pug mixes",
  13. "preferred_age": 5
  14. }
  15. }
  16. }
  17. }

上述查询将匹配 “pug and pug mixes”规则并返回 4 条哈巴狗结果。但是,由于首选年龄为 5 岁,我们将返回成年狗 Lucy 小猎犬作为第 5 个置顶结果。

将首选年龄更改为 1 会将哈巴狗结果下方的第 5 个结果替换为 dachshund 幼犬:

  1. GET query-rules-test/_search
  2. {
  3. "query": {
  4. "rule": {
  5. "organic": {
  6. "match_none": {}
  7. },
  8. "ruleset_ids": [
  9. "rescue-dog-search-ruleset"
  10. ],
  11. "match_criteria": {
  12. "query_string": "I like pugs and pug mixes",
  13. "preferred_age": 1
  14. }
  15. }
  16. }
  17. }

为了符合特殊需求的结果,所有标准必须符合:

  1. {
  2. "query": {
  3. "rule": {
  4. "organic": {
  5. "match_none": {}
  6. },
  7. "ruleset_ids": [
  8. "rescue-dog-search-ruleset"
  9. ],
  10. "match_criteria": {
  11. "work_from_home": "true",
  12. "fenced_in_yard": "true",
  13. "kids_in_house": "false",
  14. "cats_in_house": "false"
  15. }
  16. }
  17. }
  18. }

如果单个条件不匹配,则不会触发此规则:

  1. GET query-rules-test/_search
  2. {
  3. "query": {
  4. "rule": {
  5. "organic": {
  6. "match_none": {}
  7. },
  8. "ruleset_ids": [
  9. "rescue-dog-search-ruleset"
  10. ],
  11. "match_criteria": {
  12. "work_from_home": "true",
  13. "fenced_in_yard": "true",
  14. "kids_in_house": "false",
  15. "cats_in_house": "true"
  16. }
  17. }
  18. }
  19. }

查询规则故障排除

可能很难判断是否应用了固定查询规则。有两种方法可以判断查询规则是固定了你想要的文档还是自然返回。

我们确实有 explain API,但这可能不明显:规则查询被重写为固定查询,然后被重写为恒定分数查询,因此这基本上看起来像最大可能分数:

  1. "_explanation": {
  2. "value": 1.7014128e+38,
  3. "description": "max of:",
  4. "details": [
  5. {
  6. "value": 1.7014128e+38,
  7. "description": "max of:",
  8. "details": [
  9. {
  10. "value": 1.7014128e+38,
  11. "description": "ConstantScore(_id:([ff 63 68 69 68 61 75 68 75 61]))^1.7014128E38",
  12. "details": []
  13. },
  14. ...
  15. ]
  16. },
  17. ...
  18. ]
  19. }

对于 pinned rules,你还可以使用规则查询与不匹配查询相结合的方式仅返回规则,正如我们上面为了说明目的所做的那样:

  1. GET query-rules-test/_search
  2. {
  3. "query": {
  4. "rule": {
  5. "organic": {
  6. "match_none": {}
  7. },
  8. "ruleset_ids": [
  9. "dog-breed-ruleset",
  10. "super-special-ruleset"
  11. ],
  12. "match_criteria": {
  13. "breed": "pug"
  14. }
  15. }
  16. }
  17. }

或者,你可以简单地运行 organic 查询,并将该查询的结果与规则查询进行比较。

下一步是什么?

查询规则已普遍可用,但这并不意味着我们已经完成了!

我们的路线图上有很多令人兴奋的功能,包括:

  • 添加对排除文档和固定文档的支持
  • 无需进行 API 调用即可轻松管理查询规则的 UI
  • 分析器支持
  • ... 还有更多!

我们还在研究人们可能感兴趣的其他使用查询规则的方式。我们很乐意在我们的社区页面上听到你的反馈!

准备好自己尝试一下了吗?开始免费试用
想要获得 Elastic 认证吗?了解下一次 Elasticsearch 工程师培训何时举行!

原文:Elasticsearch query rules are now generally available — Search Labs

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/975725
推荐阅读
相关标签
  

闽ICP备14008679号