问题描述
我使用Elasticsearch中的Completion Suggester来允许部分单词匹配查询。在我的索引(products_index)中,我希望能够查询 product_name 字段和品牌字段。这是我的映射:
I'm using the Completion Suggester in Elasticsearch to allow partial word matching queries. In my index (products_index), I'd like to be able to query both the product_name field and the brand field. Here are my mappings:
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"
}
}
}
}
这是我的数据: / p>
Here is my data:
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 字段比品牌字段更高的权重。有什么办法可以实现吗?我查看了,使用 bool 查询,但我对Elasticsearch很新,并且不确定如何在完成建议的情况下应用它。
This works great except that I'd like to give the product_name field a higher weighting than the brand field. Is there any way I can achieve this? I have looked into this article on using bool queries but I'm quite new to Elasticsearch and unsure how I can apply that in the case of completion suggester.
非常感谢!
推荐答案
suggester真的很简单,不支持增加条目。
我的解决方案是制作两个sugtster字段,一个用于品牌,一个用于产品名称:
As redox said, the completion suggester is really simple and doesn't support entries boosting.My solution would be to make two suggester fields, one for brand and one for product name:
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
}
}
}
当查询时,在品牌和产品建议书上提出一个建议:
When querying, make one suggest on both the brand and product suggesters:
POST /product_index/_search
{
"suggest": {
"product_suggestion": {
"text": "coca-co",
"completion": {
"field": "product-suggest"
}
},
"brand_suggestion": {
"text": "coca-co",
"completion": {
"field": "brand-suggest"
}
}
}
}
您可以将品牌建议的建议列表附加到产品建议之一后,删除重复,只列出有关建议,不重复和产品建议首先。
You can append the list of suggestions of brand-suggestion to the one of product suggestion, after having removed the duplicates, to have list of suggestions with only relevant suggestions, no duplicates and the product suggestions first.
另一个解决方案是使用一个查询,提升品牌和pr产品,而不是使用suggesters。然而,这种实现速度较慢,因为它不使用suggester。
Another solution would be to use a query with boosting on brand and product, instead of using suggesters. This implementation is however slower since it doesn't use suggesters.
这篇关于弹性搜索结果在不同加权的多磁盘上完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!