# Is it possible to query multiple indices but only apply a bool to results from one of the indices?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-query-multiple-indices-but-only-apply-a-bool-to-results-from-one-of-the-indices/152286>\
**Category:** Elasticsearch\
**Created:** [October 12, 2018, 6:11pm UTC](https://discuss.elastic.co/t/is-it-possible-to-query-multiple-indices-but-only-apply-a-bool-to-results-from-one-of-the-indices/152286 "2018-10-12T18:11:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![swidge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swidge/32/36489_2.png) [@swidge](https://discuss.elastic.co/u/swidge)\
**Post date:** [October 12, 2018, 6:11pm UTC](https://discuss.elastic.co/t/is-it-possible-to-query-multiple-indices-but-only-apply-a-bool-to-results-from-one-of-the-indices/152286/1 "2018-10-12T18:11:15Z")

</div>

My use case is this: I have an index `items` and an index `userItems`. In my query, I want to pull results that match a search term on both indices. HOWEVER, I want to only pull results from `userItems` that belong to the user.

I'm not sure how to structure a query where a bool clause is only applied to results from one of the indices and not the other. Can I do this in a single query? Do I need to split this into two different queries?

Any tips would be much appreciated! Thanks!

---

<div class="post-metadata">

**Author:** ![dakrone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dakrone/32/23351_2.png) [@dakrone](https://discuss.elastic.co/u/dakrone)\
**Post date:** [October 14, 2018, 3:08am UTC](https://discuss.elastic.co/t/is-it-possible-to-query-multiple-indices-but-only-apply-a-bool-to-results-from-one-of-the-indices/152286/2 "2018-10-14T03:08:02Z")

</div>

Hi Swidge,

Yes, you can do this like so:

```auto
GET /items,userItems/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "match": {
                  "myfield": "potato"
                }
              }
            ],
            "filter": [
              {
                "term": {
                  "_index": "items"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "match": {
                  "myfield": "potato"
                }
              }
            ],
            "filter": [
              {
                "term": {
                  "_index": "userItems"
                }
              },
              {
                "term": {
                  "username": "bob"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

Hopefully that makes sense, by having a top level bool query this essentially says "give me all the documents that have "potato" in "myfield" in the "items" index, **OR** all the documents that have "potato" in "myfield" in the "userItems" index that also have the "username" of "bob".

---

<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:** [November 11, 2018, 3:08am UTC](https://discuss.elastic.co/t/is-it-possible-to-query-multiple-indices-but-only-apply-a-bool-to-results-from-one-of-the-indices/152286/3 "2018-11-11T03:08:06Z")

</div>

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