diff --git a/docs/reference/release-notes/6.5.asciidoc b/docs/reference/release-notes/6.5.asciidoc index ed3e328405d80..2b161e85e20c4 100644 --- a/docs/reference/release-notes/6.5.asciidoc +++ b/docs/reference/release-notes/6.5.asciidoc @@ -51,9 +51,13 @@ coming[6.5.0] Also see <>. -// [float] -// [[breaking-6.5.0]] -// === Breaking Changes +[float] +[[breaking-6.5.0]] +=== Breaking Changes + +Mapping:: +* Disallow `enabled` attribute changes for types in mapping updates +{pull}33933[#33933] (issue: {issue}33566[#33566]) [float] [[breaking-java-6.5.0]] diff --git a/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java index 0c95c35196f63..4fa8623183931 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java @@ -473,6 +473,8 @@ protected void doMerge(final ObjectMapper mergeWith, boolean updateAllTypes) { for (Mapper mergeWithMapper : mergeWith) { Mapper mergeIntoMapper = mappers.get(mergeWithMapper.simpleName()); + checkEnabledFieldChange(mergeWith, mergeWithMapper, mergeIntoMapper); + Mapper merged; if (mergeIntoMapper == null) { // no mapping, simply add it @@ -485,6 +487,18 @@ protected void doMerge(final ObjectMapper mergeWith, boolean updateAllTypes) { } } + private static void checkEnabledFieldChange(ObjectMapper mergeWith, Mapper mergeWithMapper, Mapper mergeIntoMapper) { + if (mergeIntoMapper instanceof ObjectMapper && mergeWithMapper instanceof ObjectMapper) { + final ObjectMapper mergeIntoObjectMapper = (ObjectMapper) mergeIntoMapper; + final ObjectMapper mergeWithObjectMapper = (ObjectMapper) mergeWithMapper; + + if (mergeIntoObjectMapper.isEnabled() != mergeWithObjectMapper.isEnabled()) { + final String path = mergeWith.fullPath() + "." + mergeWithObjectMapper.simpleName() + ".enabled"; + throw new MapperException("Can't update attribute for type [" + path + "] in index mapping"); + } + } + } + @Override public ObjectMapper updateFieldType(Map fullNameToFieldType) { List updatedMappers = null; diff --git a/server/src/test/java/org/elasticsearch/index/mapper/ObjectMapperMergeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/ObjectMapperMergeTests.java new file mode 100644 index 0000000000000..68d7ac211770f --- /dev/null +++ b/server/src/test/java/org/elasticsearch/index/mapper/ObjectMapperMergeTests.java @@ -0,0 +1,126 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper; + +import com.google.common.collect.ImmutableMap; + +import org.elasticsearch.Version; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.mapper.FieldMapper.CopyTo; +import org.elasticsearch.index.mapper.FieldMapper.MultiFields; +import org.elasticsearch.index.mapper.TextFieldMapper.TextFieldType; +import org.elasticsearch.test.ESTestCase; +import org.junit.AfterClass; + +import java.util.Map; + +import static java.util.Collections.emptyMap; +import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_VERSION_CREATED; +import static org.hamcrest.Matchers.notNullValue; + +public class ObjectMapperMergeTests extends ESTestCase { + + private static FieldMapper barFieldMapper = createTextFieldMapper("bar"); + private static FieldMapper bazFieldMapper = createTextFieldMapper("baz"); + + private static RootObjectMapper rootObjectMapper = createRootObjectMapper( + "type1", true, ImmutableMap.of( + "disabled", createObjectMapper("disabled", false, emptyMap()), + "foo", createObjectMapper("foo", true, ImmutableMap.of( + "bar", barFieldMapper)))); + + @AfterClass + public static void cleanupReferences() { + barFieldMapper = null; + bazFieldMapper = null; + rootObjectMapper = null; + } + + public void testMerge() { + // GIVEN an enriched mapping with "baz" new field + ObjectMapper mergeWith = createRootObjectMapper( + "type1", true, ImmutableMap.of( + "disabled", createObjectMapper("disabled", false, emptyMap()), + "foo", createObjectMapper("foo", true, ImmutableMap.of( + "bar", barFieldMapper, + "baz", bazFieldMapper)))); + + // WHEN merging mappings + final ObjectMapper merged = rootObjectMapper.merge(mergeWith, true); + + // THEN "baz" new field is added to merged mapping + final ObjectMapper mergedFoo = (ObjectMapper) merged.getMapper("foo"); + assertThat(mergedFoo.getMapper("bar"), notNullValue()); + assertThat(mergedFoo.getMapper("baz"), notNullValue()); + } + + public void testMergeWhenDisablingField() { + // GIVEN a mapping with "foo" field disabled + ObjectMapper mergeWith = createRootObjectMapper( + "type1", true, ImmutableMap.of( + "disabled", createObjectMapper("disabled", false, emptyMap()), + "foo", createObjectMapper("foo", false, emptyMap()))); + + // WHEN merging mappings + // THEN a MapperException is thrown with an excepted message + MapperException e = expectThrows(MapperException.class, () -> rootObjectMapper.merge(mergeWith, true)); + assertEquals("Can't update attribute for type [type1.foo.enabled] in index mapping", e.getMessage()); + } + + public void testMergeWhenEnablingField() { + // GIVEN a mapping with "disabled" field enabled + ObjectMapper mergeWith = createRootObjectMapper( + "type1", true, ImmutableMap.of( + "disabled", createObjectMapper("disabled", true, emptyMap()), + "foo", createObjectMapper("foo", true, ImmutableMap.of( + "bar", barFieldMapper)))); + + // WHEN merging mappings + // THEN a MapperException is thrown with an excepted message + MapperException e = expectThrows(MapperException.class, () -> rootObjectMapper.merge(mergeWith, true)); + assertEquals("Can't update attribute for type [type1.disabled.enabled] in index mapping", e.getMessage()); + } + + private static RootObjectMapper createRootObjectMapper(String name, boolean enabled, Map mappers) { + final Settings indexSettings = Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT).build(); + final Mapper.BuilderContext context = new Mapper.BuilderContext(indexSettings, new ContentPath()); + final RootObjectMapper rootObjectMapper = new RootObjectMapper.Builder(name).enabled(enabled).build(context); + + mappers.values().forEach(rootObjectMapper::putMapper); + + return rootObjectMapper; + } + + private static ObjectMapper createObjectMapper(String name, boolean enabled, Map mappers) { + final Settings indexSettings = Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT).build(); + final Mapper.BuilderContext context = new Mapper.BuilderContext(indexSettings, new ContentPath()); + final ObjectMapper mapper = new ObjectMapper.Builder(name).enabled(enabled).build(context); + + mappers.values().forEach(mapper::putMapper); + + return mapper; + } + + private static TextFieldMapper createTextFieldMapper(String name) { + final TextFieldType fieldType = new TextFieldType(); + final Settings indexSettings = Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT).build(); + + return new TextFieldMapper(name, fieldType, fieldType, -1, true, null, indexSettings, MultiFields.empty(), CopyTo.empty()); + } +}