# Unable to use termsAggregation with includes\[\] on Long type fields

**URL:** <https://discuss.elastic.co/t/unable-to-use-termsaggregation-with-includes-on-long-type-fields/216895>\
**Category:** Elasticsearch\
**Created:** [January 28, 2020, 5:37pm UTC](https://discuss.elastic.co/t/unable-to-use-termsaggregation-with-includes-on-long-type-fields/216895 "2020-01-28T17:37:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdcohen220](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@jdcohen220](https://discuss.elastic.co/u/jdcohen220)\
**Post date:** [January 28, 2020, 5:37pm UTC](https://discuss.elastic.co/t/unable-to-use-termsaggregation-with-includes-on-long-type-fields/216895/1 "2020-01-28T17:37:56Z")

</div>

I have a query with the following aggregation:  
"aggs": {  
"category": {  
"terms": {  
"field": "visits.categories",  
"include": [  
887706088583989170  
]  
}

This returns no results in the buckets section, however if I remove the includes that same id '887706088583989170' returns as a bucket result with data. This only seemed to become a problem when we updated the mapping of that field from string to long. Is this not supported on long types?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 29, 2020, 10:09am UTC](https://discuss.elastic.co/t/unable-to-use-termsaggregation-with-includes-on-long-type-fields/216895/2 "2020-01-29T10:09:29Z")

</div>

It looks like this is not supported.  
Numeric fields like longs are indexed in a way that is optimised for range queries so they usually represent a quantity of something (time, money, bytes etc). They are not optimised for exact-match retrieval of a value.  
For things like your "category" field I imagine these are used more like an identifier and never get queried using a range query. These are better modelled as a `keyword` field for fast retrieval and, as you've discovered, work with the include clause.  
Sadly it's a [common trap](https://github.com/elastic/elasticsearch/issues/49538) people fall into when defining mappings.

---

<div class="post-metadata">

**Author:** ![jdcohen220](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@jdcohen220](https://discuss.elastic.co/u/jdcohen220)\
**Post date:** [February 7, 2020, 6:12pm UTC](https://discuss.elastic.co/t/unable-to-use-termsaggregation-with-includes-on-long-type-fields/216895/3 "2020-02-07T18:12:53Z")

</div>

Thanks.  
We ended up being able to use a [multi-field](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html) type to work around this.  
It seems that errors thrown when one uses a `regex` type filter with this aggregation imply that any `numeric` type should work.

It seems like this and docs should be updated to show that only keyword types are _fully_ supported for the terms aggregation.

```
"root_cause": [
  {
    "type": "aggregation_execution_exception",
    "reason": "Aggregation [category] cannot support regular expression style include/exclude settings as they can only be applied to string fields. Use an array of numeric values for include/exclude clauses used to filter numeric fields"
  }
],
```

---

<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:** [March 6, 2020, 6:12pm UTC](https://discuss.elastic.co/t/unable-to-use-termsaggregation-with-includes-on-long-type-fields/216895/4 "2020-03-06T18:12:54Z")

</div>

Closed on March 6, 2020, 6:12pm UTC

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