Skip to content

Commit

Permalink
Ensure parameters are updated when merging flattened mappings. (elast…
Browse files Browse the repository at this point in the history
…ic#48971)

This PR makes the following two fixes around updating flattened fields:

* Make sure that the new value for ignore_above is immediately taken into
  affect. Previously we recorded the new value but did not use it when parsing
  documents.
* Allow depth_limit to be updated dynamically. It seems plausible that a user
  might want to tweak this setting as they encounter more data.
  • Loading branch information
jtibshirani committed Nov 12, 2019
1 parent baa5817 commit 7d500ba
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
3 changes: 2 additions & 1 deletion docs/reference/mapping/types/flattened.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ The following mapping parameters are accepted:

The maximum allowed depth of the flattened object field, in terms of nested
inner objects. If a flattened object field exceeds this limit, then an
error will be thrown. Defaults to `20`.
error will be thrown. Defaults to `20`. Note that `depth_limit` can be
updated dynamically through the <<indices-put-mapping, put mapping>> API.

<<doc-values,`doc_values`>>::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) {
}
}

private final FlatObjectFieldParser fieldParser;
private FlatObjectFieldParser fieldParser;
private int depthLimit;
private int ignoreAbove;

Expand All @@ -552,7 +552,12 @@ protected String contentType() {
@Override
protected void doMerge(Mapper mergeWith) {
super.doMerge(mergeWith);
this.ignoreAbove = ((FlatObjectFieldMapper) mergeWith).ignoreAbove;

FlatObjectFieldMapper other = ((FlatObjectFieldMapper) mergeWith);
this.depthLimit = other.depthLimit;
this.ignoreAbove = other.ignoreAbove;
this.fieldParser = new FlatObjectFieldParser(fieldType.name(), keyedFieldName(),
fieldType, depthLimit, ignoreAbove);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ public void testFieldMultiplicity() throws Exception {
}

public void testDepthLimit() throws IOException {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
// First verify the default behavior when depth_limit is not set.
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
.startObject("type")
.startObject("properties")
.startObject("field")
.field("type", "flattened")
.field("depth_limit", 2)
.endObject()
.endObject()
.endObject()
Expand All @@ -340,8 +340,25 @@ public void testDepthLimit() throws IOException {
.endObject()
.endObject());

mapper.parse(new SourceToParse("test", "1", doc, XContentType.JSON));

// Set a lower value for depth_limit and check that the field is rejected.
String newMapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
.startObject("type")
.startObject("properties")
.startObject("field")
.field("type", "flattened")
.field("depth_limit", 2)
.endObject()
.endObject()
.endObject()
.endObject());

DocumentMapper newMapper = mapper.merge(
parser.parse("type", new CompressedXContent(newMapping)).mapping());

expectThrows(MapperParsingException.class, () ->
mapper.parse(new SourceToParse("test", "1", doc, XContentType.JSON)));
newMapper.parse(new SourceToParse("test", "1", doc, XContentType.JSON)));
}

public void testEagerGlobalOrdinals() throws IOException {
Expand All @@ -362,12 +379,12 @@ public void testEagerGlobalOrdinals() throws IOException {
}

public void testIgnoreAbove() throws IOException {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
// First verify the default behavior when ignore_above is not set.
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
.startObject("type")
.startObject("properties")
.startObject("field")
.field("type", "flattened")
.field("ignore_above", 10)
.endObject()
.endObject()
.endObject()
Expand All @@ -386,7 +403,26 @@ public void testIgnoreAbove() throws IOException {

ParsedDocument parsedDoc = mapper.parse(new SourceToParse("test", "1", doc, XContentType.JSON));
IndexableField[] fields = parsedDoc.rootDoc().getFields("field");
assertEquals(0, fields.length);
assertEquals(2, fields.length);

// Set a lower value for ignore_above and check that the field is skipped.
String newMapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
.startObject("type")
.startObject("properties")
.startObject("field")
.field("type", "flattened")
.field("ignore_above", "10")
.endObject()
.endObject()
.endObject()
.endObject());

DocumentMapper newMapper = mapper.merge(
parser.parse("type", new CompressedXContent(newMapping)).mapping());

ParsedDocument newParsedDoc = newMapper.parse(new SourceToParse("test", "1", doc, XContentType.JSON));
IndexableField[] newFields = newParsedDoc.rootDoc().getFields("field");
assertEquals(0, newFields.length);
}

public void testNullValues() throws Exception {
Expand Down

0 comments on commit 7d500ba

Please sign in to comment.