Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve DateFieldMapper ignore_malformed handling #50090

Merged
merged 1 commit into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", "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", "1", BytesReference
.bytes(XContentFactory.jsonBuilder()
.startObject()
.field("field", ":1")
.field("field", value)
.endObject()),
XContentType.JSON));

Expand Down