Give the date field mapping a "numeric_precision" argument that allows a user to configure the precision of the numeric timestamps.

Supports all the time units from http://download.oracle.com/javase/6/docs/api/java/util/concurrent/TimeUnit.html
This commit is contained in:
Njal Karevoll 2011-09-20 18:49:48 +02:00 committed by Shay Banon
parent 3f8b7f0fce
commit 41dbcdb7d6
1 changed files with 22 additions and 3 deletions

View File

@ -46,6 +46,7 @@ import org.elasticsearch.index.search.NumericRangeFieldDataFilter;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
import static org.elasticsearch.index.mapper.MapperBuilders.*; import static org.elasticsearch.index.mapper.MapperBuilders.*;
import static org.elasticsearch.index.mapper.core.TypeParsers.*; import static org.elasticsearch.index.mapper.core.TypeParsers.*;
@ -61,10 +62,14 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
public static final FormatDateTimeFormatter DATE_TIME_FORMATTER = Joda.forPattern("dateOptionalTime"); public static final FormatDateTimeFormatter DATE_TIME_FORMATTER = Joda.forPattern("dateOptionalTime");
public static final String NULL_VALUE = null; public static final String NULL_VALUE = null;
public static final TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS;
} }
public static class Builder extends NumberFieldMapper.Builder<Builder, DateFieldMapper> { public static class Builder extends NumberFieldMapper.Builder<Builder, DateFieldMapper> {
protected TimeUnit timeUnit = Defaults.TIME_UNIT;
protected String nullValue = Defaults.NULL_VALUE; protected String nullValue = Defaults.NULL_VALUE;
protected FormatDateTimeFormatter dateTimeFormatter = Defaults.DATE_TIME_FORMATTER; protected FormatDateTimeFormatter dateTimeFormatter = Defaults.DATE_TIME_FORMATTER;
@ -74,6 +79,11 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
builder = this; builder = this;
} }
public Builder timeUnit(TimeUnit timeUnit) {
this.timeUnit = timeUnit;
return this;
}
public Builder nullValue(String nullValue) { public Builder nullValue(String nullValue) {
this.nullValue = nullValue; this.nullValue = nullValue;
return this; return this;
@ -86,7 +96,7 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
@Override public DateFieldMapper build(BuilderContext context) { @Override public DateFieldMapper build(BuilderContext context) {
DateFieldMapper fieldMapper = new DateFieldMapper(buildNames(context), dateTimeFormatter, DateFieldMapper fieldMapper = new DateFieldMapper(buildNames(context), dateTimeFormatter,
precisionStep, fuzzyFactor, index, store, boost, omitNorms, omitTermFreqAndPositions, nullValue); precisionStep, fuzzyFactor, index, store, boost, omitNorms, omitTermFreqAndPositions, nullValue, timeUnit);
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
} }
@ -103,6 +113,8 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
builder.nullValue(propNode.toString()); builder.nullValue(propNode.toString());
} else if (propName.equals("format")) { } else if (propName.equals("format")) {
builder.dateTimeFormatter(parseDateTimeFormatter(propName, propNode)); builder.dateTimeFormatter(parseDateTimeFormatter(propName, propNode));
} else if (propName.equals("numeric_precision")) {
builder.timeUnit(TimeUnit.valueOf(propNode.toString().toUpperCase()));
} }
} }
return builder; return builder;
@ -113,15 +125,18 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
private String nullValue; private String nullValue;
private TimeUnit timeUnit;
protected DateFieldMapper(Names names, FormatDateTimeFormatter dateTimeFormatter, int precisionStep, String fuzzyFactor, protected DateFieldMapper(Names names, FormatDateTimeFormatter dateTimeFormatter, int precisionStep, String fuzzyFactor,
Field.Index index, Field.Store store, Field.Index index, Field.Store store,
float boost, boolean omitNorms, boolean omitTermFreqAndPositions, float boost, boolean omitNorms, boolean omitTermFreqAndPositions,
String nullValue) { String nullValue, TimeUnit timeUnit) {
super(names, precisionStep, fuzzyFactor, index, store, boost, omitNorms, omitTermFreqAndPositions, super(names, precisionStep, fuzzyFactor, index, store, boost, omitNorms, omitTermFreqAndPositions,
new NamedAnalyzer("_date/" + precisionStep, new NumericDateAnalyzer(precisionStep, dateTimeFormatter.parser())), new NamedAnalyzer("_date/" + precisionStep, new NumericDateAnalyzer(precisionStep, dateTimeFormatter.parser())),
new NamedAnalyzer("_date/max", new NumericDateAnalyzer(Integer.MAX_VALUE, dateTimeFormatter.parser()))); new NamedAnalyzer("_date/max", new NumericDateAnalyzer(Integer.MAX_VALUE, dateTimeFormatter.parser())));
this.dateTimeFormatter = dateTimeFormatter; this.dateTimeFormatter = dateTimeFormatter;
this.nullValue = nullValue; this.nullValue = nullValue;
this.timeUnit = timeUnit;
} }
@Override protected double parseFuzzyFactor(String fuzzyFactor) { @Override protected double parseFuzzyFactor(String fuzzyFactor) {
@ -265,7 +280,7 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
} }
if (value != null) { if (value != null) {
LongFieldMapper.CustomLongNumericField field = new LongFieldMapper.CustomLongNumericField(this, value); LongFieldMapper.CustomLongNumericField field = new LongFieldMapper.CustomLongNumericField(this, timeUnit.toMillis(value));
field.setBoost(boost); field.setBoost(boost);
return field; return field;
} }
@ -298,6 +313,7 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
} }
if (!mergeContext.mergeFlags().simulate()) { if (!mergeContext.mergeFlags().simulate()) {
this.nullValue = ((DateFieldMapper) mergeWith).nullValue; this.nullValue = ((DateFieldMapper) mergeWith).nullValue;
this.timeUnit = ((DateFieldMapper) mergeWith).timeUnit;
} }
} }
@ -331,6 +347,9 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
if (includeInAll != null) { if (includeInAll != null) {
builder.field("include_in_all", includeInAll); builder.field("include_in_all", includeInAll);
} }
if (timeUnit != Defaults.TIME_UNIT) {
builder.field("numeric_precision", timeUnit);
}
} }
private long parseStringValue(String value) { private long parseStringValue(String value) {