# Using result of one query in another query

**URL:** https://discuss.elastic.co/t/using-result-of-one-query-in-another-query/7240
**Category:** Elasticsearch
**Created:** [April 5, 2012, 10:59am UTC](https://discuss.elastic.co/t/using-result-of-one-query-in-another-query/7240 "2012-04-05T10:59:52Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![bgossey](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@bgossey](https://discuss.elastic.co/u/bgossey)
#### Post date: [April 5, 2012, 10:59am UTC](https://discuss.elastic.co/t/using-result-of-one-query-in-another-query/7240/1 "2012-04-05T10:59:52Z")

</div>

Hey,

Is the following query possible with elastic search:

I have two types: company and category

- Company is a type containing fields like name, phone, keywords, location…  
and also a special field called category-id which contains an id (number)

company {  
name:truvo,  
phone:(0032)123.32.42  
keywords: yellow pages, white pages  
…  
category-id: 1234  
}

- Category is a type containing an id (the category id mentioned above) and  
a set of names for that category

category {  
category-id:1234  
names: software, applications  
}

Now I want to do a query: give me all companies where the category is  
software and it should return the list of companies in this category (so it  
would include Truvo). ~ a join in sql

Of course I can merge the two types before indexing, but keeping it in two  
separate types provides me the flexibility to update the listings (for  
example adding a synonym computer programs to the category 1234) without the  
need to reindex the companies.

I could of course do this also by doing two queries (the first one to  
retrieve the categories, because it could be more than one category) and  
then doing a second query using the result of the first query, but I would  
like (id possible) to do this in one query.

Any suggestions?

thanks,  
Bert

--  
View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/Using-result-of-one-query-in-another-query-tp3886873p3886873.html](http://elasticsearch-users.115913.n3.nabble.com/Using-result-of-one-query-in-another-query-tp3886873p3886873.html)  
Sent from the ElasticSearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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, 3:33am UTC](https://discuss.elastic.co/t/using-result-of-one-query-in-another-query/7240/2 "2017-07-06T03:33:29Z")

</div>


