Searching Parent and Child Documents together as one document?

(Udit Narayan) #1


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


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": {
"_id": {
"path": "postid"
"_routing": {
"path": "postsubjectid",
"_parent" : {
"type" : "post"
"properties": {
"postid": {
"type": "long"
"postsubjectid": {
"type": "long"
"type": "string",
"index": "analyzed"
"type": "string",
"index": "analyzed"
"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
To view this discussion on the web visit
For more options, visit

(Mishakogan) #2

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

(system) #3