# Aggregate after sort+size

**URL:** <https://discuss.elastic.co/t/aggregate-after-sort-size/104195>\
**Category:** Elasticsearch\
**Created:** [October 17, 2017, 7:26am UTC](https://discuss.elastic.co/t/aggregate-after-sort-size/104195 "2017-10-17T07:26:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndreKR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrekr/32/6233_2.png) [@AndreKR](https://discuss.elastic.co/u/AndreKR)\
**Post date:** [October 17, 2017, 7:26am UTC](https://discuss.elastic.co/t/aggregate-after-sort-size/104195/1 "2017-10-17T07:26:08Z")

</div>

I can get the newest document before a certain timestamp with this query (hopefully with reasonable performance):

```
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "timestamp": {
              "lt": 1508010043
            }
          }
        }
      ]
    }
  },
  "sort": {
    "timestamp": "desc"
  },
  "size": 1
}

```

How can I run an aggregation on it?

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [October 17, 2017, 7:30pm UTC](https://discuss.elastic.co/t/aggregate-after-sort-size/104195/2 "2017-10-17T19:30:26Z")

</div>

Sort and size have no affect on aggregations. Aggregations have their own  
sort and size parameters, wherever applicable.

---

<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 14, 2017, 7:30pm UTC](https://discuss.elastic.co/t/aggregate-after-sort-size/104195/3 "2017-11-14T19:30:42Z")

</div>

Closed on November 14, 2017, 7:30pm UTC

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