我想做这样的事情:
SELECT DISTINCT(CATEG) FROM TABLE;
我试过了:
{
"fields" : ["category"],
"query": {
"match_all": {}
},"aggs":{
"by_category": {
"terms": {
"field": "category"
}
}
}
}
但不能工作。
在我的java代码中,我做
ElasticSearchInstance instance = ElasticSearchInstance.getInstance();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("{\"fields\" : [\"category\"],"
+ "\"size\":0,\"aggs\":{ "
+ " \"by_category\": { "
+ " \"terms\": { "
+ " \"field\": \"category\",\"size\":0 "
+ " } } } "
+ "}");
SearchResult searchResult = instance.execute("String", stringBuilder.toString());
我的searchResult是:
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":0.0,"hits":[]},"aggregations":{"by_category":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[]}}}
我的结果应该是"蓝调"和"其他"。
有什么想法吗?
答案 0 :(得分:0)
这可以帮到你
SearchRequestBuilder srb = ClientInstance.getInstance()。getClient()。prepareSearch();
srb.setQuery(QueryBuilders.matchAllQuery())addAggregation(AggregationBuilders.terms(" BY_CATEGORY")字段("类别&#34)的尺寸(0).order(条款。 .Order.term(真))
答案 1 :(得分:0)
最后,我发现了它。
我的查询是:
{
"size": 0,
"aggs": {
"category": {
"terms": {
"field": "category"
}
}
}
}
在我的java代码中,我使用:
Search.Builder searchBuilder = new Search.Builder(stringBuilder.toString());
SearchResult searchResult = instance.execute(searchBuilder);
List<Entry> res = searchResult.getAggregations().getTermsAggregation("category").getBuckets();
我在我的列表上循环以获取密钥!!