Search code examples
elasticsearch-5elasticsearch-java-api

ElasticSearch Aggregation using Java


I have an elastic query as

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

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

I am getting the result when tried with postman. Now trying to write corresponding java code. (ElasticSearch version 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();
 }
 } );

But the result is empty. Please help me on this


Solution

  • This was my mistake. Type is case sensitive and I used camelcase in my original code.