# UNION-style search requests?

**URL:** <https://discuss.elastic.co/t/union-style-search-requests/5639>\
**Category:** Elasticsearch\
**Created:** [October 19, 2011, 9:07am UTC](https://discuss.elastic.co/t/union-style-search-requests/5639 "2011-10-19T09:07:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Regan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrew_regan/32/3080_2.png) [@Andrew\_Regan](https://discuss.elastic.co/u/Andrew_Regan)\
**Post date:** [October 19, 2011, 9:07am UTC](https://discuss.elastic.co/t/union-style-search-requests/5639/1 "2011-10-19T09:07:36Z")

</div>

Is it possible to perform multiple, nested search queries (as per  
BoolQueryBuilder), each nested query run against a potentially different set  
of indices? Like a UNION in SQL: "return all hits where property=X in  
(indexA,indexB) OR property=Y in (indexA,indexC)".

This used to be possible with Compass, but indices are specified at the  
request- rather than the query level in ElasticSearch. I'd have to run each  
search clause individually and UNION them up myself, which feels like a bad  
idea.

Is there a better way to do this using ES?

(I should probably add that, though my indices represent different data  
domains - so I can't replace them with one index and multiple types (unless  
you disagree?) - they do have common properties so that they can be searched  
together.)

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [October 19, 2011, 9:04pm UTC](https://discuss.elastic.co/t/union-style-search-requests/5639/2 "2011-10-19T21:04:45Z")

</div>

You can't do that in elasticsearch, but, we can introduce a new "index"  
element in the query DSL, that will act as match\_all if not executing on the  
listed indices, and use the internal query if it does.

On Wed, Oct 19, 2011 at 11:07 AM, Andrew Regan [aregan@gmail.com](mailto:aregan@gmail.com) wrote:

> Is it possible to perform multiple, nested search queries (as per  
> BoolQueryBuilder), each nested query run against a potentially different set  
> of indices? Like a UNION in SQL: "return all hits where property=X in  
> (indexA,indexB) OR property=Y in (indexA,indexC)".
> 
> This used to be possible with Compass, but indices are specified at the  
> request- rather than the query level in Elasticsearch. I'd have to run each  
> search clause individually and UNION them up myself, which feels like a bad  
> idea.
> 
> Is there a better way to do this using ES?
> 
> (I should probably add that, though my indices represent different data  
> domains - so I can't replace them with one index and multiple types (unless  
> you disagree?) - they do have common properties so that they can be searched  
> together.)

---

<div class="post-metadata">

**Author:** ![Andrew\_Regan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrew_regan/32/3080_2.png) [@Andrew\_Regan](https://discuss.elastic.co/u/Andrew_Regan)\
**Post date:** [October 20, 2011, 4:25pm UTC](https://discuss.elastic.co/t/union-style-search-requests/5639/3 "2011-10-20T16:25:21Z")

</div>

Thanks, Shay. Re, the 'we', is that something I can do myself, or would it  
need a new plugin, perhaps?

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [October 21, 2011, 12:51am UTC](https://discuss.elastic.co/t/union-style-search-requests/5639/4 "2011-10-21T00:51:55Z")

</div>

First you can open an issue :). It should be a built in query in  
elasticsearch, so it can either be the royal we, and I can implement it  
(should be simple), or the "commons" we, where I can help you implement it  
🙂

On Thu, Oct 20, 2011 at 6:25 PM, Andrew Regan [aregan@gmail.com](mailto:aregan@gmail.com) wrote:

> Thanks, Shay. Re, the 'we', is that something I can do myself, or would it  
> need a new plugin, perhaps?

---

<div class="post-metadata">

**Author:** ![Andrew\_Regan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrew_regan/32/3080_2.png) [@Andrew\_Regan](https://discuss.elastic.co/u/Andrew_Regan)\
**Post date:** [October 21, 2011, 2:11pm UTC](https://discuss.elastic.co/t/union-style-search-requests/5639/5 "2011-10-21T14:11:50Z")

</div>

OK, great. Have raised  
[https://github.com/elasticsearch/elasticsearch/issues/1416](https://github.com/elasticsearch/elasticsearch/issues/1416). Happy to help,  
and to test, though not an expert on the ES internals.

---

<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 6, 2017, 3:51am UTC](https://discuss.elastic.co/t/union-style-search-requests/5639/6 "2017-07-06T03:51:12Z")

</div>


