Skip to content

Commit

Permalink
Improve DateFieldMapper ignore_malformed handling (#50090)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Christoph Büscher committed Dec 13, 2019
1 parent e6cbcf7 commit c0216f9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.elasticsearch.search.DocValueFormat;

import java.io.IOException;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
Expand All @@ -77,19 +78,23 @@ public static class Defaults {

public enum Resolution {
MILLISECONDS(CONTENT_TYPE, NumericType.DATE) {
@Override
public long convert(Instant instant) {
return instant.toEpochMilli();
}

@Override
public Instant toInstant(long value) {
return Instant.ofEpochMilli(value);
}
},
NANOSECONDS("date_nanos", NumericType.DATE_NANOSECONDS) {
@Override
public long convert(Instant instant) {
return toLong(instant);
}

@Override
public Instant toInstant(long value) {
return DateUtils.toInstant(value);
}
Expand Down Expand Up @@ -274,7 +279,9 @@ public MappedFieldType clone() {

@Override
public boolean equals(Object o) {
if (!super.equals(o)) return false;
if (!super.equals(o)) {
return false;
}
DateFieldType that = (DateFieldType) o;
return Objects.equals(dateTimeFormatter, that.dateTimeFormatter) && Objects.equals(resolution, that.resolution);
}
Expand Down Expand Up @@ -536,7 +543,7 @@ protected void parseCreateField(ParseContext context, List<IndexableField> field
long timestamp;
try {
timestamp = fieldType().parse(dateAsString);
} catch (IllegalArgumentException | ElasticsearchParseException e) {
} catch (IllegalArgumentException | ElasticsearchParseException | DateTimeException e) {
if (ignoreMalformed.value()) {
context.addIgnoredField(fieldType.name());
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,14 @@ public void testStore() throws Exception {
assertEquals(1457654400000L, storedField.numericValue().longValue());
}

public void testIgnoreMalformed() throws Exception {
public void testIgnoreMalformed() throws IOException {
testIgnoreMalfomedForValue("2016-03-99",
"failed to parse date field [2016-03-99] with format [strict_date_optional_time||epoch_millis]");
testIgnoreMalfomedForValue("-2147483648",
"Invalid value for Year (valid values -999999999 - 999999999): -2147483648");
}

private void testIgnoreMalfomedForValue(String value, String expectedException) throws IOException {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field").field("type", "date").endObject().endObject()
.endObject().endObject());
Expand All @@ -171,12 +178,11 @@ public void testIgnoreMalformed() throws Exception {
ThrowingRunnable runnable = () -> mapper.parse(new SourceToParse("test", "type", "1", BytesReference
.bytes(XContentFactory.jsonBuilder()
.startObject()
.field("field", "2016-03-99")
.field("field", value)
.endObject()),
XContentType.JSON));
MapperParsingException e = expectThrows(MapperParsingException.class, runnable);
assertThat(e.getCause().getMessage(),
containsString("failed to parse date field [2016-03-99] with format [strict_date_optional_time||epoch_millis]"));
assertThat(e.getCause().getMessage(), containsString(expectedException));

mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field").field("type", "date")
Expand All @@ -188,7 +194,7 @@ public void testIgnoreMalformed() throws Exception {
ParsedDocument doc = mapper2.parse(new SourceToParse("test", "type", "1", BytesReference
.bytes(XContentFactory.jsonBuilder()
.startObject()
.field("field", ":1")
.field("field", value)
.endObject()),
XContentType.JSON));

Expand Down

0 comments on commit c0216f9

Please sign in to comment.