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

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

Opster Team

February-21, Version: 1.7-8.0

To understand why term query doesn’t support an array of values in Elasticsearch, we recommend you run the Elasticsearch Error Check-Up which can resolve issues that cause many errors. The tool is free and requires no installation.

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”classname  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);

Run the Check-Up to get customized recommendations like this:

error

Heavy merges detected in specific nodes

error-img

Description

A large number of small shards can slow down searches and cause cluster instability. Some indices have shards that are too small…

error-img

Recommendations Based on your specific ES deployment you should…

Based on your specific ES deployment you should…

1

X-PUT curl -H [a customized recommendation]

 

Optimize Elasticsearch Performance

Try The Tool