Terms aggregation cannot be applied to field – How to solve this Elasticsearch exception

Opster Team

August-23, Version: 6.8-7.7

Briefly, this error occurs when you try to apply a terms aggregation on a field that is not of type keyword or whose field data is not enabled. Elasticsearch cannot execute terms aggregation on text fields directly. To resolve this, you can either change the field type to keyword or enable field data. Alternatively, you can use a keyword subfield if one exists for the text field in question. Lastly, you can use a script to convert the field to a string before executing the aggregation.

This guide will help you check for common problems that cause the log ” terms aggregation cannot be applied to field [ ” to appear. To understand the issues related to this log, read the explanation below about the following Elasticsearch concepts: search, aggregations.

Log Context

Log “terms aggregation cannot be applied to field [” class name is TermsAggregatorFactory.java. We extracted the following from Elasticsearch source code for those seeking an in-depth context :

 return new LongTermsAggregator(name; factories; (ValuesSource.Numeric) valuesSource; config.format(); order;
 bucketCountThresholds; searchContext; parent; cm; showTermDocCountError; longFilter; pipelineAggregators;
 metaData);
 }  throw new AggregationExecutionException("terms aggregation cannot be applied to field [" + config.fieldContext().field()
 + "]. It can only be applied to numeric or string fields.");
 }  // return the SubAggCollectionMode that this aggregation should use based on the expected size
 // and the cardinality of the field

 

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?