# Elasticsearch get a selection of predefined types as result in one query

**URL:** <https://discuss.elastic.co/t/elasticsearch-get-a-selection-of-predefined-types-as-result-in-one-query/10507>\
**Category:** Elasticsearch\
**Created:** [January 25, 2013, 2:10pm UTC](https://discuss.elastic.co/t/elasticsearch-get-a-selection-of-predefined-types-as-result-in-one-query/10507 "2013-01-25T14:10:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsteinmann](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@dsteinmann](https://discuss.elastic.co/u/dsteinmann)\
**Post date:** [January 25, 2013, 2:10pm UTC](https://discuss.elastic.co/t/elasticsearch-get-a-selection-of-predefined-types-as-result-in-one-query/10507/1 "2013-01-25T14:10:48Z")

</div>

I've got an ElasticSearch index with a large set of product properties.  
They are all looking like that:

{'\_id':1,'type':'manufacturer','name':'Toyota'},  
{'\_id':2,'type':'color','name':'Green'},  
{'\_id':3,'type':'category','name':'SUV Cars'},  
{'\_id':4,'type':'material','name':'Leather'},  
{'\_id':5,'type':'manufacturer','name':'BMW'},  
{'\_id':6,'type':'color','name':'Red'},  
{'\_id':7,'type':'category','name':'Cabrios'},  
{'\_id':8,'type':'material','name':'Steel'},  
{'\_id':9,'type':'category','name':'Cabrios Hardtop'},  
{'\_id':10,'type':'category','name':'Cabrios Softtop'},  
... and 1 Mio. more ...

There are 4 different types of product properties existing: Categories,  
Manufacturers, Colors and Materials.

The question: How can i query _with only one query_ (it's a settled  
performance requirement) the _best matching result for each type_?

So if i request a full text search query i.e. "Green Toyota Cabrios" i  
should get the following results:

{'\_id':2,'type':'color','name':'Green'},  
{'\_id':1,'type':'manufacturer','name':'Toyota'},  
{'\_id':7,'type':'category','name':'Cabrios'},  
{one matching result of the 'material'-type if found by the query}

That would be the perfect result set, _always at maximum 4 results (for  
each 'type' one result)_. If there is _no matching result for a specific  
type available there should be just 3 result items returned_.

How is that possible with Elasticsearch? Thanks for your ideas!

--

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [January 25, 2013, 3:42pm UTC](https://discuss.elastic.co/t/elasticsearch-get-a-selection-of-predefined-types-as-result-in-one-query/10507/2 "2013-01-25T15:42:01Z")

</div>

> The question: How can i query with only one query (it's a settled  
> performance requirement) the best matching result for each type?

You can't. Not without combining combinations of types into a single  
document.

All I can suggest is that you use multi-search to run a query on each  
category. It'd still be a single request

clint

--

---

<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:54am UTC](https://discuss.elastic.co/t/elasticsearch-get-a-selection-of-predefined-types-as-result-in-one-query/10507/3 "2017-07-06T02:54:35Z")

</div>


