# ElasticSearch aggregation get hit with min date

**URL:** <https://discuss.elastic.co/t/elasticsearch-aggregation-get-hit-with-min-date/58916>\
**Category:** Elasticsearch\
**Created:** [August 25, 2016, 10:27am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-get-hit-with-min-date/58916 "2016-08-25T10:27:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![xinevic](https://avatars.discourse-cdn.com/v4/letter/x/dc4da7/32.png) [@xinevic](https://discuss.elastic.co/u/xinevic)\
**Post date:** [August 25, 2016, 10:27am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-get-hit-with-min-date/58916/1 "2016-08-25T10:27:55Z")

</div>

How can I get hit with min date after aggregation? I have aggregation by some field, then `subAggregation(AggregationBuilders.min("createdDate")` with this date and then topHits subAggregation. But it returns me 3 hits, not 1 with this value.

---

<div class="post-metadata">

**Author:** ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)\
**Post date:** [August 25, 2016, 10:35am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-get-hit-with-min-date/58916/2 "2016-08-25T10:35:59Z")

</div>

You cannot define topHits as a subaggregation of "min". What you probably did was to specify min and topHits on the same level, which means that those two subaggregations are independent.

Please give a mapping and a query example when asking questions, otherwise it's hard to figure out what exactly you want to achieve.

---

<div class="post-metadata">

**Author:** ![xinevic](https://avatars.discourse-cdn.com/v4/letter/x/dc4da7/32.png) [@xinevic](https://discuss.elastic.co/u/xinevic)\
**Post date:** [August 25, 2016, 10:44am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-get-hit-with-min-date/58916/3 "2016-08-25T10:44:59Z")

</div>

Here is my template with mappings:

> <http://pastebin.com/raw/pwGczHf3>

I can't get query, because I use official Java ES Framework. I can provide only code.

`SearchResponse articlesAggregationResponse = transportClient.prepareSearch(articlesIndexAndType[0]).setTypes(articlesIndexAndType[1]) .setQuery(queryBuilder) .addAggregation(AggregationBuilders.terms("aggregation").field("userId").size(criteiraSize) .subAggregation(AggregationBuilders.count("articlesCount")) .subAggregation(AggregationBuilders.min("firstArticle").field("createdDate")) .subAggregation(AggregationBuilders.topHits("hit"))).execute().actionGet();`

---

<div class="post-metadata">

**Author:** ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)\
**Post date:** [August 25, 2016, 10:57am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-get-hit-with-min-date/58916/4 "2016-08-25T10:57:52Z")

</div>

what I explained before:

the topHits sub-aggregation is at the same nesting level as the min sub-aggregation in your code, and is thus computed independently, i.e., for each userId, the minimum createdDate and the top hits are calculated independently from each other. Nesting topHits below min aggregation is not possible.

What you actually want is to sort the topHits by `createdDate` and only return the first result:

```auto
.subAggregation(AggregationBuilders.topHits("hit").sort("createdDate").size(1))

```

---

<div class="post-metadata">

**Author:** ![xinevic](https://avatars.discourse-cdn.com/v4/letter/x/dc4da7/32.png) [@xinevic](https://discuss.elastic.co/u/xinevic)\
**Post date:** [August 25, 2016, 10:58am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-get-hit-with-min-date/58916/5 "2016-08-25T10:58:36Z")

</div>

Thanks. And how can I retrieve with max date? Sort in DESC order?

---

<div class="post-metadata">

**Author:** ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)\
**Post date:** [August 25, 2016, 10:59am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-get-hit-with-min-date/58916/6 "2016-08-25T10:59:07Z")

</div>

yes, use SortOrder.DESC if you want the max.

---

<div class="post-metadata">

**Author:** ![xinevic](https://avatars.discourse-cdn.com/v4/letter/x/dc4da7/32.png) [@xinevic](https://discuss.elastic.co/u/xinevic)\
**Post date:** [August 25, 2016, 10:59am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-get-hit-with-min-date/58916/7 "2016-08-25T10:59:23Z")

</div>

Thanks. How can I plus you reputation here? 🙂

---

<div class="post-metadata">

**Author:** ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)\
**Post date:** [August 25, 2016, 11:02am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-get-hit-with-min-date/58916/8 "2016-08-25T11:02:04Z")

</div>

some ❤ is all I need 😉

---

<div class="post-metadata">

**Author:** ![xinevic](https://avatars.discourse-cdn.com/v4/letter/x/dc4da7/32.png) [@xinevic](https://discuss.elastic.co/u/xinevic)\
**Post date:** [August 25, 2016, 11:02am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-get-hit-with-min-date/58916/9 "2016-08-25T11:02:53Z")

</div>

Ok. Have a nice day and thank you again. I spent too much time to solve this 🙂

---

<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, 10:25pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-get-hit-with-min-date/58916/10 "2017-07-05T22:25:16Z")

</div>


