# Last N documents aggregation

**URL:** <https://discuss.elastic.co/t/last-n-documents-aggregation/122553>\
**Category:** Elasticsearch\
**Created:** [March 5, 2018, 3:26pm UTC](https://discuss.elastic.co/t/last-n-documents-aggregation/122553 "2018-03-05T15:26:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mPanasiewicz](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@mPanasiewicz](https://discuss.elastic.co/u/mPanasiewicz)\
**Post date:** [March 5, 2018, 3:26pm UTC](https://discuss.elastic.co/t/last-n-documents-aggregation/122553/1 "2018-03-05T15:26:40Z")

</div>

Hello,  
I want to make aggregation of last N documents(for example 100) for watcher alerts.  
Is this possible to use count of documents, not time??

```
"body": {
      "size": 0,
      "query": {
        "bool": {
          "filter": {
            "range": {
              "date": {
                "from": "now-30m",
                "to": "now"
              }
            }
          }
        }
      },
      "aggs": {
        "operator": {
          "terms": {
            "field": "operator.keyword"
          }
        }
      }
    }

```

Above is my aggregation base on time. I need the same but based on the documents count.  
Thank you

---

<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:** [April 2, 2018, 3:26pm UTC](https://discuss.elastic.co/t/last-n-documents-aggregation/122553/2 "2018-04-02T15:26:53Z")

</div>

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