使用 Java 的 ElasticSearch 聚合

ElasticSearch Aggregation using Java

我有一个弹性查询

{
  "size": 0,
  "aggs": {
    "group_by_cluster": {
      "terms": {
        "field": "cluster"
      }
    }
  }
}

http://localhost:9200/index/type/_search

我在尝试邮递员时得到了结果。现在正在尝试编写相应的 java 代码。 (弹性搜索版本 5.5)

SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withIndices("index")
.withTypes("type")
.addAggregation(AggregationBuilders
.global("agg")          
.subAggregation(AggregationBuilders.terms("group_by_cluster").field("cluster")))
.build();
 Aggregations aggregations = elasticTemplate.query(searchQuery, new 
 ResultsExtractor<Aggregations>() {
 @Override
 public Aggregations extract(SearchResponse response) {
 return response.getAggregations();
 }
 } );

但是结果是空的。请帮我解决这个问题

这是我的错误。类型区分大小写,我在原始代码中使用了驼峰式。