gpt4 book ai didi

具有不同权重的多字段 Elasticsearch 补全建议器

转载 作者:行者123 更新时间:2023-11-29 02:45:21 26 4
gpt4 key购买 nike

我在 Elasticsearch 中使用 Completion Suggester 来允许部分单词匹配查询。在我的索引 (products_index) 中,我希望能够同时查询 product_name 字段和 brand 字段。这是我的映射:

POST /product_index

mappings: {
products: {
properties: {
brand: {
type: "string",
analyzer: "english"
},
product_name: {
type: "string",
analyzer: "english"
},
id: {
type: "long"
},
lookup_count: {
type: "long"
},
suggest: {
type: "completion",
analyzer: "simple",
payloads: true,
preserve_separators: true,
preserve_position_increments: true,
max_input_length: 50
},
upc: {
type: "string"
}
}
}
}

这是我的数据:

POST /product_index/products/2
{
id: 2,
brand: "Coca-Cola",
product_name: "Classic Coke",
suggest: {
input: [
"Classic Coke",
"Coca-Cola"
],
output: "Classic Coke - Coca-Cola",
payload: {
id: 2,
product_name: "Classic Coke",
brand: "Coca-Cola",
popularity: 10
},
weight: 0
}
}

这是我的查询:

POST /product_index/_search

"suggest": {
"product_suggest": {
"text": 'coca-co',
"completion": {
"field": 'suggest'
}
}
}

除了我想为 product_name 字段提供比 brand 字段更高的权重外,这非常有效。有什么办法可以做到这一点?我调查了这个article关于使用 bool 查询,但我对 Elasticsearch 很陌生,不确定如何在完成建议的情况下应用它。

非常感谢!

最佳答案

正如 redox 所说,完成建议器非常简单,不支持条目提升。我的解决方案是制作两个建议字段,一个用于品牌,一个用于产品名称:

POST /product_index
{
"mappings": {
"products": {
"properties": {
"brand": {
"type": "string",
"analyzer": "english"
},
"product_name": {
"type": "string",
"analyzer": "english"
},
"id": {
"type": "long"
},
"lookup_count": {
"type": "long"
},
"product-suggest": {
"type": "completion",
"analyzer": "simple",
"payloads": true,
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50
},
"brand-suggest": {
"type": "completion",
"analyzer": "simple",
"payloads": true,
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50
},
"upc": {
"type": "string"
}
}
}
}
}

索引时,填写两个字段:

POST /product_index/products/2
{
"id": 2,
"brand": "Coca-Cola",
"product_name": "Classic Coke",
"brand-suggest": {
"input": [
"Coca-Cola"
],
"output": "Classic Coke - Coca-Cola",
"payload": {
"id": 2,
"product_name": "Classic Coke",
"brand": "Coca-Cola",
"popularity": 10
}
},
"product-suggest": {
"input": [
"Classic Coke"
],
"output": "Classic Coke - Coca-Cola",
"payload": {
"id": 2,
"product_name": "Classic Coke",
"brand": "Coca-Cola",
"popularity": 10
}
}
}

查询时,在品牌和产品建议者上提出一个建议:

POST /product_index/_search
{
"suggest": {
"product_suggestion": {
"text": "coca-co",
"completion": {
"field": "product-suggest"
}
},
"brand_suggestion": {
"text": "coca-co",
"completion": {
"field": "brand-suggest"
}
}
}
}

您可以将品牌建议的建议列表附加到产品建议的建议列表中,去除重复后,得到只有相关建议、没有重复和产品建议在前的建议列表。

另一种解决方案是使用提升品牌和产品的查询,而不是使用建议器。但是,此实现速度较慢,因为它不使用建议器。

关于具有不同权重的多字段 Elasticsearch 补全建议器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28268418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com