# Get documents with dynamic max field

**URL:** <https://discuss.elastic.co/t/get-documents-with-dynamic-max-field/218028>\
**Category:** Elasticsearch\
**Created:** [February 5, 2020, 4:38pm UTC](https://discuss.elastic.co/t/get-documents-with-dynamic-max-field/218028 "2020-02-05T16:38:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![OlivierC](https://avatars.discourse-cdn.com/v4/letter/o/4491bb/32.png) [@OlivierC](https://discuss.elastic.co/u/OlivierC)\
**Post date:** [February 5, 2020, 4:38pm UTC](https://discuss.elastic.co/t/get-documents-with-dynamic-max-field/218028/1 "2020-02-05T16:38:46Z")

</div>

Hello there!

First of all, thanks to the team for the work, and sorry for the title, I don't know how to summarize my issue.

I need help for something looking very simple for a human but I can't find how to do with ES:

Let's imagine I have these simplified documents in ES:  
|id |status |version\_number|created\_at |  
|XXX1 |review |4 |2020-02-05 11:22:21|  
|XXX2 |accepted |5 |2020-02-05 11:23:08|  
|XXX3 |published|10 |2020-02-05 11:23:24|  
|XXX4 |review |14 |2020-02-05 13:58:10|  
|XXX5 |accepted |15 |2020-02-05 13:58:16|  
|XXX6 |published|20 |2020-02-05 13:58:22|  
|XXX7 |review |23 |2020-02-05 14:32:34|  
|XXX8 |accepted |24 |2020-02-05 14:32:42|  
|XXX9 |review |28 |2020-02-05 14:33:28|  
|XXX10|accepted |29 |2020-02-05 14:33:33|  
|XXX11|published|30 |2020-02-05 14:33:37|

I need to get all documents, minus XXX7 and XXX8, because they aren't the last one before the next published one. So the query must get documents completing these conditions:

\*status must be "review", "accepted" or "published"  
\*get documents with "review" or "accepted" status with the higher version number before a published one.

So hits should be:  
|id |status |version\_number|created\_at |  
|XXX1 |review |4 |2020-02-05 11:22:21|  
|XXX2 |accepted |5 |2020-02-05 11:23:08|  
|XXX3 |published|10 |2020-02-05 11:23:24|  
|XXX4 |review |14 |2020-02-05 13:58:10|  
|XXX5 |accepted |15 |2020-02-05 13:58:16|  
|XXX6 |published|20 |2020-02-05 13:58:22|  
|XXX9 |review |28 |2020-02-05 14:33:28|  
|XXX10|accepted |29 |2020-02-05 14:33:33|  
|XXX11|published|30 |2020-02-05 14:33:37|

Thanks a lot for your help!

Olivier

---

<div class="post-metadata">

**Author:** ![OlivierC](https://avatars.discourse-cdn.com/v4/letter/o/4491bb/32.png) [@OlivierC](https://discuss.elastic.co/u/OlivierC)\
**Post date:** [February 6, 2020, 12:39pm UTC](https://discuss.elastic.co/t/get-documents-with-dynamic-max-field/218028/2 "2020-02-06T12:39:13Z")

</div>

I added a field to "invalidate" those I don't need.

Issue closed

---

<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:** [March 5, 2020, 12:39pm UTC](https://discuss.elastic.co/t/get-documents-with-dynamic-max-field/218028/3 "2020-03-05T12:39:58Z")

</div>

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