void assertBasicAggregationWorks(String indexName) {
    // histogram on a long
    SearchResponse searchRsp =
        client()
            .prepareSearch(indexName)
            .addAggregation(AggregationBuilders.histogram("histo").field("long_sort").interval(10))
            .get();
    ElasticsearchAssertions.assertSearchResponse(searchRsp);
    Histogram histo = searchRsp.getAggregations().get("histo");
    assertNotNull(histo);
    long totalCount = 0;
    for (Histogram.Bucket bucket : histo.getBuckets()) {
      totalCount += bucket.getDocCount();
    }
    assertEquals(totalCount, searchRsp.getHits().getTotalHits());

    // terms on a boolean
    searchRsp =
        client()
            .prepareSearch(indexName)
            .addAggregation(AggregationBuilders.terms("bool_terms").field("bool"))
            .get();
    Terms terms = searchRsp.getAggregations().get("bool_terms");
    totalCount = 0;
    for (Terms.Bucket bucket : terms.getBuckets()) {
      totalCount += bucket.getDocCount();
    }
    assertEquals(totalCount, searchRsp.getHits().getTotalHits());
  }
Esempio n. 2
0
 private HistogramBuilder histogram(MethodField field) throws SqlParseException {
   String aggName = gettAggNameFromParamsOrAlias(field);
   HistogramBuilder histogram = AggregationBuilders.histogram(aggName);
   String value = null;
   for (KVValue kv : field.getParams()) {
     value = kv.value.toString();
     switch (kv.key.toLowerCase()) {
       case "interval":
         histogram.interval(Long.parseLong(value));
         break;
       case "field":
         histogram.field(value);
         break;
       case "min_doc_count":
         histogram.minDocCount(Long.parseLong(value));
         break;
       case "extended_bounds":
         String[] bounds = value.split(":");
         if (bounds.length == 2)
           histogram.extendedBounds(Long.valueOf(bounds[0]), Long.valueOf(bounds[1]));
         break;
       case "alias":
       case "nested":
       case "reverse_nested":
         break;
       case "order":
         Histogram.Order order = null;
         switch (value) {
           case "key_desc":
             order = Histogram.Order.KEY_DESC;
             break;
           case "count_asc":
             order = Histogram.Order.COUNT_ASC;
             break;
           case "count_desc":
             order = Histogram.Order.COUNT_DESC;
             break;
           case "key_asc":
           default:
             order = Histogram.Order.KEY_ASC;
             break;
         }
         histogram.order(order);
         break;
       default:
         throw new SqlParseException("histogram err or not define field " + kv.toString());
     }
   }
   return histogram;
 }