# Aggregations on fields indexed as text (Almost) painless

**URL:** <https://discuss.elastic.co/t/aggregations-on-fields-indexed-as-text-almost-painless/161851>\
**Category:** Elasticsearch\
**Created:** [December 21, 2018, 1:34pm UTC](https://discuss.elastic.co/t/aggregations-on-fields-indexed-as-text-almost-painless/161851 "2018-12-21T13:34:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![daver208](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daver208/32/3586_2.png) [@daver208](https://discuss.elastic.co/u/daver208)\
**Post date:** [December 21, 2018, 1:34pm UTC](https://discuss.elastic.co/t/aggregations-on-fields-indexed-as-text-almost-painless/161851/1 "2018-12-21T13:34:50Z")

</div>

So I'm sure this has happened to some folks, you've made a decision to index a field as text, but don't set field data to true. At some point later, you realize this field isn't very high cardinality, and you want to do a terms aggregation to see the unique values in the field. But when you do the query you get the dreaded:

```auto
        "reason": {
          "type": "illegal_argument_exception",
          "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [text_field] 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."
        }

```

The official Elasticsearch stance on this is to set field data to true or set the type to keyword and use the re-index API. This probably is the correct solution if you wish to do this going forward. That said, everyone wants to make this `just work` now, so this is what I found. The solution is to use the params['\_source'] in a script. This only really referenced [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html#modules-scripting-stored) as far as i can tell. If you set a terms aggregation as such:

```auto
  "aggs": {
    "text_field_terms": {
      "terms": {
        "script": {
          "source": "params['_source']['text_field']",
          "lang": "painless"
        },
        "size": 50
      }
    }
  }

```

You will get the behavior you want without having to re-index. CAUTION: This query may take a very long time. If this field has a very high cardinality, this probably won't work. But if you need a quick and dirty solution to the problem, there you go. This was tested on Elasticsearch v6.4.X.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 18, 2019, 1:35pm UTC](https://discuss.elastic.co/t/aggregations-on-fields-indexed-as-text-almost-painless/161851/2 "2019-01-18T13:35:02Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
