Analyzer and search_analyzer on field – How to solve this Elasticsearch error

Analyzer and search_analyzer on field – How to solve this Elasticsearch error

Opster Team

February-21, Version: 1.7-8.0

Before you begin reading this guide, we recommend you run the Elasticsearch Error Check-Up 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 [“classname  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;

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