作者:Kathleen DeRusso
我们很高兴宣布 Elasticsearch 8.10 中的查询规则! 查询规则(query rules)允许你根据正在搜索的查询词或根据作为搜索查询的一部分提供的上下文信息来更改查询。
查询规则(query rules)允许自定义搜索相关性之外的搜索结果,这可以根据您提供的上下文信息更好地控制目标查询的结果。 这为营销活动、个性化和特定细分市场的搜索结果提供了更有针对性的搜索结果,所有这些都内置于 Elasticsearch® 中!
首先,我们支持固定查询规则(pinned query rules),它允许你根据特定查询中的上下文来识别要在搜索结果顶部推广的文档。
使用固定查询规则,你可以:
…等等!
查询规则匹配条件可以是以下任意一种:
对于固定查询规则,操作可以是固定与索引 _id 字段对应的 id,也可以是固定指定索引中相应 _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"
]
}
}
]
}
在此规则集中,我们定义了两个规则:
提示:因为我们正在将其转换为底层的固定查询,所以我们需要遵守一些基本规则:
当我们根据查询规则进行搜索时,我们想要使用 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"
}
}
}
该查询由以下部分组成:
我们验证每个规则以确定是否应将其应用于查询。 当我们处理规则查询时,我们找到来自 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