elasticsearch - Highresult 不显示在模糊查询 Elasticsearch 结果中
问题描述
您好我正在尝试通过使用 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"
}
}
进行此更改后,您将无法在这些字段上运行聚合。这是思考点。因为:如果您使用 fielddata,那么您将失去性能并且存储空间会增加。
最后一点,我建议将您的查询简化为 query_string:
{
"query": {
"query_string": {
"query": "applo~1"
}
}
}
在此处阅读有关此查询的信息。
推荐阅读
- java - 在 Recycleview 中将数据绑定到布局
- sql - 相互比较行并根据条件仅保留一行
- reactjs - 使用 SVG 时哪种方式更好
- node.js - 如果传递了无效标志,如何让 Yargs 退出?
- python - 检测多个像素并在 Python 中并行单击它们的更快方法?
- javascript - 如何在用户脚本中下载文件的前 X 个字节?
- gps - 智能手机的位置检测方法和 U-blox GPS 模块有什么区别?
- python - 在嵌套 Python 字典中搜索键
- java - 如何在没有 Spring 的情况下将 javascript 资源导入 thymeleaf 模板?
- python - 如何修复sklearn中的随机化