# 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/10506>\
**Category:** Elasticsearch\
**Created:** [January 25, 2013, 2:05pm UTC](https://discuss.elastic.co/t/elasticsearch-get-a-selection-of-predefined-types-as-result-in-one-query/10506 "2013-01-25T14:05:25Z")\
**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:05pm UTC](https://discuss.elastic.co/t/elasticsearch-get-a-selection-of-predefined-types-as-result-in-one-query/10506/1 "2013-01-25T14:05:25Z")

</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!

More here:

> <https://stackoverflow.com/questions/14523222/elasticsearch-get-a-selection-of-predefined-types-as-result-in-one-query>

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 25, 2013, 4:00pm UTC](https://discuss.elastic.co/t/elasticsearch-get-a-selection-of-predefined-types-as-result-in-one-query/10506/2 "2013-01-25T16:00:11Z")

</div>

I just answered on SOF:

I don't understand clearly your use case. What are you indexing in fact?  
If you index cars, you should index it like:

```
{
  "color": "Green",
  "manufacturer": "Toyota",
  "category": "Cabrios"
}

```

That said, from the question you ask:

You can probably define your fields as not\_indexed. That way, if you search for "Green Toyota Cabrios" if field "name" you won't get "Cabrios Hardtop".

Not sure I really answered but I don't see your use case…

Le 25 janv. 2013 à 15:05, dsteinmann [david.steinmann@jiffuy.com](mailto:david.steinmann@jiffuy.com) a écrit :

> 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!
> 
> More here: [search - Elasticsearch get a selection of predefined types as result in one query - Stack Overflow](http://stackoverflow.com/questions/14523222/elasticsearch-get-a-selection-of-predefined-types-as-result-in-one-query)
> 
> --

--

---

<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/10506/3 "2017-07-06T02:54:34Z")

</div>


