# Applying post\_filter after top\_hits aggregation not working

**URL:** <https://discuss.elastic.co/t/applying-post-filter-after-top-hits-aggregation-not-working/362839>\
**Category:** Elastic Search\
**Tags:** elastic-app-search\
**Created:** [July 10, 2024, 5:25am UTC](https://discuss.elastic.co/t/applying-post-filter-after-top-hits-aggregation-not-working/362839 "2024-07-10T05:25:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manas\_Mohanty](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manas_mohanty/32/123964_2.png) [@Manas\_Mohanty](https://discuss.elastic.co/u/Manas_Mohanty)\
**Post date:** [July 10, 2024, 5:25am UTC](https://discuss.elastic.co/t/applying-post-filter-after-top-hits-aggregation-not-working/362839/1 "2024-07-10T05:25:32Z")

</div>

My input is like this.

```auto
[
  {
    "userId": "u1",
    "status": "success",
    "time": 1719543600008
  },
  {
    "userId": "u1",
    "status": "failure",
    "time": 1719543600007
  },
  {
    "userId": "u1",
    "status": "timeout",
    "time": 1719543600006
  },
  {
    "userId": "u2",
    "status": "Failure",
    "time": 1719543600004
  },
  {
    "userId": "u2",
    "status": "Failure",
    "time": 1719543600003
  }
]

```

I need the users which are not successful in their last attempt.  
Explanation: We need to find out last status of each user and discard the users whose last status is "successful"

I have prepared the query like this. Through this I am able to get all users and their last status. After that I want to filter based on status. Can any one help here in fixing this. I have applied post\_filter after aggregation, but still its not filtering.  
Can any lead help here ?

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "data.time": {
              "gte": "1719543600000",
              "lte": "1719584179015",
              "format": "epoch_millis"
            }
          }
        },
        {
          "query_string": {
            "query": "data.type:\"user-stats\""
          }
        }
      ]
    }
  },
  "aggs": {
    "group_by_userId": {
      "terms": {
        "field": "data.userId.keyword"
      },
      "aggs": {
        "users_last_status": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "data.time": {
                  "order": "desc"
                }
              }
            ]
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [July 15, 2024, 11:18am UTC](https://discuss.elastic.co/t/applying-post-filter-after-top-hits-aggregation-not-working/362839/2 "2024-07-15T11:18:34Z")

</div>

Hi @Manas_Mohanty :

Post-filter is applied to the search results after aggregations are done, so you won't be able to use post-filter for the aggregations themselves.

I think you can achieve what you're looking for with the [bucket selector aggregator](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html) - you can use a custom script for retaining specific aggregations or not.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Manas\_Mohanty](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manas_mohanty/32/123964_2.png) [@Manas\_Mohanty](https://discuss.elastic.co/u/Manas_Mohanty)\
**Post date:** [July 22, 2024, 9:37am UTC](https://discuss.elastic.co/t/applying-post-filter-after-top-hits-aggregation-not-working/362839/3 "2024-07-22T09:37:50Z")

</div>

@Carlos_D Thank you for the response.  
Do we have any performance impact when using custom script ?

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [July 22, 2024, 10:02am UTC](https://discuss.elastic.co/t/applying-post-filter-after-top-hits-aggregation-not-working/362839/4 "2024-07-22T10:02:19Z")

</div>

The script will need to be evaluated for the results aggregated, so there will be a performance impact. It will depend on the number of results you get from the aggregation.

---

<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:** [August 19, 2024, 10:02am UTC](https://discuss.elastic.co/t/applying-post-filter-after-top-hits-aggregation-not-working/362839/5 "2024-08-19T10:02:30Z")

</div>

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