# Only query new documents from Elasticsearch index

**URL:** <https://discuss.elastic.co/t/only-query-new-documents-from-elasticsearch-index/249703>\
**Category:** Elasticsearch\
**Created:** [September 23, 2020, 8:19pm UTC](https://discuss.elastic.co/t/only-query-new-documents-from-elasticsearch-index/249703 "2020-09-23T20:19:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mussa572](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@mussa572](https://discuss.elastic.co/u/mussa572)\
**Post date:** [September 23, 2020, 8:19pm UTC](https://discuss.elastic.co/t/only-query-new-documents-from-elasticsearch-index/249703/1 "2020-09-23T20:19:10Z")

</div>

I am writing a python program which will query elasticsearch index . I am using a match all query in the while loop which looks for the new documents . First problem is that I always get all the documents every time I query however I am only interested in the new documents since the last query .

Is there a way I can only read new documents instead of running while loopAlso is there a way where elasticsearch can alert that it has received new documents since the last query . I can then query only new documents

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [September 23, 2020, 9:41pm UTC](https://discuss.elastic.co/t/only-query-new-documents-from-elasticsearch-index/249703/2 "2020-09-23T21:41:54Z")

</div>

There's nothing native to Elasticsearch that provides this sort of approach.

You will need to use a top hits aggregation, and track the time of the last query to use that as the field you run the agg against.

---

<div class="post-metadata">

**Author:** ![Emanuil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emanuil/32/36783_2.png) [@Emanuil](https://discuss.elastic.co/u/Emanuil)\
**Post date:** [September 23, 2020, 9:49pm UTC](https://discuss.elastic.co/t/only-query-new-documents-from-elasticsearch-index/249703/3 "2020-09-23T21:49:46Z")

</div>

Most people add an `@timestamp` (or `created_at` or whatever you want to call it) field to their docs.

Then you could use a [range query](https://www.elastic.co/guide/en/elasticsearch/reference/7.9/query-dsl-range-query.html) rather than match\_all to filter the docs by when they were created. As Mark says, with Elasticsearch you have to poll, it can't push an alert to 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:** [October 21, 2020, 9:49pm UTC](https://discuss.elastic.co/t/only-query-new-documents-from-elasticsearch-index/249703/4 "2020-10-21T21:49:55Z")

</div>

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