Skip to content

Commit

Permalink
Don't use user-supplied type when building DocumentMapper (#50960)
Browse files Browse the repository at this point in the history
This commit begins the process of removing types from the document parsing
infrastructure. Initially, we just ignore the user-supplied type after it has been
removed from the mapping json structure, and always supply _doc as the name
of the root parser.

The production code change is very small here, and most of the changeset
consists of alterations to Mapper test code that was passing in non-standard
type names and checking serialization.

Relates to #41059
  • Loading branch information
romseygeek authored Jan 14, 2020
1 parent 5809912 commit 774bfb5
Show file tree
Hide file tree
Showing 28 changed files with 336 additions and 367 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ static int getFrequency(TokenStream tk) throws IOException {
}

public void testDefaults() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "rank_feature").endObject().endObject()
.endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper = parser.parse("_doc", new CompressedXContent(mapping));

assertEquals(mapping, mapper.mappingSource().toString());

Expand Down Expand Up @@ -99,12 +99,12 @@ public void testDefaults() throws Exception {
}

public void testNegativeScoreImpact() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "rank_feature")
.field("positive_score_impact", false).endObject().endObject()
.endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper = parser.parse("_doc", new CompressedXContent(mapping));

assertEquals(mapping, mapper.mappingSource().toString());

Expand Down Expand Up @@ -135,12 +135,12 @@ public void testNegativeScoreImpact() throws Exception {
}

public void testRejectMultiValuedFields() throws MapperParsingException, IOException {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "rank_feature").endObject().startObject("foo")
.startObject("properties").startObject("field").field("type", "rank_feature").endObject().endObject()
.endObject().endObject().endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper = parser.parse("_doc", new CompressedXContent(mapping));

assertEquals(mapping, mapper.mappingSource().toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public void setup() {
protected Collection<Class<? extends Plugin>> getPlugins() {
return pluginList(MapperExtrasPlugin.class);
}

public void testBasics() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "rank_feature").endObject().endObject()
.endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper = parser.parse("_doc", new CompressedXContent(mapping));

assertEquals(mapping, mapper.mappingSource().toString());
assertNotNull(mapper.metadataMapper(RankFeatureMetaFieldMapper.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
}

public void testDefaults() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "rank_features").endObject().endObject()
.endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper = parser.parse("_doc", new CompressedXContent(mapping));

assertEquals(mapping, mapper.mappingSource().toString());

Expand Down Expand Up @@ -85,12 +85,12 @@ public void testDefaults() throws Exception {
}

public void testRejectMultiValuedFields() throws MapperParsingException, IOException {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "rank_features").endObject().startObject("foo")
.startObject("properties").startObject("field").field("type", "rank_features").endObject().endObject()
.endObject().endObject().endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper = parser.parse("_doc", new CompressedXContent(mapping));

assertEquals(mapping, mapper.mappingSource().toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
}

public void testDefaults() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "scaled_float")
.field("scaling_factor", 10.0).endObject().endObject()
.endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper = parser.parse("_doc", new CompressedXContent(mapping));

assertEquals(mapping, mapper.mappingSource().toString());

Expand Down Expand Up @@ -108,12 +108,12 @@ public void testIllegalScalingFactor() throws IOException {
}

public void testNotIndexed() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "scaled_float")
.field("index", false).field("scaling_factor", 10.0).endObject().endObject()
.endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper = parser.parse("_doc", new CompressedXContent(mapping));

assertEquals(mapping, mapper.mappingSource().toString());

Expand All @@ -132,12 +132,12 @@ public void testNotIndexed() throws Exception {
}

public void testNoDocValues() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "scaled_float")
.field("doc_values", false).field("scaling_factor", 10.0).endObject().endObject()
.endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper = parser.parse("_doc", new CompressedXContent(mapping));

assertEquals(mapping, mapper.mappingSource().toString());

Expand All @@ -156,12 +156,12 @@ public void testNoDocValues() throws Exception {
}

public void testStore() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "scaled_float")
.field("store", true).field("scaling_factor", 10.0).endObject().endObject()
.endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper = parser.parse("_doc", new CompressedXContent(mapping));

assertEquals(mapping, mapper.mappingSource().toString());

Expand All @@ -185,12 +185,12 @@ public void testStore() throws Exception {
}

public void testCoerce() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "scaled_float")
.field("scaling_factor", 10.0).endObject().endObject()
.endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper = parser.parse("_doc", new CompressedXContent(mapping));

assertEquals(mapping, mapper.mappingSource().toString());

Expand All @@ -209,12 +209,12 @@ public void testCoerce() throws Exception {
IndexableField dvField = fields[1];
assertEquals(DocValuesType.SORTED_NUMERIC, dvField.fieldType().docValuesType());

mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "scaled_float")
.field("scaling_factor", 10.0).field("coerce", false).endObject().endObject()
.endObject().endObject());

DocumentMapper mapper2 = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper2 = parser.parse("_doc", new CompressedXContent(mapping));

assertEquals(mapping, mapper2.mappingSource().toString());

Expand All @@ -238,12 +238,12 @@ public void testIgnoreMalformed() throws Exception {
}

private void doTestIgnoreMalformed(String value, String exceptionMessageContains) throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "scaled_float")
.field("scaling_factor", 10.0).endObject().endObject()
.endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper = parser.parse("_doc", new CompressedXContent(mapping));

assertEquals(mapping, mapper.mappingSource().toString());

Expand All @@ -256,12 +256,12 @@ private void doTestIgnoreMalformed(String value, String exceptionMessageContains
MapperParsingException e = expectThrows(MapperParsingException.class, runnable);
assertThat(e.getCause().getMessage(), containsString(exceptionMessageContains));

mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field").field("type", "scaled_float")
.field("scaling_factor", 10.0).field("ignore_malformed", true).endObject().endObject()
.endObject().endObject());

DocumentMapper mapper2 = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper2 = parser.parse("_doc", new CompressedXContent(mapping));

ParsedDocument doc = mapper2.parse(new SourceToParse("test", "1", BytesReference
.bytes(XContentFactory.jsonBuilder()
Expand All @@ -276,7 +276,7 @@ private void doTestIgnoreMalformed(String value, String exceptionMessageContains

public void testNullValue() throws IOException {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
.startObject("type")
.startObject("_doc")
.startObject("properties")
.startObject("field")
.field("type", "scaled_float")
Expand All @@ -285,7 +285,7 @@ public void testNullValue() throws IOException {
.endObject()
.endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
DocumentMapper mapper = parser.parse("_doc", new CompressedXContent(mapping));
assertEquals(mapping, mapper.mappingSource().toString());

ParsedDocument doc = mapper.parse(new SourceToParse("test", "1", BytesReference
Expand All @@ -297,7 +297,7 @@ public void testNullValue() throws IOException {
assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field"));

mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
.startObject("type")
.startObject("_doc")
.startObject("properties")
.startObject("field")
.field("type", "scaled_float")
Expand All @@ -307,7 +307,7 @@ public void testNullValue() throws IOException {
.endObject()
.endObject().endObject());

mapper = parser.parse("type", new CompressedXContent(mapping));
mapper = parser.parse("_doc", new CompressedXContent(mapping));
assertEquals(mapping, mapper.mappingSource().toString());

doc = mapper.parse(new SourceToParse("test", "1", BytesReference
Expand Down
Loading

0 comments on commit 774bfb5

Please sign in to comment.