您好我正在尝试按照sql查询进行弹性搜索查询,我想使用Java API实现相同的逻辑
select mimetype,count(*) from table group by mimetype
现在我有以下Java代码,但它没有返回预期的输出
SearchResponse response = client.prepareSearch().setTypes("table")
.setQuery(matchAllQuery())
.addAggregation(terms("mimetype").field("mimetype").size(0).order(Terms.Order.count(false)))
.execute().actionGet();
Terms terms = response.getAggregations().get("mimetype");
Collection<Terms.Bucket> buckets = terms.getBuckets();
请指导我是Elastic search的新手。
修改 我想要密钥和doc_count下面的json -
"aggregations": {
"mimeTypeGroup": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "miscellaneous",
"doc_count": 4
},
{
"key": "application",
"doc_count": 1
},
{
"key": "audio",
"doc_count": 1
},
{
"key": "text",
"doc_count": 1
}
]
}
}
答案 0 :(得分:1)
你得到的是StringTerms.Bucket
key
存储在termBytes
中,您可以使用
bucketInstance.getKey()
并计算为bucketInstance.getDocCount()
答案 1 :(得分:0)
通过在上面添加此代码来获取密钥和文档计数 -
try {
pagerAdapterAds.notifyDataSetChanged();
NUM_PAGES = allAds.size();
final Handler handler2 = new Handler();
final Runnable Update2 = new Runnable() {
public void run() {
if (currentAdsPage == NUM_PAGES - 1) {
currentAdsPage = -1;
}
pagerAds.setCurrentItem(currentAdsPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler2.post(Update2);
}
}, 1000, 4000);
} catch (Exception e) {
e.printStackTrace();
}
}