12

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."

Do i have to change my mapping or am i using the wrong aggregation ? (just updated from 2.x to 5.1)

Nacho Nieva
  • 181
  • 1
  • 1
  • 11

3 Answers3

31

You need to aggregate on the keyword sub-field, like this:

"aggs": {
"answer": {
  "terms": {
    "field": "answer.keyword"
  }
},

That will work.

Val
  • 207,596
  • 13
  • 358
  • 360
2

In Aggregation, just add keyword to answer.It worked for me. For text fields we need to add keyword. "field": "answer.keyword"

yogitha
  • 29
  • 3
1

Adding to @Val's answer, you can also set the fielddata to true during your mapping itself:

"answer": {
        "type": "text",
        "fielddata": true, <-- add this line
        "fields": {
          "keyword": {
            "type": "keyword",                
            "ignore_above": 256
          }
        }
      },
Kulasangar
  • 9,046
  • 5
  • 51
  • 82
  • 1
    I'm not certain the [`keyword` type](https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html) accepts any `fielddata` setting actually, like the `text` type does. – Val Jan 05 '17 at 12:31
  • As per the exception (`Set fielddata=true on [answer]`), I thought the issue was from the `answer` in the mapping provided. – Kulasangar Jan 05 '17 at 12:37
  • 1
    That's because he aggregated on `answer`, but he's better off aggregating on `answer.keyword` since he has that field. – Val Jan 05 '17 at 12:38