# Get the documents with max of @version based on document\_id

**URL:** <https://discuss.elastic.co/t/get-the-documents-with-max-of-version-based-on-document-id/266877>\
**Category:** Elasticsearch\
**Created:** [March 11, 2021, 2:52am UTC](https://discuss.elastic.co/t/get-the-documents-with-max-of-version-based-on-document-id/266877 "2021-03-11T02:52:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkhapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rkhapre/32/48333_2.png) [@rkhapre](https://discuss.elastic.co/u/rkhapre)\
**Post date:** [March 11, 2021, 2:52am UTC](https://discuss.elastic.co/t/get-the-documents-with-max-of-version-based-on-document-id/266877/1 "2021-03-11T02:52:47Z")

</div>

Hi All,

I have certain documents in my index with each document having a document\_id as primary key with the combination of "Campaign" and "Camp Line" which will be %{Campaign}%{Camp Line}

Every Document has @version. I want to filter all the document whose @version is max based on the primary key/document\_id

Example, my index has this document

> | Campaign | Camp Line | Clicks | "@Version" |
> | --- | --- | --- | --- |
> | Campaign 1 | 1 | 30 | 2 |
> | Campaign 1 | 2 | 45 | 1 |
> | Campaign 1 | 3 | 210 | 2 |
> | Campaign 2 | 1 | 22 | 2 |
> | Campaign 2 | 2 | 130 | 1 |
> | Campaign3 | 1 | 34 | 3 |
> | Campaign3 | 2 | 23 | 1 |
> | Campaign3 | 3 | 28 | 3 |

My expected Result is this

> | Campaign | Camp Line | Clicks | "@Version" |
> | --- | --- | --- | --- |
> | Campaign 1 | 1 | 30 | 2 |
> | Campaign 1 | 3 | 210 | 2 |
> | Campaign 2 | 1 | 22 | 2 |
> | Campaign3 | 1 | 34 | 3 |
> | Campaign3 | 3 | 28 | 3 |

Please let me know , how i can i retrieve this through REST API

Thanks

---

<div class="post-metadata">

**Author:** ![rkhapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rkhapre/32/48333_2.png) [@rkhapre](https://discuss.elastic.co/u/rkhapre)\
**Post date:** [March 13, 2021, 3:47am UTC](https://discuss.elastic.co/t/get-the-documents-with-max-of-version-based-on-document-id/266877/2 "2021-03-13T03:47:00Z")

</div>

Bump up

---

<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 10, 2021, 3:47am UTC](https://discuss.elastic.co/t/get-the-documents-with-max-of-version-based-on-document-id/266877/3 "2021-04-10T03:47:06Z")

</div>

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