# Possible to get top N results each from 2 separate indexes in single query?

**URL:** https://discuss.elastic.co/t/possible-to-get-top-n-results-each-from-2-separate-indexes-in-single-query/11443
**Category:** Elasticsearch
**Created:** [April 4, 2013, 4:44am UTC](https://discuss.elastic.co/t/possible-to-get-top-n-results-each-from-2-separate-indexes-in-single-query/11443 "2013-04-04T04:44:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Craig\_Brown](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@Craig\_Brown](https://discuss.elastic.co/u/Craig_Brown)
#### Post date: [April 4, 2013, 4:44am UTC](https://discuss.elastic.co/t/possible-to-get-top-n-results-each-from-2-separate-indexes-in-single-query/11443/1 "2013-04-04T04:44:05Z")

</div>

I have the same query that I want to run against 2 separate indexes. I want  
to get the top N results from each index. Right now I'm running as 2  
separate queries. I know I can specify multiple indexes for a query, but  
that's not quite what I mean.

So if I want to get the top 10 results from each index, running 2 separate  
queries I want to get 20 results back. Is it possible to combine the  
queries into a single one and still get the top 10 results from each index?  
I don't want the top 10 results from both indexes combined, I need to top  
10 results from each index using the same query for each.

I assume I can use a multi-query to issue both queries at the same time,  
which should be more efficient than running the queries sequentially. Just  
wanted to see if anyone had any suggestions for running the same query  
against both indexes and getting the top N from each. Maybe that would be  
no faster than the multi-query anyway.

Thanks!

- Craig

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)
#### Post date: [April 4, 2013, 5:52am UTC](https://discuss.elastic.co/t/possible-to-get-top-n-results-each-from-2-separate-indexes-in-single-query/11443/2 "2013-04-04T05:52:18Z")

</div>

Hi,

as you already pointed out, you can use multi-query and I think this can be  
the best option for you. Just go with it.

I do not know your specific use case (so what I say now might not apply to  
you) but you might also consider running a single query against two indices  
and use query\_and\_fetch [1] query type, this will return you 10 results  
from _each_ index shard (in case your indices have only a single shard then  
this would result into 20 search hits for two indices) then you could use  
\_index field value to identify source index on the client.

Regards,  
Lukas

[1] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/search-type/)

On Thu, Apr 4, 2013 at 6:44 AM, Craig Brown [cbrown@youwho.com](mailto:cbrown@youwho.com) wrote:

> I have the same query that I want to run against 2 separate indexes. I  
> want to get the top N results from each index. Right now I'm running as 2  
> separate queries. I know I can specify multiple indexes for a query, but  
> that's not quite what I mean.
> 
> So if I want to get the top 10 results from each index, running 2 separate  
> queries I want to get 20 results back. Is it possible to combine the  
> queries into a single one and still get the top 10 results from each index?  
> I don't want the top 10 results from both indexes combined, I need to top  
> 10 results from each index using the same query for each.
> 
> I assume I can use a multi-query to issue both queries at the same time,  
> which should be more efficient than running the queries sequentially. Just  
> wanted to see if anyone had any suggestions for running the same query  
> against both indexes and getting the top N from each. Maybe that would be  
> no faster than the multi-query anyway.
> 
> Thanks!
> 
> - Craig
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Craig\_Brown](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@Craig\_Brown](https://discuss.elastic.co/u/Craig_Brown)
#### Post date: [April 4, 2013, 3:50pm UTC](https://discuss.elastic.co/t/possible-to-get-top-n-results-each-from-2-separate-indexes-in-single-query/11443/3 "2013-04-04T15:50:33Z")

</div>

Thanks Lukas! I'll give both a shot. BTW, love BigDesk! Thanks for  
contributing!

- Craig

On Wed, Apr 3, 2013 at 11:52 PM, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:

> Hi,
> 
> as you already pointed out, you can use multi-query and I think this can  
> be the best option for you. Just go with it.
> 
> I do not know your specific use case (so what I say now might not apply to  
> you) but you might also consider running a single query against two indices  
> and use query\_and\_fetch [1] query type, this will return you 10 results  
> from _each_ index shard (in case your indices have only a single shard then  
> this would result into 20 search hits for two indices) then you could use  
> \_index field value to identify source index on the client.
> 
> Regards,  
> Lukas
> 
> [1] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/search-type/)
> 
> On Thu, Apr 4, 2013 at 6:44 AM, Craig Brown [cbrown@youwho.com](mailto:cbrown@youwho.com) wrote:
> 
> > I have the same query that I want to run against 2 separate indexes. I  
> > want to get the top N results from each index. Right now I'm running as 2  
> > separate queries. I know I can specify multiple indexes for a query, but  
> > that's not quite what I mean.
> > 
> > So if I want to get the top 10 results from each index, running 2  
> > separate queries I want to get 20 results back. Is it possible to combine  
> > the queries into a single one and still get the top 10 results from each  
> > index? I don't want the top 10 results from both indexes combined, I need  
> > to top 10 results from each index using the same query for each.
> > 
> > I assume I can use a multi-query to issue both queries at the same time,  
> > which should be more efficient than running the queries sequentially. Just  
> > wanted to see if anyone had any suggestions for running the same query  
> > against both indexes and getting the top N from each. Maybe that would be  
> > no faster than the multi-query anyway.
> > 
> > Thanks!
> > 
> > - Craig
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
…  
CRAIG BROWN  
Search Architect  
youwho, Inc.

[www.youwho.com](http://www.youwho.com)

M: 801.680.7300

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)
#### Post date: [April 4, 2013, 4:30pm UTC](https://discuss.elastic.co/t/possible-to-get-top-n-results-each-from-2-separate-indexes-in-single-query/11443/4 "2013-04-04T16:30:09Z")

</div>

thanks!

On Thu, Apr 4, 2013 at 5:50 PM, Craig Brown [cbrown@youwho.com](mailto:cbrown@youwho.com) wrote:

> Thanks Lukas! I'll give both a shot. BTW, love BigDesk! Thanks for  
> contributing!
> 
> - Craig
> 
> On Wed, Apr 3, 2013 at 11:52 PM, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com)wrote:
> 
> > Hi,
> > 
> > as you already pointed out, you can use multi-query and I think this can  
> > be the best option for you. Just go with it.
> > 
> > I do not know your specific use case (so what I say now might not apply  
> > to you) but you might also consider running a single query against two  
> > indices and use query\_and\_fetch [1] query type, this will return you 10  
> > results from _each_ index shard (in case your indices have only a single  
> > shard then this would result into 20 search hits for two indices) then you  
> > could use \_index field value to identify source index on the client.
> > 
> > Regards,  
> > Lukas
> > 
> > [1] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/search-type/)
> > 
> > On Thu, Apr 4, 2013 at 6:44 AM, Craig Brown [cbrown@youwho.com](mailto:cbrown@youwho.com) wrote:
> > 
> > > I have the same query that I want to run against 2 separate indexes. I  
> > > want to get the top N results from each index. Right now I'm running as 2  
> > > separate queries. I know I can specify multiple indexes for a query, but  
> > > that's not quite what I mean.
> > > 
> > > So if I want to get the top 10 results from each index, running 2  
> > > separate queries I want to get 20 results back. Is it possible to combine  
> > > the queries into a single one and still get the top 10 results from each  
> > > index? I don't want the top 10 results from both indexes combined, I need  
> > > to top 10 results from each index using the same query for each.
> > > 
> > > I assume I can use a multi-query to issue both queries at the same time,  
> > > which should be more efficient than running the queries sequentially. Just  
> > > wanted to see if anyone had any suggestions for running the same query  
> > > against both indexes and getting the top N from each. Maybe that would be  
> > > no faster than the multi-query anyway.
> > > 
> > > Thanks!
> > > 
> > > - Craig
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> …  
> CRAIG BROWN  
> Search Architect  
> youwho, Inc.
> 
> [www.youwho.com](http://www.youwho.com)
> 
> M: 801.680.7300
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:42am UTC](https://discuss.elastic.co/t/possible-to-get-top-n-results-each-from-2-separate-indexes-in-single-query/11443/5 "2017-07-06T02:42:45Z")

</div>


