# Terms aggregation on multiple indexes, ignore text fields

**URL:** <https://discuss.elastic.co/t/terms-aggregation-on-multiple-indexes-ignore-text-fields/285654>\
**Category:** Elasticsearch\
**Created:** [October 1, 2021, 7:31am UTC](https://discuss.elastic.co/t/terms-aggregation-on-multiple-indexes-ignore-text-fields/285654 "2021-10-01T07:31:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emil/32/49885_2.png) [@Emil](https://discuss.elastic.co/u/Emil)\
**Post date:** [October 1, 2021, 7:31am UTC](https://discuss.elastic.co/t/terms-aggregation-on-multiple-indexes-ignore-text-fields/285654/1 "2021-10-01T07:31:16Z")

</div>

I have multiple indexes, that I'd like to query at the same time, and find out which values are present in some field, with a terms aggregation.  
The problem is, this field is sometimes mapped as a keyword, and sometimes as a textfield with a seperate keyword-field (and fieldddata is disabled).  
So, when I do a terms aggregation, some indexes succeed, while others gives an error, "text fields are not optimised for ...".

How can I tell elastic to ignore these textfields? I don't really need a full count over all indexes, just get an overall impression. Ideally, I'd be able to do something like:

```auto
GET my_index/_search
{
  "query": {
    # Some query
  }
  "aggs": {
    "keyword-direct": {
      "terms": {
        "field": "my_field"
      }
    },
    "keyword-subfield": {
      "terms": {
        "field": "myfield.keyword"
      }
    }
  }
}

```

This should give me two aggregations, one with the results from the indices that have my\_field as a keyword, and one with results for the indices that have a my\_field.keyword-subfield.

However, the fact that the first aggregation fails causes the whole shard to just return an error, and I don't get the other results from that shard (neither the query-results nor the second aggregation).

The only workaround I've found is to split it into two queries. But then I have to manually check if all errors are expected, and also combine the query-results. It feels like a hack, and I'd think it could be done in a cleaner, more robust way.

---

<div class="post-metadata">

**Author:** ![Emil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emil/32/49885_2.png) [@Emil](https://discuss.elastic.co/u/Emil)\
**Post date:** [October 1, 2021, 7:41am UTC](https://discuss.elastic.co/t/terms-aggregation-on-multiple-indexes-ignore-text-fields/285654/2 "2021-10-01T07:41:27Z")

</div>

I also noted that if there are no results for the indexes with textfields, whether or not I get an error is undefined.

---

<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:** [October 29, 2021, 7:42am UTC](https://discuss.elastic.co/t/terms-aggregation-on-multiple-indexes-ignore-text-fields/285654/3 "2021-10-29T07:42:14Z")

</div>

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