# Index-wide search for N documents per type

**URL:** <https://discuss.elastic.co/t/index-wide-search-for-n-documents-per-type/15402>\
**Category:** Elasticsearch\
**Created:** [January 24, 2014, 10:57am UTC](https://discuss.elastic.co/t/index-wide-search-for-n-documents-per-type/15402 "2014-01-24T10:57:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ville\_Mattila](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ville_mattila/32/1457_2.png) [@Ville\_Mattila](https://discuss.elastic.co/u/Ville_Mattila)\
**Post date:** [January 24, 2014, 10:57am UTC](https://discuss.elastic.co/t/index-wide-search-for-n-documents-per-type/15402/1 "2014-01-24T10:57:26Z")

</div>

Hi,

I have an index of approximately 10 different document types. When user  
does a search, I'd like to provide a couple (let's say 5) of hits of each  
type for the user to narrow the search to some type only. At this point, I  
should also provide a total number of hits per type. Currently I make 10  
searches against the index with total result count of 5, but was wondering  
whether similar results could be received with only one search against ES?

I'd like to also use ES's scoring capabilities to get the index-wide best  
match (no matter which type) as the preferred type, apparently no when the  
queries are done per-type the scores are also given per type and they are  
not comparable with each other.

Best regards,  
Ville

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/38e263ec-3a24-48c0-bd7e-20b7264418d5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/38e263ec-3a24-48c0-bd7e-20b7264418d5%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://discuss.elastic.co/u/Binh_Ly)\
**Post date:** [January 24, 2014, 5:55pm UTC](https://discuss.elastic.co/t/index-wide-search-for-n-documents-per-type/15402/2 "2014-01-24T17:55:42Z")

</div>

Ville:

I'm not sure if you know or have tried this, but there is a multi-search  
API that allows you to make 1 call to contain multiple search queries (each  
search query can refer to a type, or an index if needed):

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

I know it's still not a single query but at least you can get all the  
results back in 1 shot and then manipulate them in 1 shot before you return  
the results to your user.

On Friday, January 24, 2014 5:57:26 AM UTC-5, Ville Mattila wrote:

> Hi,
> 
> I have an index of approximately 10 different document types. When user  
> does a search, I'd like to provide a couple (let's say 5) of hits of each  
> type for the user to narrow the search to some type only. At this point, I  
> should also provide a total number of hits per type. Currently I make 10  
> searches against the index with total result count of 5, but was wondering  
> whether similar results could be received with only one search against ES?
> 
> I'd like to also use ES's scoring capabilities to get the index-wide best  
> match (no matter which type) as the preferred type, apparently no when the  
> queries are done per-type the scores are also given per type and they are  
> not comparable with each other.
> 
> Best regards,  
> Ville

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/07e47c29-c526-413e-8042-d4f2a1861ca1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/07e47c29-c526-413e-8042-d4f2a1861ca1%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ville\_Mattila](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ville_mattila/32/1457_2.png) [@Ville\_Mattila](https://discuss.elastic.co/u/Ville_Mattila)\
**Post date:** [January 27, 2014, 4:37pm UTC](https://discuss.elastic.co/t/index-wide-search-for-n-documents-per-type/15402/3 "2014-01-27T16:37:08Z")

</div>

Hi,

Thanks for your reply, I think this is at least better than doing 10  
separate queries. As far as I understood, the queries themselves are still  
executed separately by ES, so the result scoring is done also  
independently, isn't it? To be honest, I am not sure if it matters here  
largerly... I think I need to learn the scoring mechanism (found good links  
from this SO answer: [search - ElasticSearch default scoring mechanism - Stack Overflow](http://stackoverflow.com/a/17532062/769144)) a bit more.

Thanks!

Ville

Ville:

> I'm not sure if you know or have tried this, but there is a multi-search  
> API that allows you to make 1 call to contain multiple search queries (each  
> search query can refer to a type, or an index if needed):
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html)
> 
> I know it's still not a single query but at least you can get all the  
> results back in 1 shot and then manipulate them in 1 shot before you return  
> the results to your user.
> 
> On Friday, January 24, 2014 5:57:26 AM UTC-5, Ville Mattila wrote:
> 
> > Hi,
> > 
> > I have an index of approximately 10 different document types. When user  
> > does a search, I'd like to provide a couple (let's say 5) of hits of each  
> > type for the user to narrow the search to some type only. At this point, I  
> > should also provide a total number of hits per type. Currently I make 10  
> > searches against the index with total result count of 5, but was wondering  
> > whether similar results could be received with only one search against ES?
> > 
> > I'd like to also use ES's scoring capabilities to get the index-wide best  
> > match (no matter which type) as the preferred type, apparently no when the  
> > queries are done per-type the scores are also given per type and they are  
> > not comparable with each other.
> > 
> > Best regards,  
> > Ville

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/71cda819-2677-4c7f-aedb-1a9433c1e2ea%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/71cda819-2677-4c7f-aedb-1a9433c1e2ea%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://discuss.elastic.co/u/Binh_Ly)\
**Post date:** [January 27, 2014, 5:32pm UTC](https://discuss.elastic.co/t/index-wide-search-for-n-documents-per-type/15402/4 "2014-01-27T17:32:47Z")

</div>

Ville,

About the scoring, yeah each query you send in to the \_msearch API will  
have a separate result with it's own scores. You can send multiple querys,  
let's say 1 query for each type, and then 1 overall query at the index  
level. I don't know if that would help but it can be done through \_msearch.

You can also take a look at the function\_score query type to customize  
scoring (at the DSL level) if it helps:

[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html)

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/fff25157-0df8-407b-939c-5fe65775d60d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fff25157-0df8-407b-939c-5fe65775d60d%40googlegroups.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, 1:54am UTC](https://discuss.elastic.co/t/index-wide-search-for-n-documents-per-type/15402/5 "2017-07-06T01:54:32Z")

</div>


