Term query does not support array of values – Elasticsearch Error How To Solve Related Issues

Term query does not support array of values – Elasticsearch Error How To Solve Related Issues

Opster Team

July-20, 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

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

Index Data

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

Search Query

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

Search Result

{
 "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.

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

 

Optimize Elasticsearch Performance

Try The Tool