es query+aggs 对空格,横杠处理问题

环境:
es:2.4
brand类型为 string
下面 query+aggs 查询会得出三条记录,分别是Best,Choice,Products,三条记录队了brand值,其他都相同,elasticsearch 把 Best Choice Products 拆分成三部分进行分别聚合,同样包含横杠如Best-Choice-Products,情况也一样
如果把query条件去除,则不会出现上述情况,求解!

以下是查询语句:

 {
    "size": 0,
    "aggs": {....},
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "brand": {
                                    "query": "Best Choice Products",
                                    "type": "phrase"
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}

以下是结果的部分
clipboard.png

阅读 9.1k
1 个回答

es 对 string 类型的聚合是针对分词后的 term 进行的,如果你想对原始值做聚合,使用子字段设置一个不分词的类型,然后针对该类型进行聚合分析

PUT /my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "city": {
          "type": "string",
          "fields": {
            "raw": { 
              "type":  "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
  }
}

https://www.elastic.co/guide/...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进