diff --git a/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java b/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java index 9324fb3157d4..1ed225a7fdb6 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java @@ -219,6 +219,12 @@ public static Map parseMapping(NamedXContentRegistry xContentReg public boolean updateMapping(final IndexMetadata currentIndexMetadata, final IndexMetadata newIndexMetadata) throws IOException { assert newIndexMetadata.getIndex().equals(index()) : "index mismatch: expected " + index() + " but was " + newIndexMetadata.getIndex(); + + if (currentIndexMetadata != null && currentIndexMetadata.getMappingVersion() == newIndexMetadata.getMappingVersion()) { + assertMappingVersion(currentIndexMetadata, newIndexMetadata, Collections.emptyMap()); + return false; + } + // go over and add the relevant mappings (or update them) Set existingMappers = new HashSet<>(); if (mapper != null) { @@ -230,7 +236,7 @@ public boolean updateMapping(final IndexMetadata currentIndexMetadata, final Ind final Map updatedEntries; try { // only update entries if needed - updatedEntries = internalMerge(newIndexMetadata, MergeReason.MAPPING_RECOVERY, true); + updatedEntries = internalMerge(newIndexMetadata, MergeReason.MAPPING_RECOVERY); } catch (Exception e) { logger.warn(() -> new ParameterizedMessage("[{}] failed to apply mappings", index()), e); throw e; @@ -278,7 +284,7 @@ public boolean updateMapping(final IndexMetadata currentIndexMetadata, final Ind private void assertMappingVersion( final IndexMetadata currentIndexMetadata, final IndexMetadata newIndexMetadata, - final Map updatedEntries) { + final Map updatedEntries) throws IOException { if (Assertions.ENABLED && currentIndexMetadata != null && currentIndexMetadata.getCreationVersion().onOrAfter(Version.V_6_5_0)) { @@ -302,10 +308,14 @@ private void assertMappingVersion( assert currentSource.equals(newSource) : "expected current mapping [" + currentSource + "] for type [" + mapping.type() + "] " + "to be the same as new mapping [" + newSource + "]"; + final CompressedXContent mapperSource = new CompressedXContent(Strings.toString(mapper)); + assert currentSource.equals(mapperSource) : + "expected current mapping [" + currentSource + "] for type [" + mapping.type() + "] " + + "to be the same as new mapping [" + mapperSource + "]"; } } else { - // if the mapping version is changed, it should increase, there should be updates, and the mapping should be different + // the mapping version should increase, there should be updates, and the mapping should be different final long currentMappingVersion = currentIndexMetadata.getMappingVersion(); final long newMappingVersion = newIndexMetadata.getMappingVersion(); assert currentMappingVersion < newMappingVersion : @@ -352,27 +362,19 @@ public void merge(String type, Map mappings, MergeReason reason) } public void merge(IndexMetadata indexMetadata, MergeReason reason) { - internalMerge(indexMetadata, reason, false); + internalMerge(indexMetadata, reason); } public DocumentMapper merge(String type, CompressedXContent mappingSource, MergeReason reason) { return internalMerge(Collections.singletonMap(type, mappingSource), reason).get(type); } - private synchronized Map internalMerge(IndexMetadata indexMetadata, - MergeReason reason, boolean onlyUpdateIfNeeded) { + private synchronized Map internalMerge(IndexMetadata indexMetadata, MergeReason reason) { assert reason != MergeReason.MAPPING_UPDATE_PREFLIGHT; Map map = new LinkedHashMap<>(); for (ObjectCursor cursor : indexMetadata.getMappings().values()) { MappingMetadata mappingMetadata = cursor.value; - if (onlyUpdateIfNeeded) { - DocumentMapper existingMapper = documentMapper(mappingMetadata.type()); - if (existingMapper == null || mappingMetadata.source().equals(existingMapper.mappingSource()) == false) { - map.put(mappingMetadata.type(), mappingMetadata.source()); - } - } else { - map.put(mappingMetadata.type(), mappingMetadata.source()); - } + map.put(mappingMetadata.type(), mappingMetadata.source()); } return internalMerge(map, reason); }