Getting documents having minimum value for a given field

My document structure is as below:

{'_id': 1, '_type': '2017-01-01',...}  --- (1)
{'_id': 1, '_type': '2017-01-02',...}  --- (2) 
{'_id': 2, '_type': '2017-01-01',...}  --- (3)
{'_id': 2, '_type': '2017-01-02',...}  --- (4)

Each id can belong to different types (which are dates here). The aim is to get documents matching given id list having minimum value of _type.

So for an input of id=[1,2], documents 1 & 3 should be returned as they have minimum value of _type (2017-01-01).

This can easily be done on per id basis (by querying for min available type for a given id), but that'd be too expensive as the given id list is in range of thousands.

You could use field collapsing ( to do this but note that we don't allow multiple types per index in the latest versions of es. Instead of multiple types, you could use a field to register the date for the document (_type in your example) and use another field to handle _id which does not seem to be an id in your case since multiple documents can have the same id.
With field collapsing you could do:

GET /my_index/_search
    "query": {
    "collapse" : {
        "field" : "my_id", 
        "inner_hits": {
            "name": "minimum_doc", 
            "size": 1, 
            "sort": [{ "my_date": "asc" }] 
1 Like

I've ES 2.x running in my production and thus would really like to know the alternative of collapse . I found this document but can't understand the usage of script here.

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