Skip to content

Commit 51b73a7

Browse files
committed
Fix dynamic mapping update generation. (#27467)
When a field is not mapped, Elasticsearch tries to generate a mapping update from the parsed document. Some documents can introduce corner-cases, for instance in the event of a multi-valued field whose values would be mapped to different field types if they were supplied on their own, see for instance: ``` PUT index/doc/1 { "foo": ["2017-11-10T02:00:01.247Z","bar"] } ``` In that case, dynamic mappings want to map the first value as a `date` field and the second one as a `text` field. This currently throws an exception, which is expected, but the wrong one since it throws a `class_cast_exception` (which triggers a HTTP 5xx code) when it should throw an `illegal_argument_exception` (HTTP 4xx).
1 parent 0586763 commit 51b73a7

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

core/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,8 @@ protected void parseCreateField(ParseContext context, List<IndexableField> field
481481

482482
@Override
483483
protected void doMerge(Mapper mergeWith, boolean updateAllTypes) {
484-
final DateFieldMapper other = (DateFieldMapper) mergeWith;
485484
super.doMerge(mergeWith, updateAllTypes);
485+
final DateFieldMapper other = (DateFieldMapper) mergeWith;
486486
this.includeInAll = other.includeInAll;
487487
if (other.ignoreMalformed.explicit()) {
488488
this.ignoreMalformed = other.ignoreMalformed;

core/src/test/java/org/elasticsearch/index/mapper/DateFieldMapperTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,20 @@ public void testMergeDate() throws IOException {
395395
MapperService.MergeReason.MAPPING_UPDATE, randomBoolean()));
396396
assertThat(e.getMessage(), containsString("[mapper [release_date] has different [format] values]"));
397397
}
398+
399+
public void testMergeText() throws Exception {
400+
String mapping = XContentFactory.jsonBuilder().startObject().startObject("doc")
401+
.startObject("properties").startObject("date").field("type", "date").endObject()
402+
.endObject().endObject().endObject().string();
403+
DocumentMapper mapper = indexService.mapperService().parse("doc", new CompressedXContent(mapping), false);
404+
405+
String mappingUpdate = XContentFactory.jsonBuilder().startObject().startObject("doc")
406+
.startObject("properties").startObject("date").field("type", "text").endObject()
407+
.endObject().endObject().endObject().string();
408+
DocumentMapper update = indexService.mapperService().parse("doc", new CompressedXContent(mappingUpdate), false);
409+
410+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
411+
() -> mapper.merge(update.mapping(), randomBoolean()));
412+
assertEquals("mapper [date] of different type, current_type [date], merged_type [text]", e.getMessage());
413+
}
398414
}

0 commit comments

Comments
 (0)