我在Elasticsearch中使用“完成建议程序”来允许部分单词匹配查询。在我的索引(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 字段更高的权重。有什么办法可以实现?我已经研究了有关使用 布尔 查询的文章,但是我对Elasticsearch还是很陌生,并且不确定如何在完成建议程序的情况下应用它。
非常感谢!
就像氧化还原所说的那样,完成建议器确实很简单,不支持条目提升。我的解决方案是创建两个建议字段,一个用于品牌,一个用于产品名称:
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" } } } }
在删除重复项之后,您可以将品牌建议的提议列表附加到产品建议之一中,以仅具有相关建议,无重复项和产品建议的形式具有建议列表。
另一个解决方案是使用查询来提升品牌和产品,而不是使用建议者。但是,此实现比较慢,因为它不使用建议程序。