Query: Till maximum value of a field


My documents have a version field. Only updated documents will have a new record with an incremented version number.
{id:1 , version:1,…}
{id:1 , version:2,….} //This has been updated
{id:2 , version:1,…}

In the above example id=1 has two records as it has been updated. While id=2 has a single record as it hasn’t been updated and is still sitting on version 1.

Now in elastic search are there any queries that can get a maximum version.
Example: Get unique documents TILL version 1 should give results:
{id:1 , version:1,…}
{id:2 , version:1,…}

While get unique documents till version 2 should give:
{id:1 , version:2,….}
{id:2 , version:1,…}

(in the above result there is a mix of version 1 and version 2)