Build ElasticSearch query in Java

86 Views Asked by At

I have following ElasticSearch query to fetch the data from ElasticSearch:

GET volumestatistics/_search
{
  "size": 0,
  "aggs": {
    "customer": {
      "terms": {
        "field": "cust_code",
        "size": 100
      },
      "aggs": {
        "day": {
          "date_histogram": {
            "field": "ts",
            "interval": "day",
            "format": "MM-dd-yyyy",
            "order": {
              "total_bytes": "desc"
            }
          },
          "aggs": {
            "total_bytes": {
              "sum": {
                "field": "events_size_in_bytes"
              }
            }
          }
        },
        "min_bytes_metrics": {
          "min_bucket": {
            "buckets_path": "day>total_bytes"
          }
        },
        "max_bytes_metrics": {
          "max_bucket": {
            "buckets_path": "day>total_bytes"
          }
        },
        "sum_bytes_metrics": {
          "sum_bucket": {
            "buckets_path": "day>total_bytes"
          }
        }
      }
    }
  }
}

This is working fine to fetch the data from elasticsearch cluster.

I want to build the above query in java in following method:

private AggregationBuilder<?> buildAggregationQuery()
{
    AggregationBuilder<?> customerWiseAgg = AggregationBuilders.terms("customer").field("cust_code").size(100);
    return customerWiseAgg;
}

Please help to build the complete query in java

0

There are 0 best solutions below