IndexMetaData#mappingOrDefault doesn't need to take a type argument. (#37480)

Currently it takes a type, but this isn't really needed now that indices can
have at most one type. The only downside is that we might return a different
error when trying to index into a type that doesnt't exist yet.
This commit is contained in:
Adrien Grand 2019-01-16 14:01:09 +01:00 committed by GitHub
parent 21a88d5505
commit 9d8afe68a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 7 deletions

View File

@ -351,7 +351,7 @@ public class TransportBulkAction extends HandledTransportAction<BulkRequest, Bul
case INDEX: case INDEX:
IndexRequest indexRequest = (IndexRequest) docWriteRequest; IndexRequest indexRequest = (IndexRequest) docWriteRequest;
final IndexMetaData indexMetaData = metaData.index(concreteIndex); final IndexMetaData indexMetaData = metaData.index(concreteIndex);
MappingMetaData mappingMd = indexMetaData.mappingOrDefault(indexRequest.type()); MappingMetaData mappingMd = indexMetaData.mappingOrDefault();
Version indexCreated = indexMetaData.getCreationVersion(); Version indexCreated = indexMetaData.getCreationVersion();
indexRequest.resolveRouting(metaData); indexRequest.resolveRouting(metaData);
indexRequest.process(indexCreated, mappingMd, concreteIndex.getName()); indexRequest.process(indexCreated, mappingMd, concreteIndex.getName());

View File

@ -191,7 +191,7 @@ public class TransportShardBulkAction extends TransportWriteAction<BulkShardRequ
case UPDATED: case UPDATED:
IndexRequest indexRequest = updateResult.action(); IndexRequest indexRequest = updateResult.action();
IndexMetaData metaData = context.getPrimary().indexSettings().getIndexMetaData(); IndexMetaData metaData = context.getPrimary().indexSettings().getIndexMetaData();
MappingMetaData mappingMd = metaData.mappingOrDefault(indexRequest.type()); MappingMetaData mappingMd = metaData.mappingOrDefault();
indexRequest.process(metaData.getCreationVersion(), mappingMd, updateRequest.concreteIndex()); indexRequest.process(metaData.getCreationVersion(), mappingMd, updateRequest.concreteIndex());
context.setRequestToExecute(indexRequest); context.setRequestToExecute(indexRequest);
break; break;

View File

@ -505,12 +505,15 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
* setting its routing, timestamp, and so on if needed. * setting its routing, timestamp, and so on if needed.
*/ */
@Nullable @Nullable
public MappingMetaData mappingOrDefault(String mappingType) { public MappingMetaData mappingOrDefault() {
MappingMetaData mapping = mappings.get(mappingType); MappingMetaData mapping = null;
if (mapping != null) { for (ObjectCursor<MappingMetaData> m : mappings.values()) {
return mapping; if (mapping == null || mapping.type().equals(MapperService.DEFAULT_MAPPING)) {
mapping = m.value;
} }
return mappings.get(MapperService.DEFAULT_MAPPING); }
return mapping;
} }
ImmutableOpenMap<String, DiffableStringMap> getCustomData() { ImmutableOpenMap<String, DiffableStringMap> getCustomData() {

View File

@ -19,6 +19,7 @@
package org.elasticsearch.cluster.metadata; package org.elasticsearch.cluster.metadata;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.rollover.MaxAgeCondition; import org.elasticsearch.action.admin.indices.rollover.MaxAgeCondition;
import org.elasticsearch.action.admin.indices.rollover.MaxDocsCondition; import org.elasticsearch.action.admin.indices.rollover.MaxDocsCondition;
import org.elasticsearch.action.admin.indices.rollover.MaxSizeCondition; 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.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.indices.IndicesModule; import org.elasticsearch.indices.IndicesModule;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
@ -287,4 +289,38 @@ public class IndexMetaDataTests extends ESTestCase {
() -> IndexMetaData.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.get(notAFactorySettings)); () -> IndexMetaData.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.get(notAFactorySettings));
assertEquals("the number of source shards [2] must be a factor of [3]", iae.getMessage()); 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());
}
} }