# Modify query to get 1 unique document per index?

**URL:** https://discuss.elastic.co/t/modify-query-to-get-1-unique-document-per-index/188452
**Category:** Elasticsearch
**Created:** [July 2, 2019, 7:44am UTC](https://discuss.elastic.co/t/modify-query-to-get-1-unique-document-per-index/188452 "2019-07-02T07:44:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Sjaak01](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Sjaak01](https://discuss.elastic.co/u/Sjaak01)
#### Post date: [July 2, 2019, 7:44am UTC](https://discuss.elastic.co/t/modify-query-to-get-1-unique-document-per-index/188452/1 "2019-07-02T07:44:08Z")

</div>

Hi,

I have the following query to pull some data from Elastic. The query works fine however I want to extend it a little bit by making sure I get the latest record per device per index. The current query will simply get the latest record from the three indices I'm querying but I want the latest record per index.

Increasing the size is not an option, because of the timestamp getting a record from each index will be a bit hit or miss. Of course I could simply set up a cron job to run the query three times, one time for each index, but that would be too easy and probably not very efficient either.

Edit: To clarify, each index contains the fields `device_id`. I want the latest document that contains device\_id per index per device\_id. So if I have 10 unique device\_id's, the result should be 30 records, the 10 unique device\_id's per index.

```
GET index1,index2,index3/_search?size=0
{
    "query": {
        "range" : {
            "@ingest_time" : {
              "gte": "now-10d"
            }
        }
    },
    "aggs": {
        "device": {
            "terms": {
                "field": "device_id",
                "size": 5000
            },
            "aggs": {
                "latest_data": {
                    "top_hits": {
                        "sort": [
                            {
                                "@timestamp": {
                                    "order": "desc"
                                }
                            }
                        ],
                        "_source": {
                            "includes": ["buch of fields"]
                        },
                        "size" : 1
                    }
                }
            }
        }
    }
}
```

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [July 2, 2019, 11:11am UTC](https://discuss.elastic.co/t/modify-query-to-get-1-unique-document-per-index/188452/2 "2019-07-02T11:11:12Z")

</div>

hey,

what about using a [multi search](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/search-multi-search.html) with the same query against each of those indices?

--Alex

---

<div class="post-metadata">

### Author: ![Sjaak01](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Sjaak01](https://discuss.elastic.co/u/Sjaak01)
#### Post date: [July 3, 2019, 7:47am UTC](https://discuss.elastic.co/t/modify-query-to-get-1-unique-document-per-index/188452/3 "2019-07-03T07:47:08Z")

</div>

Thanks for the tip. I managed to achieve what I want using multi search.

---

<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: [July 31, 2019, 7:47am UTC](https://discuss.elastic.co/t/modify-query-to-get-1-unique-document-per-index/188452/4 "2019-07-31T07:47:10Z")

</div>

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