'How to get latest document in elastic search

In elastic search endpoint, json contains below fields( part of json ):

{
.....
Version : some number,
Key : some alphanumeric
.....

}

Now when ever json will get updated, a new document will get created in elastic search endpoint with same key but different version. How i should frame my query to get document with latest version.

FYI : there are huge number of records with different keys. i have to filter out all document with different key with latest version.



Solution 1:[1]

Use a boolean flag "latestversion_for_key" and update it whenever you create a new document.

This way you move the "latest version"-decision from the moment of search (for a large number of keys) to the moment of creation (with only one single key).

Whenever you create a new document in ElasticSearch you would search for the current max version number for this key (either with latestversion_for_key=true and key=[yourkey] or with max aggregation https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html?msclkid=55aa34fdd05d11ecaaab8749c6549ffe). Update that document (set latestversion_for_key = false) and insert your new document with latestversion_for_key=true.

Disadvantage: for a short moment of time there will be no "latestversion_for_key" flag or maybe (if you switch the order of execution) there will be two documents for a key marked with "latestversion_for_key".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 S. Doe