# Search API using aggregation

**URL:** <https://discuss.elastic.co/t/search-api-using-aggregation/145541>\
**Category:** Elasticsearch\
**Created:** [August 22, 2018, 11:19am UTC](https://discuss.elastic.co/t/search-api-using-aggregation/145541 "2018-08-22T11:19:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raghuveer\_SJ](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Raghuveer\_SJ](https://discuss.elastic.co/u/Raghuveer_SJ)\
**Post date:** [August 22, 2018, 11:19am UTC](https://discuss.elastic.co/t/search-api-using-aggregation/145541/1 "2018-08-22T11:19:20Z")

</div>

I have started learning JAVA search APIs on elasticsearch 6.3.2 and want to be compatible for future use, i don't want to use `TransportClient`. I would like to run a query using aggregation something in SQL world would look like :

> Select count(logLevel) from table where logLevel="ERROR" and responseCode like "_500_" and timestamp between ("2018-8-11", "2018-8-12")

Actually I want a dynamic date range of 3 days from current to past 3 days.

using the aggregation code as below :

```
SearchSourceBuilder ssb = new SearchSourceBuilder();
        ssb.query(QueryBuilders.termQuery("responseCode", 500)); 
// only with this code the total hits I see is 0...!!
        TermsAggregationBuilder termAgg = AggregationBuilders.terms("ERROR")
                .field("logLevel.keyword");
        RangeAggregationBuilder rangeAgg = AggregationBuilders.range("dating").field("timestamp.keyword");
// how to put a dynamic date range here ?
        ssb.aggregation(rangeAgg);
        ssb.aggregation(termAgg);

```

I am not sure of what data format the responseCode is stored as. I want to get a hold of the queries before I go further. Kindly answer the comment section in the code. kindly suggest.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 19, 2018, 11:19am UTC](https://discuss.elastic.co/t/search-api-using-aggregation/145541/2 "2018-09-19T11:19:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
