# Has no parent query

**URL:** <https://discuss.elastic.co/t/has-no-parent-query/10973>\
**Category:** Elasticsearch\
**Created:** [March 2, 2013, 4:57am UTC](https://discuss.elastic.co/t/has-no-parent-query/10973 "2013-03-02T04:57:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![snmaynard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/snmaynard/32/2463_2.png) [@snmaynard](https://discuss.elastic.co/u/snmaynard)\
**Post date:** [March 2, 2013, 4:57am UTC](https://discuss.elastic.co/t/has-no-parent-query/10973/1 "2013-03-02T04:57:41Z")

</div>

Is there any way of querying for documents of a certain type for those that  
have no parent, or whose parent has been deleted?

I have a parent/child relationship in my system, and some parents have been  
deleted, and so I would like to delete the orphan children, as they are no  
longer required.

Thanks!

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [March 4, 2013, 10:03am UTC](https://discuss.elastic.co/t/has-no-parent-query/10973/2 "2013-03-04T10:03:11Z")

</div>

On Fri, 2013-03-01 at 20:57 -0800, Simon wrote:

> Is there any way of querying for documents of a certain type for those  
> that have no parent, or whose parent has been deleted?

Not easily...

I'd suggest doing a scan query which returns all child docs in tranches.  
Extract the parent ids from those and do an \_ids query on those. Find  
the ids which weren't returned -\> missing parents.

clint

> I have a parent/child relationship in my system, and some parents have  
> been deleted, and so I would like to delete the orphan children, as  
> they are no longer required.
> 
> Thanks!
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Hannes\_Korte](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hannes_korte/32/909_2.png) [@Hannes\_Korte](https://discuss.elastic.co/u/Hannes_Korte)\
**Post date:** [August 29, 2013, 10:50am UTC](https://discuss.elastic.co/t/has-no-parent-query/10973/3 "2013-08-29T10:50:27Z")

</div>

On Sat, 2013-03-02 05:57:41 UTC+1, Simon wrote:

> Is there any way of querying for documents of a certain type for  
> those that have no parent, or whose parent has been deleted? I have a  
> parent/child relationship in my system, and some parents have been  
> deleted, and so I would like to delete the orphan children, as they  
> are no longer required.

A rather late reply just for completeness: For me this worked using a "not"  
with a "hasParent" filter:

"filter": {  
"not": {  
"filter": {  
"has\_parent": {  
"query": {  
"match\_all": {}  
},  
"parent\_type": "XXX"  
}  
}  
}  
}

Hannes

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![simonw\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonw_2/32/1130_2.png) [@simonw\_2](https://discuss.elastic.co/u/simonw_2)\
**Post date:** [August 29, 2013, 11:59am UTC](https://discuss.elastic.co/t/has-no-parent-query/10973/4 "2013-08-29T11:59:44Z")

</div>

\nn/ 😉

On Thursday, August 29, 2013 12:50:27 PM UTC+2, Hannes Korte wrote:

> On Sat, 2013-03-02 05:57:41 UTC+1, Simon wrote:
> 
> > Is there any way of querying for documents of a certain type for  
> > those that have no parent, or whose parent has been deleted? I have a  
> > parent/child relationship in my system, and some parents have been  
> > deleted, and so I would like to delete the orphan children, as they  
> > are no longer required.
> 
> A rather late reply just for completeness: For me this worked using a  
> "not" with a "hasParent" filter:
> 
> "filter": {  
> "not": {  
> "filter": {  
> "has\_parent": {  
> "query": {  
> "match\_all": {}  
> },  
> "parent\_type": "XXX"  
> }  
> }  
> }  
> }
> 
> Hannes

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:19am UTC](https://discuss.elastic.co/t/has-no-parent-query/10973/5 "2017-07-06T02:19:01Z")

</div>


