# Build a Query based off the result of another query

**URL:** <https://discuss.elastic.co/t/build-a-query-based-off-the-result-of-another-query/288174>\
**Category:** Elasticsearch\
**Created:** [November 1, 2021, 9:10pm UTC](https://discuss.elastic.co/t/build-a-query-based-off-the-result-of-another-query/288174 "2021-11-01T21:10:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wonjun\_Cho](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wonjun_cho/32/96600_2.png) [@Wonjun\_Cho](https://discuss.elastic.co/u/Wonjun_Cho)\
**Post date:** [November 1, 2021, 9:10pm UTC](https://discuss.elastic.co/t/build-a-query-based-off-the-result-of-another-query/288174/1 "2021-11-01T21:10:41Z")

</div>

Hi guys,  
I would like to know if it is possible to make a query based off the result of another query.  
For example, there are indices for school and students.

```auto
School
{
	"settings": { ... },
	"mappings": {
		"properties": {
			"schoolId":{
				"type":"keyword"
			},
			"students":{
				"type":"keyword" //array of student ids
			},
			...
		}
	}
}

Students
{
	"settings": { ... },
	"mappings": {
		"properties": {
			"studentId":{
				"type":"keyword"
			},
			"firstName":{
				"type":"text"
			},
			...
		}
	}
}

```

If I would like to get all the schools that have the students whose first name is "John," is there any way that I can make a query to get the student ids with a name with "John" and get all the schools based on the results?

I think it would be similar to make sub queries in SQL, but I cannot find a way in Elasticsearch.

Is it possible in elastic query? If it is, then how should I make a query?

---

<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:** [November 4, 2021, 12:31am UTC](https://discuss.elastic.co/t/build-a-query-based-off-the-result-of-another-query/288174/2 "2021-11-04T00:31:51Z")

</div>

Welcome to our community! 😃 We aren't all guys though 🙂

> [@Wonjun\_Cho](#):
>
> I would like to know if it is possible to make a query based off the result of another query.

Yes you can, but you need to handle that in your client code. There's nothing native to Elasticsearch that will do this for you.

---

<div class="post-metadata">

**Author:** ![Wonjun\_Cho](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wonjun_cho/32/96600_2.png) [@Wonjun\_Cho](https://discuss.elastic.co/u/Wonjun_Cho)\
**Post date:** [November 5, 2021, 7:48pm UTC](https://discuss.elastic.co/t/build-a-query-based-off-the-result-of-another-query/288174/3 "2021-11-05T19:48:51Z")

</div>

Thank you!

---

<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:** [December 3, 2021, 7:49pm UTC](https://discuss.elastic.co/t/build-a-query-based-off-the-result-of-another-query/288174/4 "2021-12-03T19:49:44Z")

</div>

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