# Elastic: Filtering data after grouping

**URL:** <https://discuss.elastic.co/t/elastic-filtering-data-after-grouping/313240>\
**Category:** Elasticsearch\
**Created:** [August 30, 2022, 9:52am UTC](https://discuss.elastic.co/t/elastic-filtering-data-after-grouping/313240 "2022-08-30T09:52:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fchen521](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fchen521/32/110240_2.png) [@fchen521](https://discuss.elastic.co/u/fchen521)\
**Post date:** [August 30, 2022, 9:52am UTC](https://discuss.elastic.co/t/elastic-filtering-data-after-grouping/313240/1 "2022-08-30T09:52:53Z")

</div>

First aggregate according to the people, then count the data in each group, and filter out the people whose start time field is \> minimum time - 10 days and whose start time is \< minimum time + 10 days  
Is this all right, please

```auto
GET /users_index/_search
{
  "size": 0,
  "aggs": {
    "aggs": {
      "terms": {
        "field": "userId"
      },
      "aggs": {
        "minTime":{
          "min": {
            "field": "updateTime"
          }
          
        },
        "top": {
          "top_hits": {
            "size": 10,
            "sort": [
              {
                "updateTime": {
                  "order": "desc"
                }
              }
            ]
          }
        }
      }
    }
  }
}

```

---

<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 27, 2022, 9:53am UTC](https://discuss.elastic.co/t/elastic-filtering-data-after-grouping/313240/2 "2022-09-27T09:53:36Z")

</div>

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