Skip to content

Commit d13a3ea

Browse files
authored
Empty mapping to hold null meta (#72709)
With #72616 we have changed the way that empty mappings are created for an index. Accidentally such mappings have been created with an empty meta map instead of null, which causes test failures in tests that compare mappings and are now finding an unexpected empty meta object which was not there before. This commit fixes the empty mapping to not hold any meta, replacing the empty map with a null argument.
1 parent 3a3e93f commit d13a3ea

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

server/src/main/java/org/elasticsearch/index/mapper/Mapping.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.io.IOException;
2424
import java.io.UncheckedIOException;
2525
import java.util.Arrays;
26-
import java.util.Collections;
2726
import java.util.Comparator;
2827
import java.util.HashMap;
2928
import java.util.Map;
@@ -37,9 +36,7 @@
3736
public final class Mapping implements ToXContentFragment {
3837

3938
public static final Mapping EMPTY = new Mapping(
40-
new RootObjectMapper.Builder("_doc", Version.CURRENT).build(new ContentPath()),
41-
new MetadataFieldMapper[0],
42-
Collections.emptyMap());
39+
new RootObjectMapper.Builder("_doc", Version.CURRENT).build(new ContentPath()), new MetadataFieldMapper[0], null);
4340

4441
private final RootObjectMapper root;
4542
private final Map<String, Object> meta;

server/src/test/java/org/elasticsearch/index/mapper/DocumentMapperTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
1414
import org.apache.lucene.analysis.standard.StandardAnalyzer;
1515
import org.elasticsearch.Version;
16+
import org.elasticsearch.common.Strings;
1617
import org.elasticsearch.common.compress.CompressedXContent;
1718
import org.elasticsearch.common.settings.Settings;
1819
import org.elasticsearch.index.IndexSettings;
@@ -279,4 +280,17 @@ public void testMergeMetaForIndexTemplate() throws IOException {
279280
"object", org.elasticsearch.common.collect.Map.of("field1", "value1", "field2", "new_value", "field3", "value3"));
280281
assertThat(merged.getMeta(), equalTo(expected));
281282
}
283+
284+
public void testEmptyDocumentMapper() {
285+
MapperService mapperService = createMapperService(Version.CURRENT, Settings.EMPTY, () -> false);
286+
String type = randomAlphaOfLengthBetween(3, 6);
287+
DocumentMapper documentMapper = DocumentMapper.createEmpty(type, mapperService);
288+
assertEquals("{\"" + type + "\":{}}", Strings.toString(documentMapper.mapping()));
289+
assertTrue(documentMapper.mappers().hasMappings());
290+
assertNotNull(documentMapper.idFieldMapper());
291+
assertNotNull(documentMapper.sourceMapper());
292+
assertNotNull(documentMapper.IndexFieldMapper());
293+
assertEquals(10, documentMapper.mappers().getMapping().getMetadataMappersMap().size());
294+
assertEquals(10, documentMapper.mappers().fieldTypes().size());
295+
}
282296
}

server/src/test/java/org/elasticsearch/index/mapper/MappingLookupTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
1515
import org.elasticsearch.Version;
1616
import org.elasticsearch.common.Explicit;
17+
import org.elasticsearch.common.Strings;
1718
import org.elasticsearch.index.analysis.AnalyzerScope;
1819
import org.elasticsearch.index.analysis.NamedAnalyzer;
1920
import org.elasticsearch.index.query.SearchExecutionContext;
@@ -97,6 +98,16 @@ public void testAnalyzers() throws IOException {
9798
}).tokenStream("field3", "blah"));
9899
}
99100

101+
public void testEmptyMappingLookup() {
102+
MappingLookup mappingLookup = MappingLookup.EMPTY;
103+
assertEquals("{\"_doc\":{}}", Strings.toString(mappingLookup.getMapping()));
104+
assertFalse(mappingLookup.hasMappings());
105+
assertNull(mappingLookup.getMapping().getMeta());
106+
assertEquals(0, mappingLookup.getMapping().getMetadataMappersMap().size());
107+
assertFalse(mappingLookup.fieldMappers().iterator().hasNext());
108+
assertEquals(0, mappingLookup.fieldTypes().size());
109+
}
110+
100111
private void assertAnalyzes(Analyzer analyzer, String field, String output) throws IOException {
101112
try (TokenStream tok = analyzer.tokenStream(field, new StringReader(""))) {
102113
CharTermAttribute term = tok.addAttribute(CharTermAttribute.class);

x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/snapshots/SourceOnlySnapshotIT.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ public List<Setting<?>> getSettings() {
9999
}
100100
}
101101

102-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/pull/72695")
103102
public void testSnapshotAndRestore() throws Exception {
104103
final String sourceIdx = "test-idx";
105104
boolean requireRouting = randomBoolean();
@@ -130,7 +129,6 @@ public void testSnapshotAndRestore() throws Exception {
130129
assertHits(sourceIdx, builders.length, sourceHadDeletions);
131130
}
132131

133-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/pull/72695")
134132
public void testSnapshotAndRestoreWithNested() throws Exception {
135133
final String sourceIdx = "test-idx";
136134
boolean requireRouting = randomBoolean();
@@ -188,7 +186,7 @@ public void testSnapshotWithDanglingLocalSegment() throws Exception {
188186
assertSuccessful(startFullSnapshot(repo, "snapshot-3"));
189187
}
190188

191-
private void assertMappings(String sourceIdx, boolean requireRouting, boolean useNested) {
189+
private static void assertMappings(String sourceIdx, boolean requireRouting, boolean useNested) {
192190
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings(sourceIdx).get();
193191
ImmutableOpenMap<String, MappingMetadata> mapping = getMappingsResponse
194192
.getMappings().get(sourceIdx);

0 commit comments

Comments
 (0)