Skip to content

Commit 9320edc

Browse files
committed
improve guessing if upper date range check works or is it a numeric
1 parent 166e4b0 commit 9320edc

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ protected long parseStringValue(String value) {
383383
long time = Long.parseLong(value);
384384
return timeUnit.toMillis(time);
385385
} catch (NumberFormatException e1) {
386-
throw new MapperParsingException("failed to parse date field, tried both date format [" + dateTimeFormatter.format() + "], and timestamp number", e);
386+
throw new MapperParsingException("failed to parse date field [" + value + "], tried both date format [" + dateTimeFormatter.format() + "], and timestamp number", e);
387387
}
388388
}
389389
}
@@ -394,14 +394,28 @@ protected long parseUpperInclusiveStringValue(String value) {
394394
}
395395
try {
396396
MutableDateTime dateTime = new MutableDateTime(3000, 12, 31, 23, 59, 59, 999, DateTimeZone.UTC);
397-
dateTimeFormatter.parser().parseInto(dateTime, value, 0);
397+
int location = dateTimeFormatter.parser().parseInto(dateTime, value, 0);
398+
// if we parsed all the string value, we are good
399+
if (location == value.length()) {
400+
return dateTime.getMillis();
401+
}
402+
// if we did not manage to parse, or the year is really high year which is unreasonable
403+
// see if its a number
404+
if (location <= 0 || dateTime.getYear() > 5000) {
405+
try {
406+
long time = Long.parseLong(value);
407+
return timeUnit.toMillis(time);
408+
} catch (NumberFormatException e1) {
409+
throw new MapperParsingException("failed to parse date field [" + value + "], tried both date format [" + dateTimeFormatter.format() + "], and timestamp number");
410+
}
411+
}
398412
return dateTime.getMillis();
399413
} catch (RuntimeException e) {
400414
try {
401415
long time = Long.parseLong(value);
402416
return timeUnit.toMillis(time);
403417
} catch (NumberFormatException e1) {
404-
throw new MapperParsingException("failed to parse date field, tried both date format [" + dateTimeFormatter.format() + "], and timestamp number", e);
418+
throw new MapperParsingException("failed to parse date field [" + value + "], tried both date format [" + dateTimeFormatter.format() + "], and timestamp number", e);
405419
}
406420
}
407421
}

0 commit comments

Comments
 (0)