gpt4 book ai didi

java - ElasticSearch 日期直方图聚合 填充缺失数据

转载 作者:行者123 更新时间:2023-11-30 03:13:27 24 4
gpt4 key购买 nike

我正在尝试使用 ElasticSearch spring 数据进行一些聚合

这是我的查询

final FilteredQueryBuilder filteredQuery = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.andFilter(FilterBuilders.termFilter("gender", "F"),
FilterBuilders.termFilter("place", "Arizona"),
FilterBuilders.rangeFilter("dob").from(from).to(to)));

final MetricsAggregationBuilder<?> aggregateArtifactcount = AggregationBuilders.sum("delivery")
.field("birth");

final AggregationBuilder<?> dailyDateHistogarm =
AggregationBuilders.dateHistogram(AggregationConstants.DAILY).field("dob")
.interval(DateHistogram.Interval.DAY).subAggregation(aggregateArtifactcount);

final SearchQuery query = new NativeSearchQueryBuilder().withIndices(index).withTypes(type)
.withQuery(filteredQuery).addAggregation(dailyDateHistogarm).build();

return elasticsearchTemplate.query(query, new DailyDeliveryAggregation());

这也是我的聚合

        public class DailyDeliveryAggregation implements ResultsExtractor<List<DailyDeliverySum>> {

@SuppressWarnings("unchecked")
@Override
public List<DailyDeliverySum> extract(final SearchResponse response) {
final List<DailyDeliverySum> dailyDeliverySum = new ArrayList<DailyDeliverySum>();
final Aggregations aggregations = response.getAggregations();
final DateHistogram daily = aggregations.get(AggregationConstants.DAILY);
final List<DateHistogram.Bucket> buckets = (List<DateHistogram.Bucket>) daily.getBuckets();
for (final DateHistogram.Bucket bucket : buckets) {
final Sum sum = (Sum) bucket.getAggregations().getAsMap().get("delivery");
final int deliverySum = (int) sum.getValue();
final int delivery = (int) bucket.getDocCount();
final String dateString = bucket.getKeyAsText().string();
dailyDeliverySum.add(new DailyDeliverySum(deliverySum, delivery, dateString));
}
return dailyDeliverySum;
}
}

它给了我正确的数据,但它并不能满足我的所有需求假设如果我查询 10 天的时间范围,如果给定时间范围内没有某个日期的数据,那么它会错过日期直方图存储桶中的该日期,但我想将 0 设置为聚合和文档计数的默认值(如果有)无可用数据

有什么办法可以做到吗?

最佳答案

是的,您可以使用"minimum document count" feature date_histogram 聚合的值并将其设置为 0。这样,您还将获得不包含任何数据的存储桶:

final AggregationBuilder<?> dailyDateHistogarm =
AggregationBuilders.dateHistogram(AggregationConstants.DAILY)
.field("dob")
.minDocCount(0) <--- add this line
.interval(DateHistogram.Interval.DAY)
.subAggregation(aggregateArtifactcount);

关于java - ElasticSearch 日期直方图聚合 填充缺失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33166789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com