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

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?

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 people fall into when defining mappings.

1 Like

Thanks.
We ended up being able to use a multi-field 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"
  }
],

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