title: Aggregation for multiple fields in elasticsearch
body: <p>Is there any option in elasticsearch to use aggregation for multiple fields and get total count ?. 
  My query is </p>
  
  <pre><code>"SELECT COUNT(*), currency,type,status,channel  FROM temp_index WHERE country='SG'  and  received_time=now/d group by  currency,type,status,channel 
  </code></pre>
  
  <p>Trying to implement the above in Java code using RestHighLevelClient , any suggestions or assistance will be helpful.</p>
  
  <p>Currently we are using COUNT API</p>
  
  <pre><code>    List&lt;Object&gt; dashboardsDataTotal = new ArrayList&lt;&gt;();
  
          String[] channelList = { "test1", "test2", "test3", "test4", "test5", "test6" };
          String[] currencyList = { "SGD", "HKD", "USD", "INR", "IDR", "PHP", "CNY" };
          String[] statusList = { "COMPLETED", "FAILED", "PENDING", "FUTUREPROCESSINGDATE" };
          String[] paymentTypeList = { "type1", "type2" };
          String[] countryList = { "SG", "HK"};
  
  
          CountRequest countRequest = new CountRequest(INDEX);
          SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
          try {
              for (String country : countryAccess) { // per country
                  Map&lt;String, Object&gt; dashboardsDataPerCountry = new HashMap&lt;&gt;();
                  for (String channel : channelList) { // per channel
                      Map&lt;String, Object&gt; channelStore = new HashMap&lt;&gt;();
                      for (String paymentType : paymentTypeList) {
                          List&lt;Object&gt; paymentTypeStore = new ArrayList&lt;&gt;();
  
                          for (String currency : currencyList) {
                              Map&lt;String, Object&gt; currencyStore = new HashMap&lt;&gt;();
                              int receivedCount = 0;
                              for (String latestStatus : statusList) {
  
                                  BoolQueryBuilder searchBoolQuery = QueryBuilders.boolQuery();
                                  searchBoolQuery
                                          .must(QueryBuilders.termQuery("channel", channel.toLowerCase()));
                                  searchBoolQuery
                                          .must(QueryBuilders.termQuery("currency", currency.toLowerCase()));
                                  searchBoolQuery.must(QueryBuilders.matchPhraseQuery("source_country",
                                          country.toLowerCase()));
  
                                  if ("FUTUREPROCESSINGDATE".equalsIgnoreCase(latestStatus)) {
                                      searchBoolQuery.must(
                                              QueryBuilders.rangeQuery("processing_date").gt(currentDateS).timeZone(getTimeZone(country)));
                                  }
                                  else {
                                      searchBoolQuery.must(QueryBuilders.termQuery("txn_latest_status",
                                              latestStatus.toLowerCase()));
                                  }
                                  searchBoolQuery.must(
                                          QueryBuilders.termQuery("paymentType", paymentType.toLowerCase()));
  
  
                                  searchBoolQuery.must(QueryBuilders.rangeQuery("received_time").gte(currentDateS)
                                              .lte(currentDateS).timeZone(getTimeZone(country)));
  
  
                                  searchSourceBuilder.query(searchBoolQuery);
                                  countRequest.source(searchSourceBuilder);
  
  
                                  // try {
                                  CountResponse countResponse = restHighLevelClient.count(countRequest,
                                          RequestOptions.DEFAULT);
                                  if (!latestStatus.equals("FUTUREPROCESSINGDATE")) {
                                      receivedCount += countResponse.getCount();
                                  }
  
                                  currencyStore.put(latestStatus, countResponse.getCount());
  
                              }
                              currencyStore.put("RECEIVED", receivedCount); // received = pending + completed + failed
                              currencyStore.put("currency", currency);
                              paymentTypeStore.add(currencyStore);
  
                          } // per currency end
                          channelStore.put(paymentType, paymentTypeStore);
                      } // paymentType end
                      dashboardsDataPerCountry.put(channel, channelStore);
                      dashboardsDataPerCountry.put("country", country);
                  } // per channel end
                  dashboardsDataTotal.add(dashboardsDataPerCountry);
              } // per country end
              restHighLevelClient.close();
          }
  </code></pre>
  
  <p>Appreciate if someone can provide a better solution to the above.</p>