-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Disallow "enabled" attribute change for types in mapping update (#33566) #33933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cbuescher
merged 13 commits into
elastic:master
from
cbismuth:33566_disallow_type_enabled_mapping_update
Oct 1, 2018
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f09f1a1
Disallow "enabled" attribute change for types in mapping update (#33566)
cbismuth 7e6cb90
Rename method in which mapping changes are resolved (#33566)
cbismuth ce31c85
Remove unnecessary type conditions (#33566)
cbismuth 696b9a3
Remove unnecessary mapper exception constructor (#33566)
cbismuth c1db69e
Shorten "check 'enabled' field change" method name (#33566)
cbismuth e4458be
Revert unnecessary Git change (#33566)
cbismuth 02b50dd
Private method can be static (#33566)
cbismuth df57bbb
Add test cases to object mapper merge operations (#33566)
cbismuth 1a890a4
Delete unnecessary blank line (#33566)
cbismuth 51feaae
Move Explicit allocations into method to ease readability (#33566)
cbismuth 1489108
Use current version in index settings (#33566)
cbismuth 760b6cc
Prefer mapper builders over constructors (#33566)
cbismuth ec6e768
Merge branch 'master' into 33566_disallow_type_enabled_mapping_update
cbismuth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
server/src/test/java/org/elasticsearch/index/mapper/ObjectMapperMergeTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* 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() { | ||
cbuescher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
|
||
// 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)); | ||
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)); | ||
assertEquals("Can't update attribute for type [type1.disabled.enabled] in index mapping", e.getMessage()); | ||
} | ||
|
||
private static RootObjectMapper createRootObjectMapper(String name, boolean enabled, Map<String, Mapper> 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<String, Mapper> 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, null, indexSettings, MultiFields.empty(), CopyTo.empty()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.