diff --git a/server/src/main/java/org/elasticsearch/action/bulk/TransportBulkAction.java b/server/src/main/java/org/elasticsearch/action/bulk/TransportBulkAction.java index 7979b633efe..567b7fb8080 100644 --- a/server/src/main/java/org/elasticsearch/action/bulk/TransportBulkAction.java +++ b/server/src/main/java/org/elasticsearch/action/bulk/TransportBulkAction.java @@ -351,7 +351,7 @@ public class TransportBulkAction extends HandledTransportAction, ToXContentFragmen * setting its routing, timestamp, and so on if needed. */ @Nullable - public MappingMetaData mappingOrDefault(String mappingType) { - MappingMetaData mapping = mappings.get(mappingType); - if (mapping != null) { - return mapping; + public MappingMetaData mappingOrDefault() { + MappingMetaData mapping = null; + for (ObjectCursor m : mappings.values()) { + if (mapping == null || mapping.type().equals(MapperService.DEFAULT_MAPPING)) { + mapping = m.value; + } } - return mappings.get(MapperService.DEFAULT_MAPPING); + + return mapping; } ImmutableOpenMap getCustomData() { diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/IndexMetaDataTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/IndexMetaDataTests.java index 1fdea596afb..e3b6234daa7 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/IndexMetaDataTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/IndexMetaDataTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.cluster.metadata; +import org.elasticsearch.Version; import org.elasticsearch.action.admin.indices.rollover.MaxAgeCondition; import org.elasticsearch.action.admin.indices.rollover.MaxDocsCondition; import org.elasticsearch.action.admin.indices.rollover.MaxSizeCondition; @@ -39,6 +40,7 @@ import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; +import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.IndicesModule; import org.elasticsearch.test.ESTestCase; @@ -287,4 +289,38 @@ public class IndexMetaDataTests extends ESTestCase { () -> IndexMetaData.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.get(notAFactorySettings)); assertEquals("the number of source shards [2] must be a factor of [3]", iae.getMessage()); } + + public void testMappingOrDefault() throws IOException { + Settings settings = Settings.builder() + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 2) + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1) + .build(); + IndexMetaData meta = IndexMetaData.builder("index") + .settings(settings) + .build(); + assertNull(meta.mappingOrDefault()); + + meta = IndexMetaData.builder("index") + .settings(settings) + .putMapping("type", "{}") + .build(); + assertNotNull(meta.mappingOrDefault()); + assertEquals("type", meta.mappingOrDefault().type()); + + meta = IndexMetaData.builder("index") + .settings(settings) + .putMapping(MapperService.DEFAULT_MAPPING, "{}") + .build(); + assertNotNull(meta.mappingOrDefault()); + assertEquals(MapperService.DEFAULT_MAPPING, meta.mappingOrDefault().type()); + + meta = IndexMetaData.builder("index") + .settings(settings) + .putMapping("type", "{}") + .putMapping(MapperService.DEFAULT_MAPPING, "{}") + .build(); + assertNotNull(meta.mappingOrDefault()); + assertEquals("type", meta.mappingOrDefault().type()); + } }