title: Elasticsearch aggregation using Java api
body: <p>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</p>
  
  <pre><code>select dttime, avg(cpu) from table cpustats where server="X" and dttime="Y" group by dttime,cpu
  </code></pre>
  
  <p>Now I have the following Java code but it does not return expected output</p>
  
  <pre><code>SearchResponse response = client.prepareSearch("cpuindex")
        .setTypes("cputype")
        .setQuery(QueryBuilders.matchAllQuery())
        .addAggregation(AggregationBuilders.terms("cpu_agg")
             .field("cpu").size(100))
        .execute().actionGet();
  </code></pre>
  
  <p>Please guide I am new to Elastic search. Thanks in advance.</p>