Term query does not support array of values – How to solve this Elasticsearch exception

Opster Team

August-23, Version: 6.8-8.9

Briefly, this error occurs when you try to use an array of values in a term query in Elasticsearch, which is not supported. Term queries are designed to match exact values, not multiple values. To resolve this issue, you can use the ‘terms’ query instead of ‘term’ if you want to match multiple values. Alternatively, you can use a ‘bool’ query with multiple ‘term’ queries inside it, each matching a different value. This way, you can search for multiple exact values in your Elasticsearch data.

Before you dig into reading this guide, have you tried asking OpsGPT what this log means? You’ll receive a customized analysis of your log.

Try OpsGPT now for step-by-step guidance and tailored insights into your Elasticsearch/OpenSearch operation.

To fully understand why term query doesn’t support an array of values in Elasticsearch, we recommend you run AutoOps for Elasticsearch which can resolve issues that cause many errors.

This guide will help you check for common problems that cause this log to appear. It’s important to understand the issues related to this error in Elasticsearch, so to get started, read the general overview on common issues and tips related to: query and index.

Background

Term query returns documents that contain an exact term in a provided field. You cannot use term query to search an array of field values. 

If you want to search for multiple values, you should use terms query instead of term query.

How to reproduce this exception

Index mapping:

First, let’s create an index with the following mapping, which contains a single field called “name” of keyword type.

PUT /my-index
{
 "mappings": {
   "properties": {
     "name": {
       "type": "keyword"
     }
   }
 }
}

Index data:

Then, we index some sample data in the index we just created.

PUT /my-index/_doc/1?pretty
{
 "name": "Opster"
}
 
PUT /my-index/_doc/2?pretty
{
 "name": "Elasticsearch"
}

Search query:

Now let’s try to search for multiple values using the term query below:

POST /my-index/_search
{
 "query": {
   "term": {
     "name": [
       "Opster",
       "Elasticsearch"
     ]
   }
 }
}

Search result:

Doing so, Elasticsearch is going to throw an error stating that the term query does not support an array of values.

{
 "error": {
   "root_cause": [
     {
       "type": "parsing_exception",
       "reason": "[term] query does not support array of values",
       "line": 4,
       "col": 15
     }
   ],
   "type": "parsing_exception",
   "reason": "[term] query does not support array of values",
   "line": 4,
   "col": 15
 },
 "status": 400
}

How to fix this exception

If you want to search for multiple values, you should use terms query instead.

Use terms query:

POST /my-index/_search
{
 "query": {
   "terms": {
     "name": [
       "Opster",
       "Elasticsearch"
     ]
   }
 }
}

Log Context

Log “[term] query does not support array of values” class name is TermQueryBuilder.java. We extracted the following from Elasticsearch source code for those seeking an in-depth context :

 } else if (token.isValue()) {
 throwParsingExceptionOnMultipleFields(NAME; parser.getTokenLocation(); fieldName; parser.currentName());
 fieldName = currentFieldName;
 value = maybeConvertToBytesRef(parser.objectBytes());
 } else if (token == XContentParser.Token.START_ARRAY) {
 throw new ParsingException(parser.getTokenLocation(); "[term] query does not support array of values");
 }
 }  TermQueryBuilder termQuery = new TermQueryBuilder(fieldName; value);
 termQuery.boost(boost);

 

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?