# Searching Parent and Child Documents together as one document?

**URL:** https://discuss.elastic.co/t/searching-parent-and-child-documents-together-as-one-document/17922
**Category:** Elasticsearch
**Created:** [June 5, 2014, 9:39am UTC](https://discuss.elastic.co/t/searching-parent-and-child-documents-together-as-one-document/17922 "2014-06-05T09:39:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Udit\_Narayan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/udit_narayan/32/708_2.png) [@Udit\_Narayan](https://discuss.elastic.co/u/Udit_Narayan)
#### Post date: [June 5, 2014, 9:39am UTC](https://discuss.elastic.co/t/searching-parent-and-child-documents-together-as-one-document/17922/1 "2014-06-05T09:39:48Z")

</div>

Hi

I have this scenario of discussion board where people create discussion  
thread called post. Other can comment on it called comment. Now comment i  
same as post except it as parentId stored in it. In other words, my  
database schema for post table is

Post

PostId, PostSubjectId, PostTitle, PostData, PostKind, PostDate

\*PostSubjectId \*is \*PostId \*of some post which is parent to the post and is  
null for parent post.

\*Postkind \*is a enum value which says \*Post \*for parent post and \*Comment \*for  
child posts.

Now, I have stored this document in elasticsearch with help of \_id,  
\_routing and \_parent mapping I have mapped ids and parent ids  
appropriately. My mapping looks like:

PUT search  
{  
"mappings": {  
"post":{  
"\_id": {  
"path": "postid"  
},  
"\_routing": {  
"path": "postsubjectid",  
"required":false  
},  
"\_parent" : {  
"type" : "post"  
},  
"properties": {  
"postid": {  
"type": "long"  
},  
"postsubjectid": {  
"type": "long"  
},  
"posttitle":{  
"type": "string",  
"index": "analyzed"  
},  
"postdata":{  
"type": "string",  
"index": "analyzed"  
},  
"postdate":{  
"type": "date",  
"format": "date\_hour\_minute\_second\_millis"  
},  
"postkind": {  
"type": "string",  
"index": "not\_analyzed",  
"omit\_norms": true,  
"index\_options": "docs"  
}  
}  
}  
}  
}

Every parent post can have multiple comments/child posts. Parent Posts  
along with comments ordered by PostDate is called a discussion and shown as  
discussion on user interface. We need to implement search on discussions.

I want to write a query which search across discussions not posts and  
return me parent posts when either parent post or child post has a search  
hit. I can think of has\_child clause but am confused while creating this  
query as it includes both searching in parent as well as child together.

--  
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/52d12891-5c14-4b06-81ce-295931b98e12%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/52d12891-5c14-4b06-81ce-295931b98e12%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![mishakogan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mishakogan/32/17935_2.png) [@mishakogan](https://discuss.elastic.co/u/mishakogan)
#### Post date: [May 4, 2017, 3:48pm UTC](https://discuss.elastic.co/t/searching-parent-and-child-documents-together-as-one-document/17922/2 "2017-05-04T15:48:39Z")

</div>

we are also interested in a similar concept. We want to be able to search parent and it's children as if they were one document. note, that nested-document solution is not going to work for us as we can have many children and size can be an issue since nested documents must be sent as ONE document

@Udit_Narayan: let us know if you have a solution

---

<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 5, 2017, 10:00pm UTC](https://discuss.elastic.co/t/searching-parent-and-child-documents-together-as-one-document/17922/3 "2017-07-05T22:00:35Z")

</div>


