# Query top 5 from multiple indices

**URL:** <https://discuss.elastic.co/t/query-top-5-from-multiple-indices/85777>\
**Category:** Elasticsearch\
**Created:** [May 15, 2017, 2:00pm UTC](https://discuss.elastic.co/t/query-top-5-from-multiple-indices/85777 "2017-05-15T14:00:10Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [May 16, 2017, 1:11pm UTC](https://discuss.elastic.co/t/query-top-5-from-multiple-indices/85777/2 "2017-05-16T13:11:41Z")

</div>

There are two options:

- you could use a relative new Elasticsearch feature called 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))
- or a top hits aggregation ([https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html)), combined with a terms aggregation on the `_index` field.

Downside of using field collapsing is that it won't work on the `_index` metadata fied. So you will need to add an additional field to your documents that indicate what index these docs are in, so you can collapse on that field.

For example, given these docs in three indexes `a`, `b` and `c`:

```auto
    POST _bulk
    { "index" : { "_index": "a", "_type": "doc", "_id" : "1"}}
    { "foo" : "bar a", "my_index": "a"}
    { "index" : { "_index": "a", "_type": "doc", "_id" : "2"}}
    { "foo" : "bar a", "my_index": "a"}
    { "index" : { "_index": "b", "_type": "doc", "_id" : "3"}}
    { "foo" : "bar b", "my_index": "b"}
    { "index" : { "_index": "b", "_type": "doc", "_id" : "4"}}
    { "foo" : "bar b", "my_index": "b"}
    { "index" : { "_index": "c", "_type": "doc", "_id" : "5"}}
    { "foo" : "bar c", "my_index": "c"}
    { "index" : { "_index": "c", "_type": "doc", "_id" : "6"}}
    { "foo" : "bar c", "my_index": "c"}

```

You could run a collapse on the `my_index.keyword` field:

```auto
    GET a,b,c/_search
    {
      "query": {
        "match": {
          "foo": "bar"
        }
      },
      "collapse": {
        "field": "my_index.keyword",
        "inner_hits": {
          "name": "my_top_5",
          "size": 5
        }
      }
    }

```

Probably easier (because it doesn't require that additional `my_index` field) is the top hits aggregation. Given the docs above, the following aggregation request gives you what you're looking for:

```auto
GET a,b,c/_search
{
  "query": {
    "match": {
      "foo": "bar"
    }
  },
  "size": 0,
  "aggs": {
    "indices": {
      "terms": {
        "field": "_index"
      },
      "aggs": {
        "my_top_hits": {
          "top_hits": {
            "size": 5
          }
        }
      }
    }
  }
}

```

---

_[View the full topic](https://discuss.elastic.co/t/query-top-5-from-multiple-indices/85777)._
