# Getting documents having minimum value for a given field

**URL:** <https://discuss.elastic.co/t/getting-documents-having-minimum-value-for-a-given-field/106808>\
**Category:** Elasticsearch\
**Created:** [November 8, 2017, 6:48am UTC](https://discuss.elastic.co/t/getting-documents-having-minimum-value-for-a-given-field/106808 "2017-11-08T06:48:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaisharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaisharma/32/22833_2.png) [@jaisharma](https://discuss.elastic.co/u/jaisharma)\
**Post date:** [November 8, 2017, 6:48am UTC](https://discuss.elastic.co/t/getting-documents-having-minimum-value-for-a-given-field/106808/1 "2017-11-08T06:48:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [November 8, 2017, 10:12am UTC](https://discuss.elastic.co/t/getting-documents-having-minimum-value-for-a-given-field/106808/2 "2017-11-08T10:12:03Z")

</div>

You could use field collapsing ([https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html)) 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:

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

```

---

<div class="post-metadata">

**Author:** ![jaisharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaisharma/32/22833_2.png) [@jaisharma](https://discuss.elastic.co/u/jaisharma)\
**Post date:** [November 8, 2017, 4:01pm UTC](https://discuss.elastic.co/t/getting-documents-having-minimum-value-for-a-given-field/106808/3 "2017-11-08T16:01:14Z")

</div>

I've ES 2.x running in my production and thus would really like to know the alternative of **collapse**. I found this document [https://www.elastic.co/guide/en/elasticsearch/guide/current/top-hits.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/top-hits.html) but can't understand the usage of script here.

---

<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:** [December 6, 2017, 4:01pm UTC](https://discuss.elastic.co/t/getting-documents-having-minimum-value-for-a-given-field/106808/4 "2017-12-06T16:01:35Z")

</div>

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