作者: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。
该索引包括以下文档:
POST /products/_doc/us1
{
"name": "PureJuice Pro",
"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.",
"price": 15,
"currency": "USD",
"plug_type": "B",
"voltage": "120v",
"country": "us"
}
POST /products/_doc/uk1
{
"name": "PureJuice Pro - UK Compatible",
"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.",
"price": 20,
"currency": "GBP",
"plug_type": "G",
"voltage": "230V",
"country": "uk"
}
POST /products/_doc/eu1
{
"name": "PureJuice Pro - Wireless Charger suitable for European plugs",
"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.",
"price": 18,
"currency": "EUR",
"plug_type": "C",
"voltage": "230V",
"country": "euro"
}
使用以下查询在该索引中搜索无线充电器(wireless charger):
POST /products/_search?filter_path=**.hits
{
"query": {
"multi_match": {
"query": "wireless charger",
"fields": [ "name^5", "description" ]
}
}
}
由于 name 匹配,此查询将首先返回 European 充电器 - 但我们可能希望根据搜索者的位置推广不同的版本。
{
"hits": {
"hits": [
{
"_index": "products",
"_id": "eu1",
"_score": 7.590337,
"_source": {
"name": "PureJuice Pro - Wireless Charger suitable for European plugs",
"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.",
"price": 18,
"currency": "EUR",
"plug_type": "C",
"voltage": "230V"
}
},
{
"_index": "products",
"_id": "us1",
"_score": 0.1323013,
"_source": {
"name": "PureJuice Pro",
"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.",
"price": 15,
"currency": "USD",
"plug_type": "B",
"voltage": "120v"
}
},
{
"_index": "products",
"_id": "uk1",
"_score": 0.1323013,
"_source": {
"name": "PureJuice Pro - UK Compatible",
"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.",
"price": 20,
"currency": "GBP",
"plug_type": "G",
"voltage": "230V"
}
}
]
}
}
管理查询规则
首先,具有 manage_search_query_rules 权限的管理员使用查询规则管理 API 定义并存储规则集。
PUT /_query_rules/promotion-rules
{
"rules": [
{
"rule_id": "us-charger",
"type": "pinned",
"criteria": [
{
"type": "contains",
"metadata": "description",
"values": [
"wireless charger"
]
},
{
"type": "exact",
"metadata": "country",
"values": [
"us"
]
}
],
"actions": {
"ids": [
"us1"
]
}
},
{
"rule_id": "uk-charger",
"type": "pinned",
"criteria": [
{
"type": "contains",
"metadata": "description",
"values": [
"wireless charger"
]
},
{
"type": "exact",
"metadata": "country",
"values": [
"uk"
]
}
],
"actions": {
"ids": [
"uk1"
]
}
}
]
}
在此规则集中,我们定义了两个规则:
- 如果用户的搜索词包含短语 “wireless charger” 并且它们位于美国 (us),我们希望将美国产品固定在结果集的顶部。
- 如果用户执行相同的搜索但位于英国 (uk),我们希望将该产品的英国版本固定在结果集的顶部。
提示:因为我们正在将其转换为底层的固定查询,所以我们需要遵守一些基本规则:
- 我们可以定义要固定的 id 或文档 (docs),但不能同时定义两者。 为了避免混乱,最佳实践是选择其中之一并在查询规则集中保持一致。
- 固定查询(pinned query)最多只能支持 100 个固定文档。 任何给定的规则都会强制执行此限制。
搜索查询规则
当我们根据查询规则进行搜索时,我们想要使用 rule_query。 规则查询示例可能如下所示:
POST /products/_search?filter_path=**.hits
{
"query": {
"rule_query": {
"organic": {
"multi_match": {
"query": "reliable wireless charger for iPhone",
"fields": [
"name^5",
"description"
]
}
},
"match_criteria": {
"description": "reliable wireless charger for iPhone",
"country": "us"
},
"ruleset_id": "promotion-rules"
}
}
}
该查询由以下部分组成:
- organic 查询,这是我们想要运行并对文档进行排名的搜索。 如果没有应用查询规则,则不做任何修改地执行该查询。
- 匹配标准,定义了我们要匹配的标准。 在此示例中,我们有两条 match_criteria:
- my_query,在本例中与用户在搜索框中输入的字符串相同
- country,通过对用户 IP 地址使用地理定位来确定
- ruleset ID,它确定我们在其中查找匹配规则的规则集。
我们验证每个规则以确定是否应将其应用于查询。 当我们处理规则查询时,我们找到来自 us 的查询 “wireless charger” 的匹配规则:
{
"rule_id": "us-charger",
"type": "pinned",
"criteria": [
{
"type": "contains",
"metadata": "my_query",
"values": ["wireless charger"]
},
{
"type": "exact",
"metadata": "country",
"values": ["us"]
}
],
"actions": {
"ids": [
"us1"
]
}
}
提醒一下,为了使查询规则匹配,查询规则中定义的所有条件都必须与规则查询输入匹配。
在执行查询之前,它被重写为固定查询 (pinned query):
{
"query": {
"pinned": {
"organic": {
"query_string": {
"query": "reliable wireless charger for iPhone"
}
},
"ids": [
"us1"
]
}
}
}
Elasticsearch 使用这个新的固定查询进行搜索,并返回结果,其中固定的美国版本产品位于结果集的顶部。
上面查询的结果为:
{
"hits": {
"hits": [
{
"_index": "products",
"_id": "us1",
"_score": 1.7014122e+38,
"_source": {
"name": "PureJuice Pro",
"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.",
"price": 15,
"currency": "USD",
"plug_type": "B",
"voltage": "120v",
"country": "us"
}
},
{
"_index": "products",
"_id": "eu1",
"_score": 13.700377,
"_source": {
"name": "PureJuice Pro - Wireless Charger suitable for European plugs",
"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.",
"price": 18,
"currency": "EUR",
"plug_type": "C",
"voltage": "230V",
"country": "euro"
}
},
{
"_index": "products",
"_id": "uk1",
"_score": 0.104635,
"_source": {
"name": "PureJuice Pro - UK Compatible",
"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.",
"price": 20,
"currency": "GBP",
"plug_type": "G",
"voltage": "230V",
"country": "uk"
}
}
]
}
}
结论
我们向您展示了如何定义查询规则以根据用户输入的查询或个性化数据等上下文信息来推广结果,以及如何使用这些规则进行搜索。
请在 Elastic® 8.10发行说明中了解此功能及更多信息,并通过 14 天免费试用 Elastic Cloud自行尝试使用查询规则进行搜索。 我们很乐意在 GitHub 和我们的讨论论坛上收到你的来信。
本文中描述的任何特性或功能的发布和时间安排均由 Elastic 自行决定。功能可能无法按时交付或根本无法交付。
原文:Introduction to query rules in Elasticsearch | Elastic Blog