From 7fc494727183fa1c6d4ba37bc38eccf1d2eadff9 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Fri, 8 Nov 2013 10:54:01 +0100 Subject: [PATCH] _default_ mapping not applied when using separate master/data nodes fixes #4124 --- .../metadata/MetaDataMappingService.java | 4 +++ .../cluster/SpecificMasterNodesTests.java | 33 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java b/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java index 643808eafeb..3c1b03b4204 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java @@ -399,6 +399,10 @@ public class MetaDataMappingService extends AbstractComponent { final IndexMetaData indexMetaData = currentState.metaData().index(index); IndexService indexService = indicesService.createIndex(indexMetaData.index(), indexMetaData.settings(), clusterService.localNode().id()); indicesToClose.add(indexMetaData.index()); + // make sure to add custom default mapping if exists + if (indexMetaData.mappings().containsKey(MapperService.DEFAULT_MAPPING)) { + indexService.mapperService().merge(MapperService.DEFAULT_MAPPING, indexMetaData.mappings().get(MapperService.DEFAULT_MAPPING).source().string(), false); + } // only add the current relevant mapping (if exists) if (indexMetaData.mappings().containsKey(request.mappingType)) { indexService.mapperService().merge(request.mappingType, indexMetaData.mappings().get(request.mappingType).source().string(), false); diff --git a/src/test/java/org/elasticsearch/cluster/SpecificMasterNodesTests.java b/src/test/java/org/elasticsearch/cluster/SpecificMasterNodesTests.java index e4b6f67ac74..ca0dae3a03d 100644 --- a/src/test/java/org/elasticsearch/cluster/SpecificMasterNodesTests.java +++ b/src/test/java/org/elasticsearch/cluster/SpecificMasterNodesTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.cluster; +import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.discovery.MasterNotDiscoveredException; import org.elasticsearch.test.AbstractIntegrationTest; @@ -26,18 +27,19 @@ import org.elasticsearch.test.AbstractIntegrationTest.ClusterScope; import org.elasticsearch.test.AbstractIntegrationTest.Scope; import org.junit.Test; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.nullValue; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.hamcrest.Matchers.*; /** * */ -@ClusterScope(scope = Scope.TEST, numNodes=0) +@ClusterScope(scope = Scope.TEST, numNodes = 0) public class SpecificMasterNodesTests extends AbstractIntegrationTest { protected final ImmutableSettings.Builder settingsBuilder() { return ImmutableSettings.builder().put("discovery.type", "zen"); } + @Test public void simpleOnlyMasterNodeElection() { logger.info("--> start data node / non master node"); @@ -95,4 +97,29 @@ public class SpecificMasterNodesTests extends AbstractIntegrationTest { assertThat(cluster().nonMasterClient().admin().cluster().prepareState().execute().actionGet().getState().nodes().masterNode().name(), equalTo(nextMasterEligableNodeName)); assertThat(cluster().masterClient().admin().cluster().prepareState().execute().actionGet().getState().nodes().masterNode().name(), equalTo(nextMasterEligableNodeName)); } + + /** + * Tests that putting custom default mapping and then putting a type mapping will have the default mapping merged + * to the type mapping. + */ + @Test + public void testCustomDefaultMapping() throws Exception { + logger.info("--> start master node / non data"); + cluster().startNode(settingsBuilder().put("node.data", false).put("node.master", true)); + + logger.info("--> start data node / non master node"); + cluster().startNode(settingsBuilder().put("node.data", true).put("node.master", false)); + + assertAcked(client().admin().indices().prepareCreate("test").setSettings("number_of_shards", 1).get()); + assertAcked(client().admin().indices().preparePutMapping("test").setType("_default_").setSource("_timestamp", "enabled=true")); + + MappingMetaData defaultMapping = client().admin().cluster().prepareState().get().getState().getMetaData().getIndices().get("test").getMappings().get("_default_"); + assertThat(defaultMapping.getSourceAsMap().get("_timestamp"), notNullValue()); + + assertAcked(client().admin().indices().preparePutMapping("test").setType("_default_").setSource("_timestamp", "enabled=true")); + + assertAcked(client().admin().indices().preparePutMapping("test").setType("type1").setSource("foo", "enabled=true")); + MappingMetaData type1Mapping = client().admin().cluster().prepareState().get().getState().getMetaData().getIndices().get("test").getMappings().get("type1"); + assertThat(type1Mapping.getSourceAsMap().get("_timestamp"), notNullValue()); + } }