# How to query data with parent and child documents

**URL:** <https://discuss.elastic.co/t/how-to-query-data-with-parent-and-child-documents/86461>\
**Category:** Elasticsearch\
**Created:** [May 19, 2017, 4:04pm UTC](https://discuss.elastic.co/t/how-to-query-data-with-parent-and-child-documents/86461 "2017-05-19T16:04:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pravekal](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@pravekal](https://discuss.elastic.co/u/pravekal)\
**Post date:** [May 19, 2017, 4:04pm UTC](https://discuss.elastic.co/t/how-to-query-data-with-parent-and-child-documents/86461/1 "2017-05-19T16:04:27Z")

</div>

I have indexed documents of two type One Parent doc will have many child docs. Child docs just contain key value pair. Parent doc has list of keys.

I would like to query where query matches the parent doc and we need to filter the docs based on the key for both parent and child. Applying filter will return only one child doc so I can use that for filtering. Child doc values are used in score function for ranking purpose.

Can someone suggest how to do this ?

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [May 26, 2017, 10:37am UTC](https://discuss.elastic.co/t/how-to-query-data-with-parent-and-child-documents/86461/2 "2017-05-26T10:37:07Z")

</div>

For matching both parent and child you could query on parent using a bool query to combine the query on the parent and a [Has Child](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html) 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:** [June 23, 2017, 10:37am UTC](https://discuss.elastic.co/t/how-to-query-data-with-parent-and-child-documents/86461/3 "2017-06-23T10:37:30Z")

</div>

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