Mapper: `date` type to also allow providing values in numeric (milliseconds since epoch), closes #516.

This commit is contained in:
kimchy 2010-11-15 14:12:54 +02:00
parent 6d214d69b9
commit b1db5c43d6
1 changed files with 18 additions and 6 deletions

View File

@ -131,7 +131,7 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
}
@Override public Long valueFromString(String value) {
return dateTimeFormatter.parser().parseMillis(value);
return parseStringValue(value);
}
/**
@ -155,15 +155,15 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
@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<Long> {
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<Long> {
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);
}
}
}
}