Mapping update rejected by primary – How to solve this Elasticsearch error

Opster Team

Aug-23, Version: 7.6-8.2

Briefly, this error occurs when there’s an attempt to update the mapping of an index that is not accepted by the primary shard. This could be due to a conflict in the mapping structure or a network partition. To resolve this, ensure that the mapping structure is consistent across all shards. If it’s a network issue, check the cluster health and resolve any network partitions. Also, consider increasing the timeout value for mapping updates if they are large and taking longer than expected.

In addition we recommend you run the Elasticsearch Template Optimizer to fix problems in your data modeling.

It will analyze your templates to detect issues and improve search performance, reduce indexing bottlenecks and optimize storage utilization. The Template Optimizer is free and requires no installation.

Background

A mapping type used to be a separate collection inside the same index. _doc is also a mapping type. For example, before ES version 6, a forum index can have two types: user and messages. Both these types can belong to the same index, and you can search for these multiple types in a single index itself. 

But since mapping types were deprecated in ES version 6, users can only use one mapping type. You can either keep the mapping type as user or messages. Later on, for ES version 7, _doc is a part of the path.

For more information on the deprecating of mapping types, refer to this explanation

How to reproduce this log in Elasticsearch version 7.x

Create index:

PUT /my-index
{
 "mappings": {
   "properties": {
     "title": {
       "type": "text"
     }
   }
 }
}

Index data:

POST /my-index/_doctype/1?pretty
{
 "title":"hello world"
}

The response will be:

{
 "error": {
   "root_cause": [
     {
       "type": "invalid_type_name_exception",
       "reason": "mapping type name [_doctype] can't start with '_' unless it is called [_doc]"
     }
   ],
   "type": "invalid_type_name_exception",
   "reason": "mapping type name [_doctype] can't start with '_' unless it is called [_doc]"
 },
 "status": 400
}

The log generated is:

[INFO ][o.e.a.b.TransportShardBulkAction] [my-index][0] mapping update rejected by primary
org.elasticsearch.indices.InvalidTypeNameException: mapping type name [_doctype] can't start with '_' unless it is called [_doc]

What this error means

This log message is an INFO message saying that you cannot use any other mapping type. Elasticsearch indices now support only the single document type, _doc.

You can index a new JSON document or update a document with the _doc mapping type ONLY.

Quick troubleshooting steps

Considering the above example, you need to use _doc instead of _doctype, as shown below:

POST /my-index/_doc/1?pretty
{
 "title":"hello world"
}

Log Context

Log “{} mapping update rejected by primary” classname is TransportShardBulkAction.java.
We extracted the following from Elasticsearch source code for those seeking an in-depth context :

                        MapperService.SINGLE_MAPPING_NAME;
                        new CompressedXContent(result.getRequiredMappingUpdate());
                        MapperService.MergeReason.MAPPING_UPDATE_PREFLIGHT
                    );
            } catch (Exception e) {
                logger.info(() -> new ParameterizedMessage("{} mapping update rejected by primary"; primary.shardId()); e);
                assert result.getId() != null;
                onComplete(exceptionToResult(e; primary; isDelete; version; result.getId()); context; updateResult);
                return true;
            }

 

 [ratemypost]