Date Histogram Facet: NPE if using "1w" interval, closes #727.

This commit is contained in:
kimchy 2011-02-28 20:31:02 +02:00
parent d395c0a844
commit 47a6065805
1 changed files with 19 additions and 6 deletions

View File

@ -50,7 +50,7 @@ import java.util.Map;
public class DateHistogramFacetProcessor extends AbstractComponent implements FacetProcessor { public class DateHistogramFacetProcessor extends AbstractComponent implements FacetProcessor {
private final ImmutableMap<String, DateFieldParser> dateFieldParsers; private final ImmutableMap<String, DateFieldParser> dateFieldParsers;
private final TObjectIntHashMap<String> rounding = new TObjectIntHashMap<String>(Constants.DEFAULT_CAPACITY, Constants.DEFAULT_LOAD_FACTOR, MutableDateTime.ROUND_FLOOR); private final TObjectIntHashMap<String> rounding = new TObjectIntHashMap<String>(Constants.DEFAULT_CAPACITY, Constants.DEFAULT_LOAD_FACTOR, -1);
@Inject public DateHistogramFacetProcessor(Settings settings) { @Inject public DateHistogramFacetProcessor(Settings settings) {
super(settings); super(settings);
@ -164,15 +164,28 @@ public class DateHistogramFacetProcessor extends AbstractComponent implements Fa
int index = sInterval.indexOf(':'); int index = sInterval.indexOf(':');
if (index != -1) { if (index != -1) {
// set with rounding // set with rounding
DateTimeField field = dateFieldParsers.get(sInterval.substring(0, index)).parse(dateTime.getChronology()); DateFieldParser fieldParser = dateFieldParsers.get(sInterval.substring(0, index));
dateTime.setRounding(field, rounding.get(sInterval.substring(index + 1))); if (fieldParser == null) {
throw new FacetPhaseExecutionException(facetName, "failed to parse interval [" + sInterval + "] with custom rounding using built in intervals (year/month/...)");
}
DateTimeField field = fieldParser.parse(dateTime.getChronology());
int rounding = this.rounding.get(sInterval.substring(index + 1));
if (rounding == -1) {
throw new FacetPhaseExecutionException(facetName, "failed to parse interval [" + sInterval + "], rounding type [" + (sInterval.substring(index + 1)) + "] not found");
}
dateTime.setRounding(field, rounding);
} else { } else {
DateTimeField field = dateFieldParsers.get(sInterval).parse(dateTime.getChronology()); DateFieldParser fieldParser = dateFieldParsers.get(sInterval);
if (field != null) { if (fieldParser != null) {
DateTimeField field = fieldParser.parse(dateTime.getChronology());
dateTime.setRounding(field, MutableDateTime.ROUND_FLOOR); dateTime.setRounding(field, MutableDateTime.ROUND_FLOOR);
} else { } else {
// time interval // time interval
interval = TimeValue.parseTimeValue(parser.text(), null).millis(); try {
interval = TimeValue.parseTimeValue(parser.text(), null).millis();
} catch (Exception e) {
throw new FacetPhaseExecutionException(facetName, "failed to parse interval [" + sInterval + "], tried both as built in intervals (year/month/...) and as a time format");
}
} }
} }
} }