Hi,
We're trying to aggregate some data on a weekly basis, but we would like to adjust the calculation of weeks so that the weeks starts on Sundays instead.
We've tried using the offset()
operation from the DateHistogramValuesSourceBuilder to achieve this, when building CompositeAggregation.
But the query that gets generated always seems to strip/not include the offset field defined from the client, and the resulting buckets starts on Mondays.
Any ideas why we get this behaviour? Or suggestions for alternatives solutions to achieve the behaviour we want?
Client version: 7.6.2
Server version: 7.6.2
Example code below:
BoolQueryBuilder filterQuery = boolQuery()
.filter(termQuery("id.keyword", id))
.filter(rangeQuery("timestamp").gte(queryTimePeriod.getStartTime()).lt(queryTimePeriod.getEndTime()));
TermsValuesSourceBuilder versionTerm = new TermsValuesSourceBuilder("version").field("version.keyword");
TermsValuesSourceBuilder nameTerm = new TermsValuesSourceBuilder("name").field("name.keyword");
DateHistogramValuesSourceBuilder dateHistogram = new DateHistogramValuesSourceBuilder("period_start")
.field("timestamp")
.timeZone(ZoneOffset.UTC)
.format("yyyy-MM-dd'T'HH:mm:SS'Z'")
.offset(-86400000) // -1 day in ms
.calendarInterval(DateHistogramInterval.WEEK)
.order(SortOrder.DESC);
CompositeAggregationBuilder compositeAggregationBuilder = AggregationBuilders.composite("avg_over_time", List.of(idTerm, nameTerm, dateHistogram))
.subAggregation(AggregationBuilders.avg("cAvg").field("c"))
.size(MAX_RESULTS);
SearchSourceBuilder searchSource = new SearchSourceBuilder()
.aggregation(compositeAggregationBuilder)
.query(filterQuery)
.size(0);
SearchResponse response = client.search(new SearchRequest("indexToSearch").source(searchSource)