Skip to content

Commit cdfcea1

Browse files
committed
Date Histogram Facet: NPE if using "1w" interval, closes elastic#727.
1 parent 0523ff8 commit cdfcea1

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/datehistogram/DateHistogramFacetProcessor.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
public class DateHistogramFacetProcessor extends AbstractComponent implements FacetProcessor {
5151

5252
private final ImmutableMap<String, DateFieldParser> dateFieldParsers;
53-
private final TObjectIntHashMap<String> rounding = new TObjectIntHashMap<String>(Constants.DEFAULT_CAPACITY, Constants.DEFAULT_LOAD_FACTOR, MutableDateTime.ROUND_FLOOR);
53+
private final TObjectIntHashMap<String> rounding = new TObjectIntHashMap<String>(Constants.DEFAULT_CAPACITY, Constants.DEFAULT_LOAD_FACTOR, -1);
5454

5555
@Inject public DateHistogramFacetProcessor(Settings settings) {
5656
super(settings);
@@ -164,15 +164,28 @@ public class DateHistogramFacetProcessor extends AbstractComponent implements Fa
164164
int index = sInterval.indexOf(':');
165165
if (index != -1) {
166166
// set with rounding
167-
DateTimeField field = dateFieldParsers.get(sInterval.substring(0, index)).parse(dateTime.getChronology());
168-
dateTime.setRounding(field, rounding.get(sInterval.substring(index + 1)));
167+
DateFieldParser fieldParser = dateFieldParsers.get(sInterval.substring(0, index));
168+
if (fieldParser == null) {
169+
throw new FacetPhaseExecutionException(facetName, "failed to parse interval [" + sInterval + "] with custom rounding using built in intervals (year/month/...)");
170+
}
171+
DateTimeField field = fieldParser.parse(dateTime.getChronology());
172+
int rounding = this.rounding.get(sInterval.substring(index + 1));
173+
if (rounding == -1) {
174+
throw new FacetPhaseExecutionException(facetName, "failed to parse interval [" + sInterval + "], rounding type [" + (sInterval.substring(index + 1)) + "] not found");
175+
}
176+
dateTime.setRounding(field, rounding);
169177
} else {
170-
DateTimeField field = dateFieldParsers.get(sInterval).parse(dateTime.getChronology());
171-
if (field != null) {
178+
DateFieldParser fieldParser = dateFieldParsers.get(sInterval);
179+
if (fieldParser != null) {
180+
DateTimeField field = fieldParser.parse(dateTime.getChronology());
172181
dateTime.setRounding(field, MutableDateTime.ROUND_FLOOR);
173182
} else {
174183
// time interval
175-
interval = TimeValue.parseTimeValue(parser.text(), null).millis();
184+
try {
185+
interval = TimeValue.parseTimeValue(parser.text(), null).millis();
186+
} catch (Exception e) {
187+
throw new FacetPhaseExecutionException(facetName, "failed to parse interval [" + sInterval + "], tried both as built in intervals (year/month/...) and as a time format");
188+
}
176189
}
177190
}
178191
}

0 commit comments

Comments
 (0)