# Condition in aggregation does not filter the hit result

**URL:** <https://discuss.elastic.co/t/condition-in-aggregation-does-not-filter-the-hit-result/201606>\
**Category:** Elasticsearch\
**Created:** [September 30, 2019, 9:22am UTC](https://discuss.elastic.co/t/condition-in-aggregation-does-not-filter-the-hit-result/201606 "2019-09-30T09:22:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quang\_D\_Tr\_n](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quang_d_tr_n/32/54722_2.png) [@Quang\_D\_Tr\_n](https://discuss.elastic.co/u/Quang_D_Tr_n)\
**Post date:** [September 30, 2019, 9:22am UTC](https://discuss.elastic.co/t/condition-in-aggregation-does-not-filter-the-hit-result/201606/1 "2019-09-30T09:22:53Z")

</div>

Hi guys.

I am trying to get all the record having the latest created time so I come up with this query:

```
{
  "query": {
  "term": {
    "username": "xxx"
  }
  },
  "aggs": {
    "sessions": {
      "terms" : { "field" : "id", "size": 1000 },
      "aggs": {
        "created_time": {
          "min": {
            "field": "created_time"
          }
        }
      }
    }
  }
} 

```

But the hit array still return two records with different created time, only the aggs section return the latest created time records.  
Is there any way to filter the hits with created time? Because I need to get all the fields in hit array missing in aggregation array.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 30, 2019, 12:16pm UTC](https://discuss.elastic.co/t/condition-in-aggregation-does-not-filter-the-hit-result/201606/2 "2019-09-30T12:16:09Z")

</div>

Take a look at the [top\_hits aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/search-aggregations-metrics-top-hits-aggregation.html)

However, I think another way to solve your problem would be to use [field collapsing](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/search-request-body.html#request-body-search-collapse) instead, where you sort by created time.

---

<div class="post-metadata">

**Author:** ![Quang\_D\_Tr\_n](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quang_d_tr_n/32/54722_2.png) [@Quang\_D\_Tr\_n](https://discuss.elastic.co/u/Quang_D_Tr_n)\
**Post date:** [October 7, 2019, 4:28am UTC](https://discuss.elastic.co/t/condition-in-aggregation-does-not-filter-the-hit-result/201606/3 "2019-10-07T04:28:50Z")

</div>

Thank you Alex.

Both top\_hits and field collapsing return expected result.

But I think top hits solution is more suitable for my situation because I need aggregation result as well.

Cheers.

---

<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:** [November 4, 2019, 4:28am UTC](https://discuss.elastic.co/t/condition-in-aggregation-does-not-filter-the-hit-result/201606/4 "2019-11-04T04:28:53Z")

</div>

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