# Search across multiple indices

**URL:** <https://discuss.elastic.co/t/search-across-multiple-indices/199317>\
**Category:** Elasticsearch\
**Created:** [September 12, 2019, 7:02pm UTC](https://discuss.elastic.co/t/search-across-multiple-indices/199317 "2019-09-12T19:02:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junfeng](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@Junfeng](https://discuss.elastic.co/u/Junfeng)\
**Post date:** [September 12, 2019, 7:02pm UTC](https://discuss.elastic.co/t/search-across-multiple-indices/199317/1 "2019-09-12T19:02:42Z")

</div>

Hello,

I am trying to search multiple indices, each with its own query. Is it possible for 2.x?  
I get the idea from [here](https://discuss.elastic.co/t/multi-index-search-multiple-queries-within-the-query/119564). Basically my composite query looks like this:

```
POST /index1, index2/_search

```

> ```
> {
> "query": {
> "bool": {
> "should": [
> {
> "bool": {
> "filter": [
> {
> "term": {
> "_index": index1
> }
> }  
> ], 
> "must": query1
> }
> },
> {
> "bool": {
> "filter": {
> "term": {
> "_index": index2
> }
> }, 
> "must": query2
> }
> }
> ]
> }
> }
> }
> 
> ```

This works well if both queries are simple. However, my query1 is a complex query that has a has-child piece in it. It fails even if I use query1 for index2, saying that "[has\_child] No mapping for for type [subtype in index1]". Is it possible to send query1 to index1 only, and query2 to index2 only?

I would appreciate if someone can give some advice

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [September 12, 2019, 8:06pm UTC](https://discuss.elastic.co/t/search-across-multiple-indices/199317/2 "2019-09-12T20:06:29Z")

</div>

I think you'll need to use an [indices query](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/query-dsl-indices-query.html) instead of a `term` on the index field. The filters don't "share" knowledge so even though only one or the other part of the query apply to an index, all the parts of the query are checked against the mappings.

I _think_ the indices query has a different behavior, but I'm not positive.

Also, 2.x is quite old. You should probably upgrade or try a newer version 🙂

---

<div class="post-metadata">

**Author:** ![Junfeng](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@Junfeng](https://discuss.elastic.co/u/Junfeng)\
**Post date:** [September 12, 2019, 8:19pm UTC](https://discuss.elastic.co/t/search-across-multiple-indices/199317/3 "2019-09-12T20:19:06Z")

</div>

Thank you for replying, Zach! Yeah 2.x is old, but for this project my hands are tied and I need to stick with the old version.

It seems `indices query` is deprecated from 5.0 in favor of \_index ([link](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/query-dsl-indices-query.html)). Also `indices query` might not be helpful because I need to support more than 2 indices and each with a query, and `indices query` only supports `query` and `not_match_query`, thus is not extensible for my case.

My guess for query1 failing in index2 is also that query parsing and validation is done for each index. Currently my workaround is to create a `subtype in index1` in index2, simply to make query1 run without error in index2. I am not sure about the performance penalty though.

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [September 13, 2019, 12:47am UTC](https://discuss.elastic.co/t/search-across-multiple-indices/199317/4 "2019-09-13T00:47:17Z")

</div>

Hi @Junfeng

> [@Junfeng](#):
>
> Is it possible to send query1 to index1 only, and query2 to index2 only?

Can msearch solve your problem?

> **[Multi Search API | Elasticsearch Guide \[2.4\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-multi-search.html)**

---

<div class="post-metadata">

**Author:** ![Junfeng](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@Junfeng](https://discuss.elastic.co/u/Junfeng)\
**Post date:** [September 13, 2019, 2:59am UTC](https://discuss.elastic.co/t/search-across-multiple-indices/199317/5 "2019-09-13T02:59:33Z")

</div>

Hi @gabriel_tessier, thanks for the answer. I was hoping to solve it by one query without the need to merge the multiple sorted lists and to maintain the positions for each list, as I also need to do pagination. But I think your solution is cleaner.

---

<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:** [October 11, 2019, 3:00am UTC](https://discuss.elastic.co/t/search-across-multiple-indices/199317/6 "2019-10-11T03:00:15Z")

</div>

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