嗨,我试图通过使用Elasticsearch在查询中使用模糊来创建“你是不是意味着”的建议。例如,当用户搜索单词“applo”时,它将显示“apple”(因为有产品/品牌名称包含单词apple)。所以我想强调模糊匹配的单词(“apple”)并将其显示给用户。
这是我的财产:
"properties": {
"brand_name": {
"type": "keyword",
"store": true
},
{
"product_name": {
"type": "keyword",
"store": true
},
}
这是我的查询:
var should = { "should": [
{
"multi_match": {
"fields": ["product_name", "brand_name"],
"query": "applo",
"fuzziness": 2,
"prefix_length": 1
}
},
{
"query_string": {
"query": "*" + applo + "*",
"fields": ["product_name", "brand_name"]
}
}
],
"minimum_should_match": 1
};
body = {
size: 50,
from: 0,
query: {
bool: should
},
aggs: buildAggregate(),
"highlight": {
"fields": {
"brand_name": {},
"product_name": {}
}
}
};
模糊和查询工作正常,并给出正确的结果。但是结果中没有突出显示字段。我的查询中缺少什么或者映射属性中有什么要改变的?
数据示例:
{ took: 67, timed_out: false, _shards: { total: 5, successful: 5,
skipped: 0, failed: 0 }, hits: { total: 2, max_score: null, hits: [
[Object] ] }, aggregations: { brands:
{ doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [Array] },
minimum: { value: 1000 },
maximum: { value: 1000 },
values:
{ doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [Array] } } }
点击的对象:
{ _index: 'product', _type: 'product', _id: '1', _score: null, _source: { product_name: 'Apple Watch', brand_name: 'Apple' } }
开始吧:
"properties": {
"brand_name": {
"type": "text"
},
"product_name": {
"type": "text"
}
}
{
"query": {
"query_string": {
"query": "applo~1"
}
}
}
阅读此查询qazxsw poi。