# Multi Index Search - Multiple Queries within the query

**URL:** <https://discuss.elastic.co/t/multi-index-search-multiple-queries-within-the-query/119564>\
**Category:** Elasticsearch\
**Created:** [February 13, 2018, 1:02am UTC](https://discuss.elastic.co/t/multi-index-search-multiple-queries-within-the-query/119564 "2018-02-13T01:02:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![whittssg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/whittssg/32/27669_2.png) [@whittssg](https://discuss.elastic.co/u/whittssg)\
**Post date:** [February 13, 2018, 1:02am UTC](https://discuss.elastic.co/t/multi-index-search-multiple-queries-within-the-query/119564/1 "2018-02-13T01:02:09Z")

</div>

Hello,

I was wondering if there was a way to do one call with multiple indexes but also have individual queries for each index?

Currently i am specifying all of my indexes that i need above the body \> index:[index1, index2, etc]

Then in the query i am using lots of bools with must not field exists (if field exists then do this query), this seems to work but is getting complicated as my index list increases.

I realize i could just do separate calls for each index (with their own queries) but this way is alot faster and i also realize that these indexes should be normalized (but they are not and i have no control over that).

To sum it up i am trying to allow users to select some options and an optional search string, let the system search across all of the specified indexes for these options (if they make sense for that index - e.g. some have product fields some dont) and then bring back the match count for each index (the count part i am using aggs on the \_type field).

Thanks,

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [February 13, 2018, 1:22am UTC](https://discuss.elastic.co/t/multi-index-search-multiple-queries-within-the-query/119564/2 "2018-02-13T01:22:39Z")

</div>

You could do this in one query by building a composite query that combines the query that you would wish to execute against a specific index with a `term` or `terms` query that filters only to those indices you're interested in.

As an example, here's a query that executes a search request against all indices, but targets indices `index-1` and `index-2` differently to the rest by using two `bool` queries in the `should` clause of a `bool` query. Each of the inner `bool` queries combines a `must` clause (the query to execute) with a `filter` or `must_not` clause that determines the indices to apply to.

```auto
POST http://localhost:9200/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "query-field": {
                    "value": "query-term"
                  }
                }
              }
            ],
            "filter": [
              {
                "terms": {
                  "_index": [
                    "index-1",
                    "index-2"
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "no-match-query-field": {
                    "value": "no-match-query-term"
                  }
                }
              }
            ],
            "must_not": [
              {
                "terms": {
                  "_index": [
                    "index-1",
                    "index-2"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![whittssg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/whittssg/32/27669_2.png) [@whittssg](https://discuss.elastic.co/u/whittssg)\
**Post date:** [February 13, 2018, 1:48am UTC](https://discuss.elastic.co/t/multi-index-search-multiple-queries-within-the-query/119564/3 "2018-02-13T01:48:06Z")

</div>

Thanks for taking the time to explain this, I will try it first thing in the morning and will report back 😀

---

<div class="post-metadata">

**Author:** ![whittssg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/whittssg/32/27669_2.png) [@whittssg](https://discuss.elastic.co/u/whittssg)\
**Post date:** [February 13, 2018, 3:55am UTC](https://discuss.elastic.co/t/multi-index-search-multiple-queries-within-the-query/119564/4 "2018-02-13T03:55:45Z")

</div>

Seems to work great. 🙂

Any reason why we need to negate the fields and not promote them? Just want to understand why its working rather than "it works". In my head i would want to think " i want to match these fields to these indexes" but the way this works is "i want to match these fields to any index apart from these.

Thanks again

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [February 13, 2018, 4:00am UTC](https://discuss.elastic.co/t/multi-index-search-multiple-queries-within-the-query/119564/5 "2018-02-13T04:00:32Z")

</div>

This is purely an example; you can extend the number of `bool` queries as `should` clauses to implement pretty much any arbitrary index targeting that you need to perform.

I pulled this example out of another question that was asking how to support similar functionality to [`indices` queries](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-indices-query.html) now that they're deprecated in 5.x 😄

---

<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:** [March 13, 2018, 4:00am UTC](https://discuss.elastic.co/t/multi-index-search-multiple-queries-within-the-query/119564/6 "2018-03-13T04:00:40Z")

</div>

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