# Chaining queries across multiple types

**URL:** https://discuss.elastic.co/t/chaining-queries-across-multiple-types/21197
**Category:** Elasticsearch
**Created:** [December 10, 2014, 6:46pm UTC](https://discuss.elastic.co/t/chaining-queries-across-multiple-types/21197 "2014-12-10T18:46:54Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Rajesh\_Shetty](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@Rajesh\_Shetty](https://discuss.elastic.co/u/Rajesh_Shetty)
#### Post date: [December 10, 2014, 6:46pm UTC](https://discuss.elastic.co/t/chaining-queries-across-multiple-types/21197/1 "2014-12-10T18:46:54Z")

</div>

I have following application.

Product (document of \_type : product) belongs to Category (document of  
\_type : category) . User (document of \_type : user) can own these  
products under any categories. 1 product can be under more than one  
category. couple of ways of storing the product/category in user doc

_Requirement_

When consumer type keywords (they could type products or categories) in an  
open text field, I need to find users who own these products/categories  
(based on the keywords typed)

_Model structure_

_Product_

{  
" \_id" : "p100",  
"\_type" : "product",  
"name" : "camera"  
"categories" :["c100","c110"]  
}

_Category_

{  
" \_id" : "c100",  
"\_type" : "category",  
"name" : "electronics"  
}

{  
" \_id" : "c110",  
"\_type" : "category",  
"name" : "baby"  
}

_approach 1 for user doc_

Storing in _denormalized_ form product , category name itself in the

{  
" \_id" : "\<user\_id\>",  
"name" : "John Smith",  
"product\_categories" :[  
{  
"product" : "tv",  
"category" : "electronics"  
},  
{  
"product" : "camera",  
"category" : "electronics"  
}  
]  
}

{  
" \_id" : "\<user\_id\>",  
"name" : "Jane Smith",  
"product\_categories" :[  
{  
"product" : "camera",  
"category" : "baby"  
}  
]  
}

_approach 2 for user doc_

Storing in _normalized_ form with product id , category id .

{  
" \_id" : "\<user\_id\>",  
"name" : "John Smith",  
"product\_categories" :[  
{  
"product" : "p100",  
"category" : "c100"  
}  
]  
}

{  
" \_id" : "\<user\_id\>",  
"name" : "Jane Smith",  
"product\_categories" :[  
{  
"product" : "p100",  
"category" : "c110"  
}  
]  
}

_Approach advantages and disadvantages_

_Approach #1_

_advantage_

- Possibly one query can achieve getting list of users with search text  
as "_camera baby_"

_disadvantage_

- If ever we change product name "camera" or category name "baby" we  
will have a hell updating all the documents with these product and category  
names.

_Approach #2_

_advantage_

- If ever we change product name "camera" or category name "baby" , we  
just change the name in product and category documents and all users are  
still related to the new names with product id and category id in the user  
documents

_disadvantage_

- we will have to do _MORE_ _THAN 1 query_ to list of users with search  
text as "_camera baby_"

So when someone searches

- 

```
"camera electronics" - john smith should show up in search result

```

- 

```
"camera baby" - jane smith should show up in search result

```

- 

```
"camera" - john smith and jane smith should show up in search 

```

result

_Million $ question_

Assuming I decided to go with approach #2 . How do I achieve this query in  
Elasticsearch ?

My approach

1. Fire first query against elastic search on _MULTIPLE TYPES (_ product  
and category type index) with term "camera baby" . Which will return  
documents matching different types.
2. Group document ids based on types (application side work)
3. Fire second query against elastic search on a \*USER TYPE index \*with  
respective ids obtained from step 2.

_So the question is ...._

1. Is it even possible to fire "camera baby" query against multiple  
types under same index in elastic search (remember Im doing this since  
when someone enters "camera baby" we don't know if each term is a product  
or a category ? )
2. Is there any way to chain query in step1 and step3 without grouping  
ids on the application side ?
3. Is there any better way to achieve what I'm doing ( in terms  
model/query/id mapping etc) above in elastic search assuming I want to  
retain single place to change product /category names so that its  
automatically reflected to all users who own those products in specific  
category?

--  
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/6688550a-748c-4d7c-870e-0cf78b6ff83c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6688550a-748c-4d7c-870e-0cf78b6ff83c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:44am UTC](https://discuss.elastic.co/t/chaining-queries-across-multiple-types/21197/2 "2017-07-06T00:44:22Z")

</div>


