public SearchResult(SearchResponse resp, Select select) throws SqlParseException {
   Aggregations aggs = resp.getAggregations();
   if (aggs.get("filter") != null) {
     InternalFilter inf = aggs.get("filter");
     aggs = inf.getAggregations();
   }
   if (aggs.get("group by") != null) {
     InternalTerms terms = aggs.get("group by");
     Collection<Bucket> buckets = terms.getBuckets();
     this.total = buckets.size();
     results = new ArrayList<>(buckets.size());
     for (Bucket bucket : buckets) {
       Map<String, Object> aggsMap = toAggsMap(bucket.getAggregations().getAsMap());
       aggsMap.put("docCount", bucket.getDocCount());
       results.add(aggsMap);
     }
   } else {
     results = new ArrayList<>(1);
     this.total = 1;
     Map<String, Object> map = new HashMap<>();
     for (Aggregation aggregation : aggs) {
       map.put(aggregation.getName(), covenValue(aggregation));
     }
     results.add(map);
   }
 }
  @Test
  public void reverseAnotherNestedGroupByOnNestedFieldWithFilterTestWithReverseNestedNoPath()
      throws Exception {
    Aggregations result =
        query(
            String.format(
                "SELECT COUNT(*) FROM %s/nestedType GROUP BY  nested(message.info),filter('myFilter',message.info = 'a'),reverse_nested(comment.data,'~comment')",
                TEST_INDEX));
    InternalNested nested = result.get("message.info@NESTED");
    InternalFilter filter = nested.getAggregations().get("myFilter@FILTER");
    Terms infos = filter.getAggregations().get("message.info");
    Assert.assertEquals(1, infos.getBuckets().size());
    for (Terms.Bucket bucket : infos.getBuckets()) {
      InternalReverseNested reverseNested =
          bucket.getAggregations().get("comment.data@NESTED_REVERSED");
      InternalNested innerNested = reverseNested.getAggregations().get("comment.data@NESTED");
      Terms terms = innerNested.getAggregations().get("comment.data");
      Terms.Bucket internalBucket = terms.getBuckets().get(0);

      long count = ((ValueCount) internalBucket.getAggregations().get("COUNT(*)")).getValue();
      String key = internalBucket.getKey();
      if (key.equalsIgnoreCase("ab")) {
        Assert.assertEquals(2, count);
      } else {
        throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key));
      }
    }
  }
 @Test
 public void reverseToRootGroupByOnNestedFieldWithFilterAndSumOnReverseNestedField()
     throws Exception {
   Aggregations result =
       query(
           String.format(
               "SELECT sum(reverse_nested(myNum)) bla FROM %s/nestedType GROUP BY  nested(message.info),filter('myFilter',message.info = 'a')",
               TEST_INDEX));
   InternalNested nested = result.get("message.info@NESTED");
   InternalFilter filter = nested.getAggregations().get("myFilter@FILTER");
   Terms infos = filter.getAggregations().get("message.info");
   Assert.assertEquals(1, infos.getBuckets().size());
   for (Terms.Bucket bucket : infos.getBuckets()) {
     InternalReverseNested reverseNested = bucket.getAggregations().get("myNum@NESTED");
     InternalSum sum = reverseNested.getAggregations().get("bla");
     Assert.assertEquals(5.0, sum.getValue(), 0.000001);
   }
 }
 @Test
 public void reverseToRootGroupByOnNestedFieldWithFilterTestWithReverseNestedOnHistogram()
     throws Exception {
   Aggregations result =
       query(
           String.format(
               "SELECT COUNT(*) FROM %s/nestedType GROUP BY  nested(message.info),filter('myFilter',message.info = 'a'),histogram('field'='myNum','reverse_nested'='','interval'='2' , 'alias' = 'someAlias' )",
               TEST_INDEX));
   InternalNested nested = result.get("message.info@NESTED");
   InternalFilter filter = nested.getAggregations().get("myFilter@FILTER");
   Terms infos = filter.getAggregations().get("message.info");
   Assert.assertEquals(1, infos.getBuckets().size());
   for (Terms.Bucket bucket : infos.getBuckets()) {
     InternalReverseNested reverseNested = bucket.getAggregations().get("someAlias@NESTED");
     InternalHistogram histogram = reverseNested.getAggregations().get("someAlias");
     Assert.assertEquals(2, histogram.getBuckets().size());
   }
 }
  @Test
  public void groupByTestWithFilter() throws Exception {
    Aggregations result =
        query(
            String.format(
                "SELECT COUNT(*) FROM %s/account GROUP BY filter(gender='m'),gender", TEST_INDEX));
    InternalFilter filter = result.get("filter(gender = 'm')@FILTER");
    Terms gender = filter.getAggregations().get("gender");

    for (Terms.Bucket bucket : gender.getBuckets()) {
      String key = bucket.getKey();
      long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
      if (key.equalsIgnoreCase("m")) {
        Assert.assertEquals(507, count);
      } else {
        throw new Exception(String.format("Unexpected key. expected: only m. found: %s", key));
      }
    }
  }
 @Test
 public void groupByOnNestedFieldWithFilterTest() throws Exception {
   Aggregations result =
       query(
           String.format(
               "SELECT COUNT(*) FROM %s/nestedType GROUP BY  nested(message.info),filter('myFilter',message.info = 'a')",
               TEST_INDEX));
   InternalNested nested = result.get("message.info@NESTED");
   InternalFilter filter = nested.getAggregations().get("myFilter@FILTER");
   Terms infos = filter.getAggregations().get("message.info");
   Assert.assertEquals(1, infos.getBuckets().size());
   for (Terms.Bucket bucket : infos.getBuckets()) {
     String key = bucket.getKey();
     long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
     if (key.equalsIgnoreCase("a")) {
       Assert.assertEquals(2, count);
     } else {
       throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key));
     }
   }
 }