Elasticsearch 8.10 中引入查询规则 query rules

作者:Kathleen DeRusso

我们很高兴宣布 Elasticsearch 8.10 中的查询规则! 查询规则(query rules)允许你根据正在搜索的查询词或根据作为搜索查询的一部分提供的上下文信息来更改查询。

什么是查询规则?

查询规则(query rules)允许自定义搜索相关性之外的搜索结果,这可以根据您提供的上下文信息更好地控制目标查询的结果。 这为营销活动、个性化和特定细分市场的搜索结果提供了更有针对性的搜索结果,所有这些都内置于 Elasticsearch® 中!

支持哪些类型的规则?

首先,我们支持固定查询规则(pinned query rules),它允许你根据特定查询中的上下文来识别要在搜索结果顶部推广的文档。

使用固定查询规则,你可以:

  • 当人们搜索 iPhone 时,固定结果以预订最新的 iPhone
  • 当人们搜索 iPhone 时,固定推荐其他品牌的文章
  • 如果用户搜索 “football” 位于美国或英国,则固定不同的结果
  • 固定重要的组织公告,使其位于每个人搜索结果的顶部
  • 有关所有非经理员工即将进行的员工绩效评估的固定信息

…等等!

支持哪些类型的标准?

查询规则匹配条件可以是以下任意一种:

  • exact:精确匹配指定值
  • fuzzy:在允许的 Levenshtein 编辑距离内匹配指定值
  • prefix:以指定值开头
  • suffix:以指定值结尾
  • contains:包含指定值
  • lt:小于规定值
  • lte:小于或等于指定值
  • gt:大于指定值
  • gte:大于或等于指定值
  • always:始终匹配所有规则查询

支持哪些类型的操作?

对于固定查询规则,操作可以是固定与索引 _id 字段对应的 id,也可以是固定指定索引中相应 _id 字段的文档。

查询规则如何工作?

在底层,创建和使用查询规则的过程如下:

  • 管理员创建包含一个或多个上下文查询规则的查询规则集。
  • 使用 query rules management API,,我们将这些查询规则存储在 Elasticsearch 中。
  • 搜索使用 rule_query,其中包括搜索查询以及查询规则集和匹配条件。
  • Elasticsearch 识别规则集中与查询中指定的条件相匹配的所有规则。
  • 每个匹配规则按照其在规则集中出现的顺序应用。
  • 在查询重写阶段,该查询被重写为固定查询(pinned query),固定规则中标识的 ID 或文档。
  • 新的固定查询运行并返回结果,匹配的升级结果位于顶部。
  • 该图显示了生命周期:
  • 例子

    一家全球电子商务网站想要推广其新型无线充电器 PureJuice Pro。

    该索引包括以下文档:

    1. POST /products/_doc/us1
    2. {
    3. "name": "PureJuice Pro",
    4. "description": "PureJuice Pro: Experience the pinnacle of wireless charging. Blending rapid charging tech with sleek design, it ensures your devices are powered swiftly and safely. The future of charging is here.",
    5. "price": 15,
    6. "currency": "USD",
    7. "plug_type": "B",
    8. "voltage": "120v",
    9. "country": "us"
    10. }
    12. POST /products/_doc/uk1
    13. {
    14. "name": "PureJuice Pro - UK Compatible",
    15. "description": "PureJuice Pro: Redefining wireless charging. Seamlessly merging swift charging capabilities with a refined aesthetic, it guarantees your devices receive rapid and secure power. Welcome to the next generation of charging.",
    16. "price": 20,
    17. "currency": "GBP",
    18. "plug_type": "G",
    19. "voltage": "230V",
    20. "country": "uk"
    21. }
    23. POST /products/_doc/eu1
    24. {
    25. "name": "PureJuice Pro - Wireless Charger suitable for European plugs",
    26. "description": "PureJuice Pro: Elevating wireless charging. Combining unparalleled charging speeds with elegant design, it promises both rapid and dependable energy for your devices. Embrace the future of wireless charging.",
    27. "price": 18,
    28. "currency": "EUR",
    29. "plug_type": "C",
    30. "voltage": "230V",
    31. "country": "euro"
    32. }

    使用以下查询在该索引中搜索无线充电器(wireless charger):

    1. POST /products/_search?filter_path=**.hits
    2. {
    3. "query": {
    4. "multi_match": {
    5. "query": "wireless charger",
    6. "fields": [ "name^5", "description" ]
    7. }
    8. }
    9. }

    由于 name 匹配,此查询将首先返回 European 充电器 - 但我们可能希望根据搜索者的位置推广不同的版本。

    1. {
    2. "hits": {
    3. "hits": [
    4. {
    5. "_index": "products",
    6. "_id": "eu1",
    7. "_score": 7.590337,
    8. "_source": {
    9. "name": "PureJuice Pro - Wireless Charger suitable for European plugs",
    10. "description": "PureJuice Pro: Elevating wireless charging. Combining unparalleled charging speeds with elegant design, it promises both rapid and dependable energy for your devices. Embrace the future of wireless charging.",
    11. "price": 18,
    12. "currency": "EUR",
    13. "plug_type": "C",
    14. "voltage": "230V"
    15. }
    16. },
    17. {
    18. "_index": "products",
    19. "_id": "us1",
    20. "_score": 0.1323013,
    21. "_source": {
    22. "name": "PureJuice Pro",
    23. "description": "PureJuice Pro: Experience the pinnacle of wireless charging. Blending rapid charging tech with sleek design, it ensures your devices are powered swiftly and safely. The future of charging is here.",
    24. "price": 15,
    25. "currency": "USD",
    26. "plug_type": "B",
    27. "voltage": "120v"
    28. }
    29. },
    30. {
    31. "_index": "products",
    32. "_id": "uk1",
    33. "_score": 0.1323013,
    34. "_source": {
    35. "name": "PureJuice Pro - UK Compatible",
    36. "description": "PureJuice Pro: Redefining wireless charging. Seamlessly merging swift charging capabilities with a refined aesthetic, it guarantees your devices receive rapid and secure power. Welcome to the next generation of charging.",
    37. "price": 20,
    38. "currency": "GBP",
    39. "plug_type": "G",
    40. "voltage": "230V"
    41. }
    42. }
    43. ]
    44. }
    45. }

    管理查询规则

    首先,具有 manage_search_query_rules 权限的管理员使用查询规则管理 API 定义并存储规则集。

    1. PUT /_query_rules/promotion-rules
    2. {
    3. "rules": [
    4. {
    5. "rule_id": "us-charger",
    6. "type": "pinned",
    7. "criteria": [
    8. {
    9. "type": "contains",
    10. "metadata": "description",
    11. "values": [
    12. "wireless charger"
    13. ]
    14. },
    15. {
    16. "type": "exact",
    17. "metadata": "country",
    18. "values": [
    19. "us"
    20. ]
    21. }
    22. ],
    23. "actions": {
    24. "ids": [
    25. "us1"
    26. ]
    27. }
    28. },
    29. {
    30. "rule_id": "uk-charger",
    31. "type": "pinned",
    32. "criteria": [
    33. {
    34. "type": "contains",
    35. "metadata": "description",
    36. "values": [
    37. "wireless charger"
    38. ]
    39. },
    40. {
    41. "type": "exact",
    42. "metadata": "country",
    43. "values": [
    44. "uk"
    45. ]
    46. }
    47. ],
    48. "actions": {
    49. "ids": [
    50. "uk1"
    51. ]
    52. }
    53. }
    54. ]
    55. }

    在此规则集中,我们定义了两个规则:

    • 如果用户的搜索词包含短语 “wireless charger” 并且它们位于美国 (us),我们希望将美国产品固定在结果集的顶部。
    • 如果用户执行相同的搜索但位于英国 (uk),我们希望将该产品的英国版本固定在结果集的顶部。

    提示:因为我们正在将其转换为底层的固定查询,所以我们需要遵守一些基本规则:

    • 我们可以定义要固定的 id 或文档 (docs),但不能同时定义两者。 为了避免混乱,最佳实践是选择其中之一并在查询规则集中保持一致。
    • 固定查询(pinned query)最多只能支持 100 个固定文档。 任何给定的规则都会强制执行此限制。

    搜索查询规则

    当我们根据查询规则进行搜索时,我们想要使用 rule_query。 规则查询示例可能如下所示:

    1. POST /products/_search?filter_path=**.hits
    2. {
    3. "query": {
    4. "rule_query": {
    5. "organic": {
    6. "multi_match": {
    7. "query": "reliable wireless charger for iPhone",
    8. "fields": [
    9. "name^5",
    10. "description"
    11. ]
    12. }
    13. },
    14. "match_criteria": {
    15. "description": "reliable wireless charger for iPhone",
    16. "country": "us"
    17. },
    18. "ruleset_id": "promotion-rules"
    19. }
    20. }
    21. }

    该查询由以下部分组成:

    • organic 查询,这是我们想要运行并对文档进行排名的搜索。 如果没有应用查询规则,则不做任何修改地执行该查询。
    • 匹配标准,定义了我们要匹配的标准。 在此示例中,我们有两条 match_criteria:
      • my_query,在本例中与用户在搜索框中输入的字符串相同
      • country,通过对用户 IP 地址使用地理定位来确定
    • ruleset ID,它确定我们在其中查找匹配规则的规则集。

    我们验证每个规则以确定是否应将其应用于查询。 当我们处理规则查询时,我们找到来自 us 的查询 “wireless charger” 的匹配规则:

    1. {
    2. "rule_id": "us-charger",
    3. "type": "pinned",
    4. "criteria": [
    5. {
    6. "type": "contains",
    7. "metadata": "my_query",
    8. "values": ["wireless charger"]
    9. },
    10. {
    11. "type": "exact",
    12. "metadata": "country",
    13. "values": ["us"]
    14. }
    15. ],
    16. "actions": {
    17. "ids": [
    18. "us1"
    19. ]
    20. }
    21. }

    提醒一下,为了使查询规则匹配,查询规则中定义的所有条件都必须与规则查询输入匹配。

    在执行查询之前,它被重写为固定查询 (pinned query):

    1. {
    2. "query": {
    3. "pinned": {
    4. "organic": {
    5. "query_string": {
    6. "query": "reliable wireless charger for iPhone"
    7. }
    8. },
    9. "ids": [
    10. "us1"
    11. ]
    12. }
    13. }
    14. }

    Elasticsearch 使用这个新的固定查询进行搜索,并返回结果,其中固定的美国版本产品位于结果集的顶部。

    上面查询的结果为:

    1. {
    2. "hits": {
    3. "hits": [
    4. {
    5. "_index": "products",
    6. "_id": "us1",
    7. "_score": 1.7014122e+38,
    8. "_source": {
    9. "name": "PureJuice Pro",
    10. "description": "PureJuice Pro: Experience the pinnacle of wireless charging. Blending rapid charging tech with sleek design, it ensures your devices are powered swiftly and safely. The future of charging is here.",
    11. "price": 15,
    12. "currency": "USD",
    13. "plug_type": "B",
    14. "voltage": "120v",
    15. "country": "us"
    16. }
    17. },
    18. {
    19. "_index": "products",
    20. "_id": "eu1",
    21. "_score": 13.700377,
    22. "_source": {
    23. "name": "PureJuice Pro - Wireless Charger suitable for European plugs",
    24. "description": "PureJuice Pro: Elevating wireless charging. Combining unparalleled charging speeds with elegant design, it promises both rapid and dependable energy for your devices. Embrace the future of wireless charging.",
    25. "price": 18,
    26. "currency": "EUR",
    27. "plug_type": "C",
    28. "voltage": "230V",
    29. "country": "euro"
    30. }
    31. },
    32. {
    33. "_index": "products",
    34. "_id": "uk1",
    35. "_score": 0.104635,
    36. "_source": {
    37. "name": "PureJuice Pro - UK Compatible",
    38. "description": "PureJuice Pro: Redefining wireless charging. Seamlessly merging swift charging capabilities with a refined aesthetic, it guarantees your devices receive rapid and secure power. Welcome to the next generation of charging.",
    39. "price": 20,
    40. "currency": "GBP",
    41. "plug_type": "G",
    42. "voltage": "230V",
    43. "country": "uk"
    44. }
    45. }
    46. ]
    47. }
    48. }

    结论

    我们向您展示了如何定义查询规则以根据用户输入的查询或个性化数据等上下文信息来推广结果,以及如何使用这些规则进行搜索。

    请在 Elastic® 8.10发行说明中了解此功能及更多信息,并通过 14 天免费试用 Elastic Cloud自行尝试使用查询规则进行搜索。 我们很乐意在 GitHub 和我们的讨论论坛上收到你的来信。

    本文中描述的任何特性或功能的发布和时间安排均由 Elastic 自行决定。功能可能无法按时交付或根本无法交付。

    原文:Introduction to query rules in Elasticsearch | Elastic Blog