1
votes

I'm trying to write a query to return unique cities. My code is:

GET /files/_doc/_search
{
  "size":"0",
  "aggs" : {
    "uniq_cities" : {
      "terms" : { "field" : "cities" }

    }
  }
}

I have an error message as follows:

Fielddata is disabled on text fields by default. Set fielddata=true on [cities] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.

When I run

GET /files/_doc/_mapping

I get:

"cities" : {
  "type" : "text",
  "fields" : {
    "keyword" : {
      "type" : "keyword",
      "ignore_above" : 256
    }
  }
}

Based on the above, it seems that I already have a keyword field. How do I fix the error message?

1

1 Answers

3
votes

The error says "Fielddata is disabled on text fields by default" because you are trying to apply aggregation on a text field cities. It also has a sub field keyword whose data type is keyword. Therefore, apply the aggregation on cities.keyword field as below:

GET /files/_doc/_search
{
  "size":"0",
  "aggs" : {
    "uniq_cities" : {
      "terms" : { "field" : "cities.keyword" }

    }
  }
}