Highresult不会在模糊查询Elasticsearch结果中显示

问题描述 投票:0回答:1

嗨,我试图通过使用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'
   }
}
elasticsearch search
1个回答
0
投票

开始吧:

  1. 您的映射不适合搜索模糊。您需要了解textkeyword数据类型之间的区别。简而言之,关键字按原样索引,无法更改。文本转换为令牌,更多转化应用于令牌。为了更多的理解,如何索引过程我建议开始阅读this article。然后我建议你改变你的映射:
"properties": {
   "brand_name": {
       "type": "text"
   },
   "product_name": {
      "type": "text"
    }
 }
  1. 在此更改之后,您无法在这些字段上运行聚合。这是思考点。因为:如果你创建了fielddata,那么你会失去性能并且会增加存储空间。
  2. 为了最后一点,我建议将查询简化为query_string:
{
  "query": {
     "query_string": {
       "query": "applo~1"
     }
  }
}

阅读此查询qazxsw poi。

© www.soinside.com 2019 - 2024. All rights reserved.