![](/img/trans.png)
[英]Elasticsearch Java client SearchResponse not recognizing aggregations/buckets
[英]How to access Aggregations result with elasticSearch java api in SearchResponse?
有沒有辦法使用 java API 在搜索響應中檢索聚合的存儲桶?
{
"took" : 185,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 200,
"max_score" : 1.0,
"hits" : [...]
},
"aggregations" : {
"agg1" : {
"buckets" : [...]
},
"agg2" : {
"buckets" : [...]
}
}
}
目前,可以獲取聚合,但我不知道如何獲取存儲桶。
目前 ElasticSearch 的 1.0 版本(v1.0.0.Beta2)仍然是 beta 版本,也許這個功能還需要添加,但也沒有找到這方面的信息。
查看Github上的ES 源代碼,我在他們的測試中看到以下內容:
SearchResponse response = client().prepareSearch("idx").setTypes("type")
.setQuery(matchAllQuery())
.addAggregation(terms("keys").field("key").size(3).order(Terms.Order.count(false)))
.execute().actionGet();
Terms terms = response.getAggregations().get("keys");
Collection<Terms.Bucket> buckets = terms.getBuckets();
assertThat(buckets.size(), equalTo(3));
如果有人想知道從這些存儲桶中訪問實際文檔計數可能會有所幫助。
Terms terms = response.getAggregations().get("agg1");
Collection<Terms.Bucket> buckets = terms.getBuckets();
for (Bucket bucket : buckets) {
System.out.println(bucket.getKeyAsText() +" ("+bucket.getDocCount()+")");
}
如果有人正在尋找sum
聚合,就像將結果分配給org.elasticsearch.search.aggregations.metrics.sum.Sum
變量一樣簡單:
ElasticConnector connector = ... // initialize ES connector
SearchResponde response = connector.getClient()
.prepareSearch(...)
.setQuery(...)
.addAggregation(AggregationBuilders.sum("my_sum").field(...))
.get();
Sum sum = response.getAggregations().get("my_sum");
double result = sum.getValue();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.