用于弹性搜索中所有索引的java中的弹性搜索聚合 [英] elastic search aggregation in java for all indexes in elastic search
问题描述
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)))
。执行()actionGet();
条款术语= response.getAggregations()。get(mimetype);
集合< Terms.Bucket> buckets = terms.getBuckets();
请指导我是弹性搜索的新手。
编辑
我想要键和doc_count在下面的json -
聚合:{
mimeTypeGroup:{
doc_count_error_upper_bound:0,
sum_other_doc_count:0,
buckets:[
{
key:杂项,
doc_count:4
},
{
key:application,
doc_count
},
{
key:audio,
doc_count:1
},
{
key :text,
doc_count:1
}
]
}
}
但是我只收到文档计数java中的关键名称 -
StringTerms.Bucket
存储键
在 termBytes
中,您可以使用
bucketInstance.getKey()
并计数像 bucketInstance.getDocCount()
Hi I am trying to do query on elastic search by following the sql query and I want to implement same logic using Java API
select mimetype,count(*) from table group by mimetype
Now I have the following Java code but it does not return expected output
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();
Please guide I am new to Elastic search.
EDIT I want key and the doc_count for below 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
}
]
}
}
But I get only doc count not the key name in java -
What you've got is an instance of StringTerms.Bucket
The key
is stored in termBytes
and you can retrieve it using
bucketInstance.getKey()
and count like bucketInstance.getDocCount()
这篇关于用于弹性搜索中所有索引的java中的弹性搜索聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!