# Query and return up to n results based on a type field

**URL:** <https://discuss.elastic.co/t/query-and-return-up-to-n-results-based-on-a-type-field/265213>\
**Category:** Elasticsearch\
**Created:** [February 23, 2021, 3:00pm UTC](https://discuss.elastic.co/t/query-and-return-up-to-n-results-based-on-a-type-field/265213 "2021-02-23T15:00:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![larswise](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/larswise/32/84395_2.png) [@larswise](https://discuss.elastic.co/u/larswise)\
**Post date:** [February 23, 2021, 3:00pm UTC](https://discuss.elastic.co/t/query-and-return-up-to-n-results-based-on-a-type-field/265213/1 "2021-02-23T15:00:30Z")

</div>

Hi,

I have a datamodel with different object types in the same index. These are simply distinguished by a field I have called "type".

I would like to do a "all in one query" within each of these types, giving me the top, lets say 10 most relevant results for each (if as many as 10 exists for any given type).  
(My scenario is a composite autocomplete search)

How should I approach this?  
I have been thinking of using the multi search api. Would this be the way to go?  
Are there other recommended approaches?

So far I have tried to just search without any filtering with multi match, but I often end up with 10 of just the same type, or a few different types, because they happen to be the most relevant.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [February 25, 2021, 3:02am UTC](https://discuss.elastic.co/t/query-and-return-up-to-n-results-based-on-a-type-field/265213/2 "2021-02-25T03:02:29Z")

</div>

`_msearch` would work, just do one for each of your types.

---

<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 25, 2021, 3:02am UTC](https://discuss.elastic.co/t/query-and-return-up-to-n-results-based-on-a-type-field/265213/3 "2021-03-25T03:02:37Z")

</div>

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