# Nested Query vs. Has Child Query

**URL:** <https://discuss.elastic.co/t/nested-query-vs-has-child-query/10304>\
**Category:** Elasticsearch\
**Created:** [January 10, 2013, 11:34pm UTC](https://discuss.elastic.co/t/nested-query-vs-has-child-query/10304 "2013-01-10T23:34:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![otisg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/otisg/32/492_2.png) [@otisg](https://discuss.elastic.co/u/otisg)\
**Post date:** [January 10, 2013, 11:34pm UTC](https://discuss.elastic.co/t/nested-query-vs-has-child-query/10304/1 "2013-01-10T23:34:41Z")

</div>

Hi,

What exactly is the difference between a Nested Query and a Has Child  
Query? Is the main difference around the indexing and updating (all docs  
in case of nested docs and individual docs in case or parent-child docs)?

When is the former better or more appropriate than the latter?

Is there inherent (search) performance difference?

## Thanks, Otis

ELASTICSEARCH Performance Monitoring - [http://sematext.com/spm/index.html](http://sematext.com/spm/index.html)

--

---

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [January 11, 2013, 11:32am UTC](https://discuss.elastic.co/t/nested-query-vs-has-child-query/10304/2 "2013-01-11T11:32:36Z")

</div>

Hi Otis,

the last time I was looking at this the advantage of Nested Documents is  
better performance but less flexibility as opposed to Parent/Child  
documents.

The performance of Nested Documents is driven by Lucene level  
implementation. Check  
[http://lucene.apache.org/core/3\_6\_1/api/all/org/apache/lucene/search/join/ToParentBlockJoinQuery.htmlfor](http://lucene.apache.org/core/3_6_1/api/all/org/apache/lucene/search/join/ToParentBlockJoinQuery.htmlfor)  
more details.

The downside is that any update in parent or child documents means  
reindexing of whole block.

Lukas

On Fri, Jan 11, 2013 at 12:34 AM, Otis Gospodnetic \<  
[otis.gospodnetic@gmail.com](mailto:otis.gospodnetic@gmail.com)\> wrote:

> Hi,
> 
> What exactly is the difference between a Nested Query and a Has Child  
> Query? Is the main difference around the indexing and updating (all docs  
> in case of nested docs and individual docs in case or parent-child docs)?
> 
> When is the former better or more appropriate than the latter?
> 
> Is there inherent (search) performance difference?
> 
> ## Thanks, Otis
> 
> ELASTICSEARCH Performance Monitoring - [Sematext Monitoring | Infrastructure Monitoring Service](http://sematext.com/spm/index.html)
> 
> --

--

---

<div class="post-metadata">

**Author:** ![otisg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/otisg/32/492_2.png) [@otisg](https://discuss.elastic.co/u/otisg)\
**Post date:** [January 11, 2013, 6:50pm UTC](https://discuss.elastic.co/t/nested-query-vs-has-child-query/10304/3 "2013-01-11T18:50:43Z")

</div>

Hi Lukáš,  
("I put diacritics back in your name" ;))

Ah, I see, ES uses Lucene's BlockJoinQuery for Nested Query, of course!  
Thanks for the pointer.

Good explanation of what happens under the hood:

> **[Searching relational content with Lucene's BlockJoinQuery](https://blog.mikemccandless.com/2012/01/searching-relational-content-with.html)**
>
> Lucene's 3.4.0 release adds a new feature called index-time join (also sometimes called sub-documents, nested documents or parent/child d...

## Otis

ELASTICSEARCH Performance Monitoring - [Sematext Monitoring | Infrastructure Monitoring Service](http://sematext.com/spm/index.html)

On Friday, January 11, 2013 6:32:36 AM UTC-5, Lukáš Vlček wrote:

> Hi Otis,
> 
> the last time I was looking at this the advantage of Nested Documents is  
> better performance but less flexibility as opposed to Parent/Child  
> documents.
> 
> The performance of Nested Documents is driven by Lucene level  
> implementation. Check  
> [http://lucene.apache.org/core/3\_6\_1/api/all/org/apache/lucene/search/join/ToParentBlockJoinQuery.htmlfor](http://lucene.apache.org/core/3_6_1/api/all/org/apache/lucene/search/join/ToParentBlockJoinQuery.htmlfor) more details.
> 
> The downside is that any update in parent or child documents means  
> reindexing of whole block.
> 
> Lukas
> 
> On Fri, Jan 11, 2013 at 12:34 AM, Otis Gospodnetic \<[otis.gos...@gmail.com](mailto:otis.gos...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi,
> > 
> > What exactly is the difference between a Nested Query and a Has Child  
> > Query? Is the main difference around the indexing and updating (all docs  
> > in case of nested docs and individual docs in case or parent-child docs)?
> > 
> > When is the former better or more appropriate than the latter?
> > 
> > Is there inherent (search) performance difference?
> > 
> > ## Thanks, Otis
> > 
> > ELASTICSEARCH Performance Monitoring - [Sematext Monitoring | Infrastructure Monitoring Service](http://sematext.com/spm/index.html)
> > 
> > --

--

---

<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, 2:56am UTC](https://discuss.elastic.co/t/nested-query-vs-has-child-query/10304/4 "2017-07-06T02:56:51Z")

</div>


