# Search on only parent documents?

**URL:** https://discuss.elastic.co/t/search-on-only-parent-documents/8685
**Category:** Elasticsearch
**Created:** [August 8, 2012, 8:06pm UTC](https://discuss.elastic.co/t/search-on-only-parent-documents/8685 "2012-08-08T20:06:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![shane](https://avatars.discourse-cdn.com/v4/letter/s/dbc845/32.png) [@shane](https://discuss.elastic.co/u/shane)
#### Post date: [August 8, 2012, 8:06pm UTC](https://discuss.elastic.co/t/search-on-only-parent-documents/8685/1 "2012-08-08T20:06:53Z")

</div>

This might be a simple question, and if so I apologize, but I'm not sure  
how to go about this.

How would I execute a query against only top-level documents? I have an  
index of documents, some have child documents, but I want to exclude the  
children from the query.

Thanks!

---

<div class="post-metadata">

### Author: ![Radu\_Gheorghe1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe1/32/2688_2.png) [@Radu\_Gheorghe1](https://discuss.elastic.co/u/Radu_Gheorghe1)
#### Post date: [August 9, 2012, 7:03am UTC](https://discuss.elastic.co/t/search-on-only-parent-documents/8685/2 "2012-08-09T07:03:40Z")

</div>

Hi Shane,

If you have them all in one type, you can search in that type. Like:

curl -XPOST localhost:9200/myindex/mytype/\_search

On Wednesday, August 8, 2012 10:06:53 PM UTC+2, shane wrote:

> This might be a simple question, and if so I apologize, but I'm not sure  
> how to go about this.
> 
> How would I execute a query against only top-level documents? I have an  
> index of documents, some have child documents, but I want to exclude the  
> children from the query.
> 
> Thanks!

---

<div class="post-metadata">

### Author: ![olof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olof/32/2721_2.png) [@olof](https://discuss.elastic.co/u/olof)
#### Post date: [August 9, 2012, 7:46am UTC](https://discuss.elastic.co/t/search-on-only-parent-documents/8685/3 "2012-08-09T07:46:18Z")

</div>

There's a filter for this:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Does this help? I haven't used parent-child relations in ES myself.

Den onsdagen den 8:e augusti 2012 kl. 22:06:53 UTC+2 skrev shane:

> This might be a simple question, and if so I apologize, but I'm not sure  
> how to go about this.
> 
> How would I execute a query against only top-level documents? I have an  
> index of documents, some have child documents, but I want to exclude the  
> children from the query.
> 
> Thanks!

---

<div class="post-metadata">

### Author: ![Shane\_Iseminger](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@Shane\_Iseminger](https://discuss.elastic.co/u/Shane_Iseminger)
#### Post date: [August 9, 2012, 5:24pm UTC](https://discuss.elastic.co/t/search-on-only-parent-documents/8685/4 "2012-08-09T17:24:11Z")

</div>

Hi olof,

I did see that, but it seems like that only controls whether or not  
results have children. What I want to do is match against all top-  
level docs (regardless of existence or number of children), but not  
match against any children.

Shane

On Aug 9, 2012, at 12:46 AM, olof wrote:

> There's a filter for this:  
> [http://www.elasticsearch.org/guide/reference/query-dsl/has-child-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/has-child-filter.html)
> 
> Does this help? I haven't used parent-child relations in ES myself.
> 
> Den onsdagen den 8:e augusti 2012 kl. 22:06:53 UTC+2 skrev shane:  
> This might be a simple question, and if so I apologize, but I'm not  
> sure how to go about this.
> 
> How would I execute a query against only top-level documents? I have  
> an index of documents, some have child documents, but I want to  
> exclude the children from the query.
> 
> Thanks!

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [August 9, 2012, 7:41pm UTC](https://discuss.elastic.co/t/search-on-only-parent-documents/8685/5 "2012-08-09T19:41:21Z")

</div>

Typically, parent and child documents have different types. If this is your  
case, all you need to do is to limit your search to the parent type. If for  
some reason you used the same type, you can find all records that have no  
parents specified by searching for -\_parent:\* like this:

curl "[http://localhost:9200/your-index/your-type/\_search?q=-\_parent:](http://localhost:9200/your-index/your-type/_search?q=-_parent:)\*"

This will return all nodes that have empty \_parent field.

Igor

On Thursday, August 9, 2012 1:24:11 PM UTC-4, shane wrote:

> Hi olof,
> 
> I did see that, but it seems like that only controls whether or not  
> results have children. What I want to do is match against all top-  
> level docs (regardless of existence or number of children), but not  
> match against any children.
> 
> Shane
> 
> On Aug 9, 2012, at 12:46 AM, olof wrote:
> 
> > There's a filter for this:
> 
> [http://www.elasticsearch.org/guide/reference/query-dsl/has-child-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/has-child-filter.html)
> 
> > Does this help? I haven't used parent-child relations in ES myself.
> > 
> > Den onsdagen den 8:e augusti 2012 kl. 22:06:53 UTC+2 skrev shane:  
> > This might be a simple question, and if so I apologize, but I'm not  
> > sure how to go about this.
> > 
> > How would I execute a query against only top-level documents? I have  
> > an index of documents, some have child documents, but I want to  
> > exclude the children from the query.
> > 
> > Thanks!

---

<div class="post-metadata">

### Author: ![Shane\_Iseminger](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@Shane\_Iseminger](https://discuss.elastic.co/u/Shane_Iseminger)
#### Post date: [August 9, 2012, 9:04pm UTC](https://discuss.elastic.co/t/search-on-only-parent-documents/8685/6 "2012-08-09T21:04:38Z")

</div>

Aha, thanks Igor. Yes, I do happen to be using the same type for  
parent and child. In this case, it's an index of products, and  
children are variations of those products.

Shane

On Aug 9, 2012, at 12:41 PM, Igor Motov wrote:

> Typically, parent and child documents have different types. If this  
> is your case, all you need to do is to limit your search to the  
> parent type. If for some reason you used the same type, you can find  
> all records that have no parents specified by searching for -  
> \_parent:\* like this:
> 
> curl "[http://localhost:9200/your-index/your-type/\_search?q=-\_parent:](http://localhost:9200/your-index/your-type/_search?q=-_parent:)\*"
> 
> This will return all nodes that have empty \_parent field.
> 
> Igor
> 
> On Thursday, August 9, 2012 1:24:11 PM UTC-4, shane wrote:  
> Hi olof,
> 
> I did see that, but it seems like that only controls whether or not  
> results have children. What I want to do is match against all top-  
> level docs (regardless of existence or number of children), but not  
> match against any children.
> 
> Shane
> 
> On Aug 9, 2012, at 12:46 AM, olof wrote:
> 
> > There's a filter for this:  
> > [http://www.elasticsearch.org/guide/reference/query-dsl/has-child-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/has-child-filter.html)
> > 
> > Does this help? I haven't used parent-child relations in ES myself.
> > 
> > Den onsdagen den 8:e augusti 2012 kl. 22:06:53 UTC+2 skrev shane:  
> > This might be a simple question, and if so I apologize, but I'm not  
> > sure how to go about this.
> > 
> > How would I execute a query against only top-level documents? I have  
> > an index of documents, some have child documents, but I want to  
> > exclude the children from the query.
> > 
> > Thanks!

---

<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:16am UTC](https://discuss.elastic.co/t/search-on-only-parent-documents/8685/7 "2017-07-06T03:16:53Z")

</div>


