弹性搜索:无法获得存储桶结果
Elastic search : Impossible to get buckets result
我每天按 apkId 计算安装数量。
LocalDate today = LocalDate.now();
LocalDate beginningDate = today.minusDays(intervalle);
BoolQueryBuilder query = QueryBuilders.boolQuery();
query.must(QueryBuilders.rangeQuery("date")
.gte(convertToDateViaInstant(beginningDate))
.lte(convertToDateViaInstant(today)));
query.must(QueryBuilders.matchQuery("apkId", apkId));
query.must(QueryBuilders.matchQuery("event", Event.INSTALL));
Iterable<ApkHistory> list = apkHistoryRepo.search(query);
AggregationBuilder aggregation = AggregationBuilders
.dateHistogram("nb_install_per_day")
.field("date")
.dateHistogramInterval(DateHistogramInterval.DAY);
SearchRequest searchRequest = new SearchRequest("apkhistory");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(query).aggregation(aggregation);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregation agg = searchResponse.getAggregations().get("nb_install_per_day");
System.out.println("test");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
搜索响应 return 正确 json,我可以在其中看到我正在寻找的结果。
{
"took":70,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":34,
"max_score":2.0042462,
"hits":[
{
"_index":"apkhistory",
"_type":"apkhistory",
"_id":"mQw_OnYBj8D21YQgTRHA",
"_score":2.0042462,
"_source":{
"appUserId":"rsty-45ts-8978HU9",
"apkId":1,
"event":"INSTALL",
"date":1605481200000
}
},
{
"_index":"apkhistory",
"_type":"apkhistory",
"_id":"-wwPOnYBj8D21YQgdA9Q",
"_score":2.0042462,
"_source":{
"appUserId":"id-test-app-user95",
"apkId":1,
"event":"INSTALL",
"date":1605654000000
}
}...
}
]
},
"aggregations":{
"date_histogram#per_day_count":{
"buckets":[
{
"key_as_string":"2020-11-07T00:00:00.000Z",
"key":1604707200000,
"doc_count":2
},
{
"key_as_string":"2020-11-08T00:00:00.000Z",
"key":1604793600000,
"doc_count":2
}...
]
}
}
}
当我试图拿到水桶时,这是不可能的。我在聚合中没有 getBuckets()
方法。
当我尝试 Terms terms = searchResponse.getAggregations().get("nb_install_per_day")
时,出现异常,cannot convert Aggregations to Terms
拜托,我该怎么做才能得到这个桶值?
我使用 Spring 2.2.4
和 spring data elastic search 3.2.4.RELEASE
.
谢谢
这是我的变量的内容。在 agg 中,我没有方法获取 buckets ArrayList ...我尝试了很多方法都没有成功。请帮忙。
今天本地日期 = LocalDate.now();
LocalDate beginningDate = today.minusDays(intervalle);
BoolQueryBuilder query = QueryBuilders.boolQuery();
query.must(QueryBuilders.rangeQuery("date")
.gte(beginningDate)
.lte(today));
query.must(QueryBuilders.matchQuery("apkId", apkId));
query.must(QueryBuilders.matchQuery("event", Event.INSTALL));
Iterable<ApkHistory> list = apkHistoryRepo.search(query);
AggregationBuilder aggregation = AggregationBuilders
.dateHistogram("nb_install_per_day")
.field("date")
.dateHistogramInterval(DateHistogramInterval.DAY);
SearchRequest searchRequest = new SearchRequest("apkhistory");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(query).aggregation(aggregation);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
ParsedDateHistogram dateGroupBy = searchResponse.getAggregations().get("nb_install_per_day");
for(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket b : dateGroupBy.getBuckets()) {
System.out.println(b.getKeyAsString() + " "+b.getDocCount());
}
System.out.println("test");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我每天按 apkId 计算安装数量。
LocalDate today = LocalDate.now();
LocalDate beginningDate = today.minusDays(intervalle);
BoolQueryBuilder query = QueryBuilders.boolQuery();
query.must(QueryBuilders.rangeQuery("date")
.gte(convertToDateViaInstant(beginningDate))
.lte(convertToDateViaInstant(today)));
query.must(QueryBuilders.matchQuery("apkId", apkId));
query.must(QueryBuilders.matchQuery("event", Event.INSTALL));
Iterable<ApkHistory> list = apkHistoryRepo.search(query);
AggregationBuilder aggregation = AggregationBuilders
.dateHistogram("nb_install_per_day")
.field("date")
.dateHistogramInterval(DateHistogramInterval.DAY);
SearchRequest searchRequest = new SearchRequest("apkhistory");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(query).aggregation(aggregation);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregation agg = searchResponse.getAggregations().get("nb_install_per_day");
System.out.println("test");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
搜索响应 return 正确 json,我可以在其中看到我正在寻找的结果。
{
"took":70,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":34,
"max_score":2.0042462,
"hits":[
{
"_index":"apkhistory",
"_type":"apkhistory",
"_id":"mQw_OnYBj8D21YQgTRHA",
"_score":2.0042462,
"_source":{
"appUserId":"rsty-45ts-8978HU9",
"apkId":1,
"event":"INSTALL",
"date":1605481200000
}
},
{
"_index":"apkhistory",
"_type":"apkhistory",
"_id":"-wwPOnYBj8D21YQgdA9Q",
"_score":2.0042462,
"_source":{
"appUserId":"id-test-app-user95",
"apkId":1,
"event":"INSTALL",
"date":1605654000000
}
}...
}
]
},
"aggregations":{
"date_histogram#per_day_count":{
"buckets":[
{
"key_as_string":"2020-11-07T00:00:00.000Z",
"key":1604707200000,
"doc_count":2
},
{
"key_as_string":"2020-11-08T00:00:00.000Z",
"key":1604793600000,
"doc_count":2
}...
]
}
} }
当我试图拿到水桶时,这是不可能的。我在聚合中没有 getBuckets()
方法。
当我尝试 Terms terms = searchResponse.getAggregations().get("nb_install_per_day")
时,出现异常,cannot convert Aggregations to Terms
拜托,我该怎么做才能得到这个桶值?
我使用 Spring 2.2.4
和 spring data elastic search 3.2.4.RELEASE
.
谢谢
这是我的变量的内容。在 agg 中,我没有方法获取 buckets ArrayList ...我尝试了很多方法都没有成功。请帮忙。
今天本地日期 = LocalDate.now();
LocalDate beginningDate = today.minusDays(intervalle);
BoolQueryBuilder query = QueryBuilders.boolQuery();
query.must(QueryBuilders.rangeQuery("date")
.gte(beginningDate)
.lte(today));
query.must(QueryBuilders.matchQuery("apkId", apkId));
query.must(QueryBuilders.matchQuery("event", Event.INSTALL));
Iterable<ApkHistory> list = apkHistoryRepo.search(query);
AggregationBuilder aggregation = AggregationBuilders
.dateHistogram("nb_install_per_day")
.field("date")
.dateHistogramInterval(DateHistogramInterval.DAY);
SearchRequest searchRequest = new SearchRequest("apkhistory");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(query).aggregation(aggregation);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
ParsedDateHistogram dateGroupBy = searchResponse.getAggregations().get("nb_install_per_day");
for(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket b : dateGroupBy.getBuckets()) {
System.out.println(b.getKeyAsString() + " "+b.getDocCount());
}
System.out.println("test");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}