From b1db5c43d6f970781a95822ea71ffe125b76986e Mon Sep 17 00:00:00 2001 From: kimchy Date: Mon, 15 Nov 2010 14:12:54 +0200 Subject: [PATCH] Mapper: `date` type to also allow providing values in numeric (milliseconds since epoch), closes #516. --- .../mapper/xcontent/DateFieldMapper.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/DateFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/DateFieldMapper.java index 3aac1b3127e..0b078b6a787 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/DateFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/DateFieldMapper.java @@ -131,7 +131,7 @@ public class DateFieldMapper extends NumberFieldMapper { } @Override public Long valueFromString(String value) { - return dateTimeFormatter.parser().parseMillis(value); + return parseStringValue(value); } /** @@ -155,15 +155,15 @@ public class DateFieldMapper extends NumberFieldMapper { @Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeQuery.newLongRange(names.indexName(), precisionStep, - lowerTerm == null ? null : dateTimeFormatter.parser().parseMillis(lowerTerm), - upperTerm == null ? null : dateTimeFormatter.parser().parseMillis(upperTerm), + lowerTerm == null ? null : parseStringValue(lowerTerm), + upperTerm == null ? null : parseStringValue(upperTerm), includeLower, includeUpper); } @Override public Filter rangeFilter(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeFilter.newLongRange(names.indexName(), precisionStep, - lowerTerm == null ? null : dateTimeFormatter.parser().parseMillis(lowerTerm), - upperTerm == null ? null : dateTimeFormatter.parser().parseMillis(upperTerm), + lowerTerm == null ? null : parseStringValue(lowerTerm), + upperTerm == null ? null : parseStringValue(upperTerm), includeLower, includeUpper); } @@ -189,7 +189,7 @@ public class DateFieldMapper extends NumberFieldMapper { context.allEntries().addText(names.fullName(), dateAsString, boost); } - long value = dateTimeFormatter.parser().parseMillis(dateAsString); + long value = parseStringValue(dateAsString); Field field = null; if (stored()) { field = new Field(names.indexName(), Numbers.longToBytes(value), store); @@ -248,4 +248,16 @@ public class DateFieldMapper extends NumberFieldMapper { builder.field("include_in_all", includeInAll); } } + + private long parseStringValue(String value) { + try { + return dateTimeFormatter.parser().parseMillis(value); + } catch (RuntimeException e) { + try { + return Long.parseLong(value); + } catch (NumberFormatException e1) { + throw new MapperParsingException("failed to parse date field, tried both date format [" + dateTimeFormatter.format() + "], and timestamp number", e); + } + } + } } \ No newline at end of file