ingest: date processor should not fail if timestamp is specified as json number

Closes #26967
This commit is contained in:
Martijn van Groningen 2017-10-12 15:04:48 +02:00
parent 35984a616e
commit 141d1b62e9
No known key found for this signature in database
GPG Key ID: AB236F4FCF2AF12A
3 changed files with 19 additions and 3 deletions

View File

@ -63,7 +63,12 @@ public final class DateIndexNameProcessor extends AbstractProcessor {
@Override
public void execute(IngestDocument ingestDocument) throws Exception {
// Date can be specified as a string or long:
String date = Objects.toString(ingestDocument.getFieldValue(field, Object.class));
Object obj = ingestDocument.getFieldValue(field, Object.class);
String date = null;
if (obj != null) {
// Not use Objects.toString(...) here, because null gets changed to "null" which may confuse some date parsers
date = obj.toString();
}
DateTime dateTime = null;
Exception lastException = null;

View File

@ -30,10 +30,10 @@ import org.joda.time.DateTimeZone;
import org.joda.time.format.ISODateTimeFormat;
import java.util.ArrayList;
import java.util.IllformedLocaleException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
public final class DateProcessor extends AbstractProcessor {
@ -64,7 +64,12 @@ public final class DateProcessor extends AbstractProcessor {
@Override
public void execute(IngestDocument ingestDocument) {
String value = ingestDocument.getFieldValue(field, String.class);
Object obj = ingestDocument.getFieldValue(field, Object.class);
String value = null;
if (obj != null) {
// Not use Objects.toString(...) here, because null gets changed to "null" which may confuse some date parsers
value = obj.toString();
}
DateTime dateTime = null;
Exception lastException = null;

View File

@ -134,6 +134,12 @@ public class DateProcessorTests extends ESTestCase {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
dateProcessor.execute(ingestDocument);
assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("1970-01-01T00:16:40.500Z"));
document = new HashMap<>();
document.put("date_as_string", 1000500L);
ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
dateProcessor.execute(ingestDocument);
assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("1970-01-01T00:16:40.500Z"));
}
public void testUnix() {