# How to retrieve children with its parent?

**URL:** https://discuss.elastic.co/t/how-to-retrieve-children-with-its-parent/9797
**Category:** Elasticsearch
**Created:** [November 22, 2012, 9:40pm UTC](https://discuss.elastic.co/t/how-to-retrieve-children-with-its-parent/9797 "2012-11-22T21:40:49Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![hdvyas007](https://avatars.discourse-cdn.com/v4/letter/h/a6a055/32.png) [@hdvyas007](https://discuss.elastic.co/u/hdvyas007)
#### Post date: [November 22, 2012, 9:40pm UTC](https://discuss.elastic.co/t/how-to-retrieve-children-with-its-parent/9797/1 "2012-11-22T21:40:49Z")

</div>

Hello All,

I am working on Parent-Child document in my current project. I want to  
fetch parent documents and child documents with its parent document if  
child documents satisfy search term.

Following is my current scenario and my questions.

Parent Document Mapping

Parent Document Name

Authors

Fields

AuthorCode

AuthorName

Parent Document Data

Id

AuthorName

1

John

2

Marry

3

Waulter

Child Document Mapping

Child Document Name

Book

Fields

BookName

Genre

Publisher

Child Document Data

ParentId/AuthorId

ChildId

BookName

Genre

Publisher

1

1

MerryChristmas

Family

McGrow

2

2

Johncarter

Adventure

Deitel

2

3

Java Bible

Programming

BPB

3

4

How to play flute

Self Study

Johnson

Now from above parent and child data, I would like to search term which is  
start with “john”. So I would expect following output. Please note that I  
would expect parent who satisfy search term and child (with ITS PARENT) who  
satisfy search term. Following should be expected output. Last column of  
following result explain why particular document does appear in search  
result.

AuthorName

BookName

Genre

Publisher

Reason to display in result

John

N/A

N/A

N/A

Author name start with John(only parent doc will return)

Marry

Johncarter

Adventure

Deitel

Bookname start with John(Parent doc along with child doc return)

Waulter

flute

Self Study

Johnson

Publisher start with John(Parent doc along with child doc return)

So my question is How can we write query that return parent and child with  
its parent?

Thank you?

--

---

<div class="post-metadata">

### Author: ![Gaurav\_Bansal](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@Gaurav\_Bansal](https://discuss.elastic.co/u/Gaurav_Bansal)
#### Post date: [November 23, 2012, 6:19am UTC](https://discuss.elastic.co/t/how-to-retrieve-children-with-its-parent/9797/2 "2012-11-23T06:19:34Z")

</div>

Hi,

Well , you can't get both parent and child at same time because ES does  
not have any concept of joins.  
You can embed the authors and their books ( children as per your schema )  
in 1 document itself , the "books" field being the nested one , and then  
fire a query  
matching your criteria ( i.e starting with 'john' here )  
But this will give you hit per doc and you will get a document having 1  
author and all his books even if only one of his book's fields has 'john'  
keyword

Second approach can be using has\_child or\_has parent queries and doing  
manual joining at client side

Regards  
Gaurav

On Friday, November 23, 2012 3:10:49 AM UTC+5:30, hdvyas wrote:

> Hello All,
> 
> I am working on Parent-Child document in my current project. I want to  
> fetch parent documents and child documents with its parent document if  
> child documents satisfy search term.
> 
> Following is my current scenario and my questions.
> 
> Parent Document Mapping
> 
> Parent Document Name
> 
> Authors
> 
> Fields
> 
> AuthorCode
> 
> AuthorName
> 
> Parent Document Data
> 
> Id
> 
> AuthorName
> 
> 1
> 
> John
> 
> 2
> 
> Marry
> 
> 3
> 
> Waulter
> 
> Child Document Mapping
> 
> Child Document Name
> 
> Book
> 
> Fields
> 
> BookName
> 
> Genre
> 
> Publisher
> 
> Child Document Data
> 
> ParentId/AuthorId
> 
> ChildId
> 
> BookName
> 
> Genre
> 
> Publisher
> 
> 1
> 
> 1
> 
> MerryChristmas
> 
> Family
> 
> McGrow
> 
> 2
> 
> 2
> 
> Johncarter
> 
> Adventure
> 
> Deitel
> 
> 2
> 
> 3
> 
> Java Bible
> 
> Programming
> 
> BPB
> 
> 3
> 
> 4
> 
> How to play flute
> 
> Self Study
> 
> Johnson
> 
> Now from above parent and child data, I would like to search term which is  
> start with “john”. So I would expect following output. Please note that I  
> would expect parent who satisfy search term and child (with ITS PARENT) who  
> satisfy search term. Following should be expected output. Last column of  
> following result explain why particular document does appear in search  
> result.
> 
> AuthorName
> 
> BookName
> 
> Genre
> 
> Publisher
> 
> Reason to display in result
> 
> John
> 
> N/A
> 
> N/A
> 
> N/A
> 
> Author name start with John(only parent doc will return)
> 
> Marry
> 
> Johncarter
> 
> Adventure
> 
> Deitel
> 
> Bookname start with John(Parent doc along with child doc return)
> 
> Waulter
> 
> flute
> 
> Self Study
> 
> Johnson
> 
> Publisher start with John(Parent doc along with child doc return)
> 
> So my question is How can we write query that return parent and child with  
> its parent?
> 
> Thank you?

--

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [November 23, 2012, 6:53am UTC](https://discuss.elastic.co/t/how-to-retrieve-children-with-its-parent/9797/3 "2012-11-23T06:53:23Z")

</div>

+1 : if you are searching for books, index books with all search fields. You are in the NoSQL world now and you must forget almost everything you know about relational databases.

In you use case, once the book has been written, the author name won't change. So it really make sense to index it within the book. It's like taking a snapshot of your book cover. Author is part of it.

Make sense?

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 23 nov. 2012 à 07:19, Gaurav Bansal [shehzada.gaurav@gmail.com](mailto:shehzada.gaurav@gmail.com) a écrit :

Hi,

Well , you can't get both parent and child at same time because ES does not have any concept of joins.  
You can embed the authors and their books ( children as per your schema ) in 1 document itself , the "books" field being the nested one , and then fire a query  
matching your criteria ( i.e starting with 'john' here )  
But this will give you hit per doc and you will get a document having 1 author and all his books even if only one of his book's fields has 'john' keyword

Second approach can be using has\_child or\_has parent queries and doing manual joining at client side

Regards  
Gaurav

On Friday, November 23, 2012 3:10:49 AM UTC+5:30, hdvyas wrote:

> Hello All,
> 
> I am working on Parent-Child document in my current project. I want to fetch parent documents and child documents with its parent document if child documents satisfy search term.
> 
> Following is my current scenario and my questions.
> 
> Parent Document Mapping
> 
> Parent Document Name  
> Authors  
> Fields  
> AuthorCode
> 
> AuthorName
> 
> Parent Document Data
> 
> Id  
> AuthorName  
> 1  
> John  
> 2  
> Marry  
> 3  
> Waulter
> 
> Child Document Mapping
> 
> Child Document Name  
> Book  
> Fields  
> BookName
> 
> Genre
> 
> Publisher
> 
> Child Document Data
> 
> ParentId/AuthorId  
> ChildId  
> BookName  
> Genre  
> Publisher  
> 1  
> 1  
> MerryChristmas  
> Family  
> McGrow  
> 2  
> 2  
> Johncarter  
> Adventure  
> Deitel  
> 2  
> 3  
> Java Bible  
> Programming  
> BPB  
> 3  
> 4  
> How to play flute  
> Self Study  
> Johnson
> 
> Now from above parent and child data, I would like to search term which is start with “john”. So I would expect following output. Please note that I would expect parent who satisfy search term and child (with ITS PARENT) who satisfy search term. Following should be expected output. Last column of following result explain why particular document does appear in search result.
> 
> AuthorName  
> BookName  
> Genre  
> Publisher  
> Reason to display in result  
> John  
> N/A  
> N/A  
> N/A  
> Author name start with John(only parent doc will return)  
> Marry  
> Johncarter  
> Adventure  
> Deitel  
> Bookname start with John(Parent doc along with child doc return)  
> Waulter  
> flute  
> Self Study  
> Johnson  
> Publisher start with John(Parent doc along with child doc return)
> 
> So my question is How can we write query that return parent and child with its parent?
> 
> Thank you?

--

--

---

<div class="post-metadata">

### Author: ![hdvyas007](https://avatars.discourse-cdn.com/v4/letter/h/a6a055/32.png) [@hdvyas007](https://discuss.elastic.co/u/hdvyas007)
#### Post date: [November 23, 2012, 9:52am UTC](https://discuss.elastic.co/t/how-to-retrieve-children-with-its-parent/9797/4 "2012-11-23T09:52:36Z")

</div>

We are providing google like search(There will be single text box and user  
will enter whatever they want to search) so client side operation will be  
very costly for us. Any way I do really appreciated your help.....!! Thank  
you

On Friday, November 23, 2012 8:19:34 AM UTC+2, Gaurav Bansal wrote:

> Hi,
> 
> Well , you can't get both parent and child at same time because ES does  
> not have any concept of joins.  
> You can embed the authors and their books ( children as per your schema )  
> in 1 document itself , the "books" field being the nested one , and then  
> fire a query  
> matching your criteria ( i.e starting with 'john' here )  
> But this will give you hit per doc and you will get a document having 1  
> author and all his books even if only one of his book's fields has 'john'  
> keyword
> 
> Second approach can be using has\_child or\_has parent queries and doing  
> manual joining at client side
> 
> Regards  
> Gaurav
> 
> On Friday, November 23, 2012 3:10:49 AM UTC+5:30, hdvyas wrote:
> 
> > Hello All,
> > 
> > I am working on Parent-Child document in my current project. I want to  
> > fetch parent documents and child documents with its parent document if  
> > child documents satisfy search term.
> > 
> > Following is my current scenario and my questions.
> > 
> > Parent Document Mapping
> > 
> > Parent Document Name
> > 
> > Authors
> > 
> > Fields
> > 
> > AuthorCode
> > 
> > AuthorName
> > 
> > Parent Document Data
> > 
> > Id
> > 
> > AuthorName
> > 
> > 1
> > 
> > John
> > 
> > 2
> > 
> > Marry
> > 
> > 3
> > 
> > Waulter
> > 
> > Child Document Mapping
> > 
> > Child Document Name
> > 
> > Book
> > 
> > Fields
> > 
> > BookName
> > 
> > Genre
> > 
> > Publisher
> > 
> > Child Document Data
> > 
> > ParentId/AuthorId
> > 
> > ChildId
> > 
> > BookName
> > 
> > Genre
> > 
> > Publisher
> > 
> > 1
> > 
> > 1
> > 
> > MerryChristmas
> > 
> > Family
> > 
> > McGrow
> > 
> > 2
> > 
> > 2
> > 
> > Johncarter
> > 
> > Adventure
> > 
> > Deitel
> > 
> > 2
> > 
> > 3
> > 
> > Java Bible
> > 
> > Programming
> > 
> > BPB
> > 
> > 3
> > 
> > 4
> > 
> > How to play flute
> > 
> > Self Study
> > 
> > Johnson
> > 
> > Now from above parent and child data, I would like to search term which  
> > is start with “john”. So I would expect following output. Please note that  
> > I would expect parent who satisfy search term and child (with ITS PARENT)  
> > who satisfy search term. Following should be expected output. Last column  
> > of following result explain why particular document does appear in search  
> > result.
> > 
> > AuthorName
> > 
> > BookName
> > 
> > Genre
> > 
> > Publisher
> > 
> > Reason to display in result
> > 
> > John
> > 
> > N/A
> > 
> > N/A
> > 
> > N/A
> > 
> > Author name start with John(only parent doc will return)
> > 
> > Marry
> > 
> > Johncarter
> > 
> > Adventure
> > 
> > Deitel
> > 
> > Bookname start with John(Parent doc along with child doc return)
> > 
> > Waulter
> > 
> > flute
> > 
> > Self Study
> > 
> > Johnson
> > 
> > Publisher start with John(Parent doc along with child doc return)
> > 
> > So my question is How can we write query that return parent and child  
> > with its parent?
> > 
> > Thank you?

--

---

<div class="post-metadata">

### Author: ![hdvyas007](https://avatars.discourse-cdn.com/v4/letter/h/a6a055/32.png) [@hdvyas007](https://discuss.elastic.co/u/hdvyas007)
#### Post date: [November 23, 2012, 9:55am UTC](https://discuss.elastic.co/t/how-to-retrieve-children-with-its-parent/9797/5 "2012-11-23T09:55:12Z")

</div>

Hi David,

It makes sense. But in future Author may have other children. for example  
author may have Publishers(Though right now we are making PublisherName  
property in Book). So we are not able to create Author as a part of book.

But I do really appreciated your help............!!! Thank you......!!!

On Friday, November 23, 2012 8:53:27 AM UTC+2, David Pilato wrote:

> +1 : if you are searching for books, index books with all search fields.  
> You are in the NoSQL world now and you must forget almost everything you  
> know about relational databases.
> 
> In you use case, once the book has been written, the author name won't  
> change. So it really make sense to index it within the book. It's like  
> taking a snapshot of your book cover. Author is part of it.
> 
> Make sense?
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 23 nov. 2012 à 07:19, Gaurav Bansal \<[shehzad...@gmail.com](mailto:shehzad...@gmail.com) \<javascript:\>\>  
> a écrit :
> 
> Hi,
> 
> Well , you can't get both parent and child at same time because ES does  
> not have any concept of joins.  
> You can embed the authors and their books ( children as per your schema )  
> in 1 document itself , the "books" field being the nested one , and then  
> fire a query  
> matching your criteria ( i.e starting with 'john' here )  
> But this will give you hit per doc and you will get a document having 1  
> author and all his books even if only one of his book's fields has 'john'  
> keyword
> 
> Second approach can be using has\_child or\_has parent queries and doing  
> manual joining at client side
> 
> Regards  
> Gaurav
> 
> On Friday, November 23, 2012 3:10:49 AM UTC+5:30, hdvyas wrote:
> 
> > Hello All,
> > 
> > I am working on Parent-Child document in my current project. I want to  
> > fetch parent documents and child documents with its parent document if  
> > child documents satisfy search term.
> > 
> > Following is my current scenario and my questions.
> > 
> > Parent Document Mapping
> > 
> > Parent Document Name
> > 
> > Authors
> > 
> > Fields
> > 
> > AuthorCode
> > 
> > AuthorName
> > 
> > Parent Document Data
> > 
> > Id
> > 
> > AuthorName
> > 
> > 1
> > 
> > John
> > 
> > 2
> > 
> > Marry
> > 
> > 3
> > 
> > Waulter
> > 
> > Child Document Mapping
> > 
> > Child Document Name
> > 
> > Book
> > 
> > Fields
> > 
> > BookName
> > 
> > Genre
> > 
> > Publisher
> > 
> > Child Document Data
> > 
> > ParentId/AuthorId
> > 
> > ChildId
> > 
> > BookName
> > 
> > Genre
> > 
> > Publisher
> > 
> > 1
> > 
> > 1
> > 
> > MerryChristmas
> > 
> > Family
> > 
> > McGrow
> > 
> > 2
> > 
> > 2
> > 
> > Johncarter
> > 
> > Adventure
> > 
> > Deitel
> > 
> > 2
> > 
> > 3
> > 
> > Java Bible
> > 
> > Programming
> > 
> > BPB
> > 
> > 3
> > 
> > 4
> > 
> > How to play flute
> > 
> > Self Study
> > 
> > Johnson
> > 
> > Now from above parent and child data, I would like to search term which  
> > is start with “john”. So I would expect following output. Please note that  
> > I would expect parent who satisfy search term and child (with ITS PARENT)  
> > who satisfy search term. Following should be expected output. Last column  
> > of following result explain why particular document does appear in search  
> > result.
> > 
> > AuthorName
> > 
> > BookName
> > 
> > Genre
> > 
> > Publisher
> > 
> > Reason to display in result
> > 
> > John
> > 
> > N/A
> > 
> > N/A
> > 
> > N/A
> > 
> > Author name start with John(only parent doc will return)
> > 
> > Marry
> > 
> > Johncarter
> > 
> > Adventure
> > 
> > Deitel
> > 
> > Bookname start with John(Parent doc along with child doc return)
> > 
> > Waulter
> > 
> > flute
> > 
> > Self Study
> > 
> > Johnson
> > 
> > Publisher start with John(Parent doc along with child doc return)
> > 
> > So my question is How can we write query that return parent and child  
> > with its parent?
> > 
> > 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: [July 6, 2017, 3:03am UTC](https://discuss.elastic.co/t/how-to-retrieve-children-with-its-parent/9797/6 "2017-07-06T03:03:11Z")

</div>


