# Date Histogram

**URL:** https://discuss.elastic.co/t/date-histogram/45090
**Category:** Elasticsearch
**Created:** [March 22, 2016, 10:31am UTC](https://discuss.elastic.co/t/date-histogram/45090 "2016-03-22T10:31:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sanju1323](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@sanju1323](https://discuss.elastic.co/u/sanju1323)
#### Post date: [March 22, 2016, 10:31am UTC](https://discuss.elastic.co/t/date-histogram/45090/1 "2016-03-22T10:31:58Z")

</div>

Hi,

I'm facing a problem while fetching data from Elasticsearch using Java API.  
Please have a look at the following index.

 ![](https://us1.discourse-cdn.com/elastic/original/2X/4/4c75da49cda7ff415a3d0d54e58b0ad0910c7b8d.png)

From the above index, I need year wise aggregation of the data.  
I'm using the following code to achieve that.

```
BoolQueryBuilder query = new BoolQueryBuilder();
query.must(QueryBuilders.termsQuery("customerId", customerIds));
query.must(QueryBuilders.termsQuery("productId", productIds));
SearchRequestBuilder req = con.getClient().prepareSearch("investigation_info_idx").setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(query);
req.setFetchSource(new String[] { "customerId", "productId", "testResult", "orderDate" }, null)
.addSort("orderDate", SortOrder.ASC).addAggregation(AggregationBuilders.dateHistogram("dateagg").field("orderDate").interval(DateHistogram.Interval.YEAR));
SearchResponse res = req.execute().actionGet();

```

for every year in OrderDate, I need only the first result of that year as I have selected Interval as Year.  
In this index, I'm expecting the result for testResult as 102,93,77 having orderDates ( 2010-04-20T18:30:00.000Z, 2011-04-22T18:30:00.000Z, 2012-04-24T18:30:00.000Z) respectively.

But I'm getting all the testResult values. Please correct me if I'm doing anything wrong in the code.  
Please help me in getting out of this issue.

Thanks & Regards,  
Sanjay Reddy

---

<div class="post-metadata">

### Author: ![sanju1323](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@sanju1323](https://discuss.elastic.co/u/sanju1323)
#### Post date: [April 5, 2016, 8:45am UTC](https://discuss.elastic.co/t/date-histogram/45090/2 "2016-04-05T08:45:19Z")

</div>

Can anyone please help me in this issue

---

<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: [July 5, 2017, 11:02pm UTC](https://discuss.elastic.co/t/date-histogram/45090/3 "2017-07-05T23:02:21Z")

</div>


