首页 > 技术文章 > Elasticsearch5.x版本中对Text类型进行聚合时提示illegal_argument_exception

duanxuan 2017-03-17 15:57 原文

Having this field in my mapping
"answer": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
I try to execute this aggregation
"aggs": {
"answer": {
  "terms": {
    "field": "answer"
  }
},

but I get this error

"type": "illegal_argument_exception",
      "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [answer] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."

得到解决
You need to aggregate on the keyword sub-field, like this:
"aggs": {
"answer": {
  "terms": {
    "field": "answer.keyword"
  }
},

开始在网上查询解决方案时,有的是修改mapping
1.PUT my_index/_mapping/my_type { "properties": { "my_field": { "type": "text", "fielddata": true } } }


或者
2.使用.keyword 推荐
"aggs": { "answer": { "terms": { "field": "answer.keyword" } }

推荐阅读