Skip to content

Commit

Permalink
DocumentMapper to not implement ToXContent (#68653)
Browse files Browse the repository at this point in the history
DocumentMapper does not need to implement ToXContent, in fact it is its inner Mapping that needs to and already does. Consumers can switch to calling mapping() and toXContent against it.
  • Loading branch information
javanna committed Feb 8, 2021
1 parent 8d975d2 commit 0ca6819
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ public void testSearchAnalyzerSerialization() throws IOException {

XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
mapper.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("include_defaults", "true")));
mapper.mapping().toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("include_defaults", "true")));
builder.endObject();

String mappingString = Strings.toString(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.IndexAnalyzers;
import org.elasticsearch.index.mapper.MapperService.MergeReason;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Stream;

public class DocumentMapper implements ToXContentFragment {
public class DocumentMapper {
private final String type;
private final CompressedXContent mappingSource;
private final DocumentParser documentParser;
Expand Down Expand Up @@ -56,7 +53,7 @@ public DocumentMapper(RootObjectMapper.Builder rootBuilder, MapperService mapper
this.mappingLookup = MappingLookup.fromMapping(mapping, documentParser, indexSettings, indexAnalyzers);

try {
mappingSource = new CompressedXContent(this, XContentType.JSON, ToXContent.EMPTY_PARAMS);
mappingSource = new CompressedXContent(mapping, XContentType.JSON, ToXContent.EMPTY_PARAMS);
} catch (Exception e) {
throw new ElasticsearchGenerationException("failed to serialize source for type [" + type + "]", e);
}
Expand Down Expand Up @@ -155,9 +152,4 @@ public void validate(IndexSettings settings, boolean checkLimits) {
this.mappingLookup.checkLimits(settings);
}
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return mapping().toXContent(builder, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,9 @@ private void assertMappingVersion(
assert currentSource.equals(newSource) :
"expected current mapping [" + currentSource + "] for type [" + mapping.type() + "] "
+ "to be the same as new mapping [" + newSource + "]";
final CompressedXContent mapperSource = new CompressedXContent(Strings.toString(mapper));
assert currentSource.equals(mapperSource) :
assert currentSource.equals(mapper.mappingSource()) :
"expected current mapping [" + currentSource + "] for type [" + mapping.type() + "] "
+ "to be the same as new mapping [" + mapperSource + "]";
+ "to be the same as new mapping [" + mapper.mappingSource() + "]";
}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,10 @@ public void testObjectSerialization() throws IOException {
"\"is_interim\":{\"type\":\"boolean\"}}}}}}";

MapperService mapperService = createMapperService(mapping);
assertEquals(mapping, Strings.toString(mapperService.documentMapper()));
assertEquals(mapping, Strings.toString(mapperService.documentMapper().mapping()));

mapperService.merge("_doc", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
assertEquals(mapping, Strings.toString(mapperService.documentMapper()));
assertEquals(mapping, Strings.toString(mapperService.documentMapper().mapping()));
}

// test custom serializer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public void testBWCSerialization() throws IOException {

assertEquals(
"{\"_doc\":{\"properties\":{\"field\":{\"type\":\"text\",\"fields\":{\"subfield\":{\"type\":\"long\"}},\"fielddata\":true}}}}",
Strings.toString(mapperService.documentMapper()));
Strings.toString(mapperService.documentMapper().mapping()));
}

public void testEnableStore() throws IOException {
Expand Down Expand Up @@ -295,7 +295,7 @@ public void testIndexOptions() throws IOException {
mapping.endObject().endObject().endObject();

DocumentMapper mapper = createDocumentMapper(mapping);
String serialized = Strings.toString(mapper);
String serialized = Strings.toString(mapper.mapping());
assertThat(serialized, containsString("\"offsets\":{\"type\":\"text\",\"index_options\":\"offsets\"}"));
assertThat(serialized, containsString("\"freqs\":{\"type\":\"text\",\"index_options\":\"freqs\"}"));
assertThat(serialized, containsString("\"docs\":{\"type\":\"text\",\"index_options\":\"docs\"}"));
Expand Down Expand Up @@ -370,7 +370,7 @@ public void testSearchAnalyzerSerialization() throws IOException {

XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
createDocumentMapper(fieldMapping(this::minimalMapping)).toXContent(
createDocumentMapper(fieldMapping(this::minimalMapping)).mapping().toXContent(
builder,
new ToXContent.MapParams(Collections.singletonMap("include_defaults", "true"))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void testDateWithFormat() throws IOException {
MapperService mapperService = createMapperService(mapping.get());
MappedFieldType fieldType = mapperService.fieldType("field");
assertThat(fieldType, instanceOf(DateScriptFieldType.class));
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper()));
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper().mapping()));
}

public void testDateWithLocale() throws IOException {
Expand All @@ -105,7 +105,7 @@ public void testDateWithLocale() throws IOException {
MapperService mapperService = createMapperService(mapping.get());
MappedFieldType fieldType = mapperService.fieldType("field");
assertThat(fieldType, instanceOf(DateScriptFieldType.class));
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper()));
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper().mapping()));
}

public void testDateWithLocaleAndFormat() throws IOException {
Expand All @@ -116,7 +116,7 @@ public void testDateWithLocaleAndFormat() throws IOException {
MapperService mapperService = createMapperService(mapping.get());
MappedFieldType fieldType = mapperService.fieldType("field");
assertThat(fieldType, instanceOf(DateScriptFieldType.class));
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper()));
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper().mapping()));
}

public void testFormat() throws IOException {
Expand Down

0 comments on commit 0ca6819

Please sign in to comment.