Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix multiple year,quarter,month,week for DateHistogramAggregationBuil… #29266

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

package org.elasticsearch.search.aggregations.bucket.histogram;

import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
Expand Down Expand Up @@ -331,17 +330,38 @@ public String getType() {
return new DateHistogramAggregatorFactory(name, config, interval, dateHistogramInterval, offset, order, keyed, minDocCount,
rounding, roundedBounds, context, parent, subFactoriesBuilder, metaData);
}

/**Transform year/quarter/month/week interval to days interval
*/
private String transformFromDateUnitsToDateTimeUnits(String interval){
int multiplier = 1;
if (interval.endsWith("year") || interval.endsWith("y")){
multiplier = 365;
}else if (interval.endsWith("quarter") || interval.endsWith("q")) {
multiplier = 90;
}else if (interval.endsWith("month") || interval.endsWith("m")) {
multiplier = 30;
}else if (interval.endsWith("week") || interval.endsWith("w")){
multiplier = 7;
}else{
return interval;
}
Integer day_multiplier = Integer.parseInt(interval.replaceAll("\\D+","")) * multiplier;
return day_multiplier.toString()+"d";
}

private Rounding createRounding() {
Rounding.Builder tzRoundingBuilder;
if (dateHistogramInterval != null) {
// left this code for backward compatibility
DateTimeUnit dateTimeUnit = DATE_FIELD_UNITS.get(dateHistogramInterval.toString());
if (dateTimeUnit != null) {
tzRoundingBuilder = Rounding.builder(dateTimeUnit);
} else {
// the interval is a time value?
String dayed_interval = transformFromDateUnitsToDateTimeUnits(dateHistogramInterval.toString());
tzRoundingBuilder = Rounding.builder(
TimeValue.parseTimeValue(dateHistogramInterval.toString(), null, getClass().getSimpleName() + ".interval"));
TimeValue.parseTimeValue(dayed_interval, null, getClass().getSimpleName() + ".interval"));
}
} else {
// the interval is an integer time value in millis?
Expand Down