Skip to content

Commit

Permalink
Remove index mapper dynamic settings (#25734)
Browse files Browse the repository at this point in the history
Remove "index.mapper.dynamic" setting for 6.0 (and after) indices, but
still keep working for 5.x (and before) indices. Remove two index
dynamic disable test cases as the disability of index.mapper.dynamic is
already removed for current version. Add a new test class for version
test.
  • Loading branch information
PnPie authored and jpountz committed Sep 12, 2017
1 parent 06150d4 commit 3d4e28a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,18 @@ public MapperService(IndexSettings indexSettings, IndexAnalyzers indexAnalyzers,
this.searchQuoteAnalyzer = new MapperAnalyzerWrapper(indexAnalyzers.getDefaultSearchQuoteAnalyzer(), p -> p.searchQuoteAnalyzer());
this.mapperRegistry = mapperRegistry;

this.dynamic = this.indexSettings.getValue(INDEX_MAPPER_DYNAMIC_SETTING);
if (indexSettings.getIndexVersionCreated().onOrAfter(Version.V_6_0_0_alpha1)) {
if (INDEX_MAPPER_DYNAMIC_SETTING.exists(indexSettings.getSettings())) {
if (indexSettings.getIndexVersionCreated().onOrAfter(Version.V_7_0_0_alpha1)) {
throw new IllegalArgumentException("Setting " + INDEX_MAPPER_DYNAMIC_SETTING.getKey() + " was removed after version 6.0.0");
} else {
DEPRECATION_LOGGER.deprecated("Setting " + INDEX_MAPPER_DYNAMIC_SETTING.getKey() + " is deprecated since indices may not have more than one type anymore.");
}
}
this.dynamic = INDEX_MAPPER_DYNAMIC_DEFAULT;
} else {
this.dynamic = this.indexSettings.getValue(INDEX_MAPPER_DYNAMIC_SETTING);
}
defaultMappingSource = "{\"_default_\":{}}";

if (logger.isTraceEnabled()) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -153,34 +153,4 @@ public void run() {
assertTrue(client().prepareGet("index", "type", Integer.toString(i)).get().isExists());
}
}

public void testAutoCreateWithDisabledDynamicMappings() throws Exception {
assertAcked(client().admin().indices().preparePutTemplate("my_template")
.setCreate(true)
.setPatterns(Collections.singletonList("index_*"))
.addMapping("foo", "field", "type=keyword")
.setSettings(Settings.builder().put("index.mapper.dynamic", false).build())
.get());

// succeeds since 'foo' has an explicit mapping in the template
indexRandom(true, false, client().prepareIndex("index_1", "foo", "1").setSource("field", "abc"));

// fails since 'bar' does not have an explicit mapping in the template and dynamic template creation is disabled
TypeMissingException e1 = expectThrows(TypeMissingException.class,
() -> client().prepareIndex("index_2", "bar", "1").setSource("field", "abc").get());
assertEquals("type[bar] missing", e1.getMessage());
assertEquals("trying to auto create mapping, but dynamic mapping is disabled", e1.getCause().getMessage());

BulkResponse bulkResponse = client().prepareBulk().add(new IndexRequest("index_2", "bar", "2").source("field", "abc")).get();
assertTrue(bulkResponse.hasFailures());
BulkItemResponse.Failure firstFailure = bulkResponse.getItems()[0].getFailure();
assertThat(firstFailure.getCause(), instanceOf(TypeMissingException.class));
assertEquals("type[bar] missing", firstFailure.getCause().getMessage());
assertEquals("trying to auto create mapping, but dynamic mapping is disabled", firstFailure.getCause().getCause().getMessage());

// make sure no mappings were created for bar
GetIndexResponse getIndexResponse = client().admin().indices().prepareGetIndex().addIndices("index_2").get();
assertFalse(getIndexResponse.mappings().containsKey("bar"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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 org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.indices.TypeMissingException;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.elasticsearch.test.InternalSettingsPlugin;

import java.io.IOException;
import java.util.Collection;

public class DynamicMappingVersionTests extends ESSingleNodeTestCase {

@Override
protected Collection<Class<? extends Plugin>> getPlugins() {
return pluginList(InternalSettingsPlugin.class);
}

public void testDynamicMappingDefault() throws IOException {
MapperService mapperService = createIndex("my-index").mapperService();
DocumentMapper documentMapper = mapperService
.documentMapperWithAutoCreate("my-type").getDocumentMapper();

ParsedDocument parsedDoc = documentMapper.parse(
SourceToParse.source("my-index", "my-type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("foo", 3)
.endObject()
.bytes(), XContentType.JSON));

String expectedMapping = XContentFactory.jsonBuilder().startObject()
.startObject("my-type")
.startObject("properties")
.startObject("foo").field("type", "long")
.endObject().endObject().endObject().endObject().string();
assertEquals(expectedMapping, parsedDoc.dynamicMappingsUpdate().toString());
}

public void testDynamicMappingSettingRemoval() {
Settings settings = Settings.builder()
.put(MapperService.INDEX_MAPPER_DYNAMIC_SETTING.getKey(), false)
.build();
Exception e = expectThrows(IllegalArgumentException.class, () -> createIndex("test-index", settings));
assertEquals(e.getMessage(), "Setting index.mapper.dynamic was removed after version 6.0.0");
}

public void testDynamicMappingDisablePreEs6() {
Settings settingsPreEs6 = Settings.builder()
.put(MapperService.INDEX_MAPPER_DYNAMIC_SETTING.getKey(), false)
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_5_0_0)
.build();
MapperService preEs6MapperService = createIndex("pre-es6-index", settingsPreEs6).mapperService();
Exception e = expectThrows(TypeMissingException.class,
() -> preEs6MapperService.documentMapperWithAutoCreate("pre-es6-type"));
assertEquals(e.getMessage(), "type[pre-es6-type] missing");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.test.ESIntegTestCase;

Expand All @@ -42,7 +43,7 @@ public void testGetSettingsWithBlocks() throws Exception {
.setSettings(Settings.builder()
.put("index.refresh_interval", -1)
.put("index.merge.policy.expunge_deletes_allowed", "30")
.put(MapperService.INDEX_MAPPER_DYNAMIC_SETTING.getKey(), false)));
.put(FieldMapper.IGNORE_MALFORMED_SETTING.getKey(), false)));

for (String block : Arrays.asList(SETTING_BLOCKS_READ, SETTING_BLOCKS_WRITE, SETTING_READ_ONLY, SETTING_READ_ONLY_ALLOW_DELETE)) {
try {
Expand All @@ -51,7 +52,7 @@ public void testGetSettingsWithBlocks() throws Exception {
assertThat(response.getIndexToSettings().size(), greaterThanOrEqualTo(1));
assertThat(response.getSetting("test", "index.refresh_interval"), equalTo("-1"));
assertThat(response.getSetting("test", "index.merge.policy.expunge_deletes_allowed"), equalTo("30"));
assertThat(response.getSetting("test", MapperService.INDEX_MAPPER_DYNAMIC_SETTING.getKey()), equalTo("false"));
assertThat(response.getSetting("test", FieldMapper.IGNORE_MALFORMED_SETTING.getKey()), equalTo("false"));
} finally {
disableIndexBlock("test", block);
}
Expand Down

0 comments on commit 3d4e28a

Please sign in to comment.