Analyzer and search analyzer on field – How to solve this Elasticsearch exception

Opster Team

August-23, Version: 6.8-7.9

Briefly, this error occurs when both the ‘analyzer’ and ‘search_analyzer’ are defined on the same field in Elasticsearch. Elasticsearch uses ‘analyzer’ for indexing and ‘search_analyzer’ for searching. If both are defined, it can cause conflicts. To resolve this, you can either remove one of them or ensure they are set to compatible values. If you want different behaviors for indexing and searching, consider using ‘analyzer’ for indexing and ‘search_analyzer’ for searching, but ensure they are compatible to avoid unexpected results.

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.

In addition we recommend you run the AutoOps for Elasticsearch which can resolve issues that cause many errors.

This guide will help you understand why the log “analyzer and search_analyzer on field” appears. It’s important to understand the relevant basic information as well, so see the concept definition of index below.

Background

Analysis is the process that Elasticsearch performs on the body of a document before the document is sent off to be added to the inverted index. Elasticsearch goes through a number of steps for every analyzed field before the document is added to the index. These steps are:

  1. Character filtering
  2. Breaking text into tokens
  3. Token filtering

Analyzers are a clever mix of these three components added together. By default, queries will use the analyzer defined in the field mapping, but this can be overridden with the search_analyzer setting. search_analyzer is defined when you want to use a different analyzer at the search time. 

Note that this behaviour is different in ES 7.10 version. Elasticsearch no longer expects you to give both analyzer and search_analyzer when you use search_quote_analyzer in the mapping, hence this error is valid only in Elasticsearch versions below 7.10.

How to reproduce this exception

To recreate this exception, create an index with the following mapping:

PUT /my-index
{
  "settings":{
     "analysis":{
        "analyzer":{
           "my_analyzer":{
              "type":"custom",
              "tokenizer":"standard",
              "filter":[
                 "lowercase"
              ]
           },
           "my_stop_analyzer":{
              "type":"custom",
              "tokenizer":"standard",
              "filter":[
                 "lowercase",
                 "english_stop"
              ]
           }
        },
        "filter":{
           "english_stop":{
              "type":"stop",
              "stopwords":"_english_"
           }
        }
     }
  },
  "mappings":{
      "properties":{
         "title": {
            "type":"text",
            "search_quote_analyzer":"my_analyzer"
        }
     }
  }
}

The response will be:

{
 "error": {
   "root_cause": [
     {
       "type": "mapper_parsing_exception",
       "reason": "analyzer and search_analyzer on field [title] must be set when search_quote_analyzer is set"
     }
   ],
   "type": "mapper_parsing_exception",
   "reason": "Failed to parse mapping [_doc]: analyzer and search_analyzer on field [title] must be set when search_quote_analyzer is set",
   "caused_by": {
     "type": "mapper_parsing_exception",
     "reason": "analyzer and search_analyzer on field [title] must be set when search_quote_analyzer is set"
   }
 },
 "status": 400
}

How to fix this exception

The exception clearly states that you need to set both the analyzer and search analyzer, when search_quote_analyzer is set. The search_quote_analyzer setting points to the my_analyzer analyzer, as it allows you to specify an analyzer for phrases.

To fix this exception, modify the index mapping:

PUT /my-index
{
  "settings":{
     "analysis":{
        "analyzer":{
           "my_analyzer":{
              "type":"custom",
              "tokenizer":"standard",
              "filter":[
                 "lowercase"
              ]
           },
           "my_stop_analyzer":{
              "type":"custom",
              "tokenizer":"standard",
              "filter":[
                 "lowercase",
                 "english_stop"
              ]
           }
        },
        "filter":{
           "english_stop":{
              "type":"stop",
              "stopwords":"_english_"
           }
        }
     }
  },
  "mappings":{
      "properties":{
         "title": {
            "type":"text",
            "analyzer":"my_analyzer",
            "search_analyzer":"my_stop_analyzer",
            "search_quote_analyzer":"my_analyzer"
        }
     }
  }
}

Log Context

Log “analyzer and search_analyzer on field [” class name is TypeParsers.java. We extracted the following from Elasticsearch source code for those seeking an in-depth context :

 if (indexAnalyzer == null && searchAnalyzer != null) {
 throw new MapperParsingException("analyzer on field [" + name + "] must be set when search_analyzer is set");
 }  if (searchAnalyzer == null && searchQuoteAnalyzer != null) {
 throw new MapperParsingException("analyzer and search_analyzer on field [" + name +
 "] must be set when search_quote_analyzer is set");
 }  if (searchAnalyzer == null) {
 searchAnalyzer = indexAnalyzer;

 

 [ratemypost]