Skip to content

Commit bd85f89

Browse files
committed
Ensure parameters are updated when merging flattened mappings.
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.
1 parent 33a7f06 commit bd85f89

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

docs/reference/mapping/types/flattened.asciidoc

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ The following mapping parameters are accepted:
140140

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

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

x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) {
525525
}
526526
}
527527

528-
private final FlatObjectFieldParser fieldParser;
528+
private FlatObjectFieldParser fieldParser;
529529
private int depthLimit;
530530
private int ignoreAbove;
531531

@@ -552,7 +552,12 @@ protected String contentType() {
552552
@Override
553553
protected void doMerge(Mapper mergeWith) {
554554
super.doMerge(mergeWith);
555-
this.ignoreAbove = ((FlatObjectFieldMapper) mergeWith).ignoreAbove;
555+
556+
FlatObjectFieldMapper other = ((FlatObjectFieldMapper) mergeWith);
557+
this.depthLimit = other.depthLimit;
558+
this.ignoreAbove = other.ignoreAbove;
559+
this.fieldParser = new FlatObjectFieldParser(fieldType.name(), keyedFieldName(),
560+
fieldType, depthLimit, ignoreAbove);
556561
}
557562

558563
@Override

x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapperTests.java

+42-6
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,12 @@ public void testFieldMultiplicity() throws Exception {
316316
}
317317

318318
public void testDepthLimit() throws IOException {
319-
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
319+
// First verify the default behavior when depth_limit is not set.
320+
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
320321
.startObject("type")
321322
.startObject("properties")
322323
.startObject("field")
323324
.field("type", "flattened")
324-
.field("depth_limit", 2)
325325
.endObject()
326326
.endObject()
327327
.endObject()
@@ -340,8 +340,25 @@ public void testDepthLimit() throws IOException {
340340
.endObject()
341341
.endObject());
342342

343+
mapper.parse(new SourceToParse("test", "1", doc, XContentType.JSON));
344+
345+
// Set a lower value for depth_limit and check that the field is rejected.
346+
String newMapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
347+
.startObject("type")
348+
.startObject("properties")
349+
.startObject("field")
350+
.field("type", "flattened")
351+
.field("depth_limit", 2)
352+
.endObject()
353+
.endObject()
354+
.endObject()
355+
.endObject());
356+
357+
DocumentMapper newMapper = mapper.merge(
358+
parser.parse("type", new CompressedXContent(newMapping)).mapping());
359+
343360
expectThrows(MapperParsingException.class, () ->
344-
mapper.parse(new SourceToParse("test", "1", doc, XContentType.JSON)));
361+
newMapper.parse(new SourceToParse("test", "1", doc, XContentType.JSON)));
345362
}
346363

347364
public void testEagerGlobalOrdinals() throws IOException {
@@ -362,12 +379,12 @@ public void testEagerGlobalOrdinals() throws IOException {
362379
}
363380

364381
public void testIgnoreAbove() throws IOException {
365-
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
382+
// First verify the default behavior when ignore_above is not set.
383+
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
366384
.startObject("type")
367385
.startObject("properties")
368386
.startObject("field")
369387
.field("type", "flattened")
370-
.field("ignore_above", 10)
371388
.endObject()
372389
.endObject()
373390
.endObject()
@@ -386,7 +403,26 @@ public void testIgnoreAbove() throws IOException {
386403

387404
ParsedDocument parsedDoc = mapper.parse(new SourceToParse("test", "1", doc, XContentType.JSON));
388405
IndexableField[] fields = parsedDoc.rootDoc().getFields("field");
389-
assertEquals(0, fields.length);
406+
assertEquals(2, fields.length);
407+
408+
// Set a lower value for ignore_above and check that the field is skipped.
409+
String newMapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
410+
.startObject("type")
411+
.startObject("properties")
412+
.startObject("field")
413+
.field("type", "flattened")
414+
.field("ignore_above", "10")
415+
.endObject()
416+
.endObject()
417+
.endObject()
418+
.endObject());
419+
420+
DocumentMapper newMapper = mapper.merge(
421+
parser.parse("type", new CompressedXContent(newMapping)).mapping());
422+
423+
ParsedDocument newParsedDoc = newMapper.parse(new SourceToParse("test", "1", doc, XContentType.JSON));
424+
IndexableField[] newFields = newParsedDoc.rootDoc().getFields("field");
425+
assertEquals(0, newFields.length);
390426
}
391427

392428
public void testNullValues() throws Exception {

0 commit comments

Comments
 (0)