Skip to content

Commit 398ccaa

Browse files
author
Christoph Büscher
committed
Improve DateFieldMapper ignore_malformed handling (#50090)
A recent change around date parsing (#46675) made it stricter, so we should now also catch DateTimeExceptions in DateFieldMapper and ignore those when the `ignore_malformed` option is set. Closes #50081
1 parent 7b7fec0 commit 398ccaa

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.elasticsearch.search.DocValueFormat;
5555

5656
import java.io.IOException;
57+
import java.time.DateTimeException;
5758
import java.time.Instant;
5859
import java.time.ZoneId;
5960
import java.time.ZoneOffset;
@@ -77,19 +78,23 @@ public static class Defaults {
7778

7879
public enum Resolution {
7980
MILLISECONDS(CONTENT_TYPE, NumericType.DATE) {
81+
@Override
8082
public long convert(Instant instant) {
8183
return instant.toEpochMilli();
8284
}
8385

86+
@Override
8487
public Instant toInstant(long value) {
8588
return Instant.ofEpochMilli(value);
8689
}
8790
},
8891
NANOSECONDS("date_nanos", NumericType.DATE_NANOSECONDS) {
92+
@Override
8993
public long convert(Instant instant) {
9094
return toLong(instant);
9195
}
9296

97+
@Override
9398
public Instant toInstant(long value) {
9499
return DateUtils.toInstant(value);
95100
}
@@ -274,7 +279,9 @@ public MappedFieldType clone() {
274279

275280
@Override
276281
public boolean equals(Object o) {
277-
if (!super.equals(o)) return false;
282+
if (!super.equals(o)) {
283+
return false;
284+
}
278285
DateFieldType that = (DateFieldType) o;
279286
return Objects.equals(dateTimeFormatter, that.dateTimeFormatter) && Objects.equals(resolution, that.resolution);
280287
}
@@ -536,7 +543,7 @@ protected void parseCreateField(ParseContext context, List<IndexableField> field
536543
long timestamp;
537544
try {
538545
timestamp = fieldType().parse(dateAsString);
539-
} catch (IllegalArgumentException | ElasticsearchParseException e) {
546+
} catch (IllegalArgumentException | ElasticsearchParseException | DateTimeException e) {
540547
if (ignoreMalformed.value()) {
541548
context.addIgnoredField(fieldType.name());
542549
return;

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,14 @@ public void testStore() throws Exception {
159159
assertEquals(1457654400000L, storedField.numericValue().longValue());
160160
}
161161

162-
public void testIgnoreMalformed() throws Exception {
162+
public void testIgnoreMalformed() throws IOException {
163+
testIgnoreMalfomedForValue("2016-03-99",
164+
"failed to parse date field [2016-03-99] with format [strict_date_optional_time||epoch_millis]");
165+
testIgnoreMalfomedForValue("-2147483648",
166+
"Invalid value for Year (valid values -999999999 - 999999999): -2147483648");
167+
}
168+
169+
private void testIgnoreMalfomedForValue(String value, String expectedException) throws IOException {
163170
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
164171
.startObject("properties").startObject("field").field("type", "date").endObject().endObject()
165172
.endObject().endObject());
@@ -171,12 +178,11 @@ public void testIgnoreMalformed() throws Exception {
171178
ThrowingRunnable runnable = () -> mapper.parse(new SourceToParse("test", "type", "1", BytesReference
172179
.bytes(XContentFactory.jsonBuilder()
173180
.startObject()
174-
.field("field", "2016-03-99")
181+
.field("field", value)
175182
.endObject()),
176183
XContentType.JSON));
177184
MapperParsingException e = expectThrows(MapperParsingException.class, runnable);
178-
assertThat(e.getCause().getMessage(),
179-
containsString("failed to parse date field [2016-03-99] with format [strict_date_optional_time||epoch_millis]"));
185+
assertThat(e.getCause().getMessage(), containsString(expectedException));
180186

181187
mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
182188
.startObject("properties").startObject("field").field("type", "date")
@@ -188,7 +194,7 @@ public void testIgnoreMalformed() throws Exception {
188194
ParsedDocument doc = mapper2.parse(new SourceToParse("test", "type", "1", BytesReference
189195
.bytes(XContentFactory.jsonBuilder()
190196
.startObject()
191-
.field("field", ":1")
197+
.field("field", value)
192198
.endObject()),
193199
XContentType.JSON));
194200

0 commit comments

Comments
 (0)