# Parent/child "where parent exists" query

**URL:** <https://discuss.elastic.co/t/parent-child-where-parent-exists-query/15384>\
**Category:** Elasticsearch\
**Created:** [January 23, 2014, 6:25pm UTC](https://discuss.elastic.co/t/parent-child-where-parent-exists-query/15384 "2014-01-23T18:25:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Bolis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_bolis/32/700_2.png) [@Thomas\_Bolis](https://discuss.elastic.co/u/Thomas_Bolis)\
**Post date:** [January 23, 2014, 6:25pm UTC](https://discuss.elastic.co/t/parent-child-where-parent-exists-query/15384/1 "2014-01-23T18:25:01Z")

</div>

Hi,

I have been working with a parent child schema creation and I was wondering if there is a way to perform a search in children documents with the query "where parent exists" and get only those documents.

I can index children documents and there is no mandatory to have parent document. Therefore, I want only to get the children who have a parent document.

Is this functionality possible? How is possible to perform such query? I work with latest version 1.0.0.RC1

Thank you  
Thomas

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/69af02d6-a717-42b4-956a-8f76c5ebb2e9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/69af02d6-a717-42b4-956a-8f76c5ebb2e9%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [January 23, 2014, 8:28pm UTC](https://discuss.elastic.co/t/parent-child-where-parent-exists-query/15384/2 "2014-01-23T20:28:04Z")

</div>

Hi Thomas,

Using a has\_child[1] filter on top of a match\_all[2] filter should work.

[1]

> **[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.

[2]

> **[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.

On Thu, Jan 23, 2014 at 7:25 PM, Thomas [thomas.bolis@gmail.com](mailto:thomas.bolis@gmail.com) wrote:

> Hi,
> 
> I have been working with a parent child schema creation and I was  
> wondering if there is a way to perform a search in children documents with  
> the query "where parent exists" and get only those documents.
> 
> I can index children documents and there is no mandatory to have parent  
> document. Therefore, I want only to get the children who have a parent  
> document.
> 
> Is this functionality possible? How is possible to perform such query? I  
> work with latest version 1.0.0.RC1
> 
> Thank you  
> Thomas
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/69af02d6-a717-42b4-956a-8f76c5ebb2e9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/69af02d6-a717-42b4-956a-8f76c5ebb2e9%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Adrien Grand

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j7ruw7nm6dCPwNF%2B6GU7A3iocRXKZbY%3DQrdoCxzMhy1fQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j7ruw7nm6dCPwNF%2B6GU7A3iocRXKZbY%3DQrdoCxzMhy1fQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Thomas\_Bolis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_bolis/32/700_2.png) [@Thomas\_Bolis](https://discuss.elastic.co/u/Thomas_Bolis)\
**Post date:** [January 24, 2014, 8:30am UTC](https://discuss.elastic.co/t/parent-child-where-parent-exists-query/15384/3 "2014-01-24T08:30:06Z")

</div>

Hi Adrien and thanks for the reply,

This sounds like what I was looking for 🙂 Will investigate it

Thanks  
Thomas

On Thursday, 23 January 2014 20:25:01 UTC+2, Thomas wrote:

> Hi,
> 
> I have been working with a parent child schema creation and I was  
> wondering if there is a way to perform a search in children documents with  
> the query "where parent exists" and get only those documents.
> 
> I can index children documents and there is no mandatory to have parent  
> document. Therefore, I want only to get the children who have a parent  
> document.
> 
> Is this functionality possible? How is possible to perform such query? I  
> work with latest version 1.0.0.RC1
> 
> Thank you  
> Thomas

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/3bda84b6-a01c-4123-a875-05d7b23e9389%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3bda84b6-a01c-4123-a875-05d7b23e9389%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Thomas\_Bolis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_bolis/32/700_2.png) [@Thomas\_Bolis](https://discuss.elastic.co/u/Thomas_Bolis)\
**Post date:** [January 24, 2014, 10:10am UTC](https://discuss.elastic.co/t/parent-child-where-parent-exists-query/15384/4 "2014-01-24T10:10:18Z")

</div>

Hi,

I have two questions with regards this feature

1. I have been reading the documentation and at some point it described  
that all \_ids are loaded into memory:

all \_id values are loaded to memory (heap) in order to support fast lookups

This means that only the parent \_ids that match correct? Not all the \_ids  
of the parent/child relationship

1. Is it possible to perform an aggregation and take some values from the  
parent document and some from the child document? for example take some  
information from the parent document (e.g. city) and some information from  
the child document (e.g. for car model= BMW) and do some counts? and  
present them all together?

e.g.

{

"parentDoc":{

"properties": {

"city":{

"type": "string",

"index": "not\_analyzed"

}

}

}

}

{

"childDoc":{

"properties": {

"carModel":{

"type": "string",

"index": "not\_analyzed"

}

}

}

}

Aggregate child documents per child Document where parent exists (because i  
have childDocuments without parent info and i do not want to take those)  
per city and carModel. Below is an idea of what I'm trying to do

curl -XGET 'localhost:9200/delivery\_logs\_pc\_idx/childDoc/\_search?pretty' -d  
'{  
"query": {  
"match\_all":{}  
},  
"aggregations" : {  
"myFilter" : {  
"filter" : {  
{  
"has\_parent" : {  
"type" : "deliveryLog",  
"query": {"match\_all":{}}  
}  
}  
},  
"aggregations" : {  
"preferrence" : { "terms" : { "script" : "  
_doc.parent.city.value_ + _doc.child.carModel.value_", "size":100 } }  
}  
}  
},  
"size":0  
}'

Is there an alternative way of achieving that?

Thank you

On Thursday, 23 January 2014 20:25:01 UTC+2, Thomas wrote:

> Hi,
> 
> I have been working with a parent child schema creation and I was  
> wondering if there is a way to perform a search in children documents with  
> the query "where parent exists" and get only those documents.
> 
> I can index children documents and there is no mandatory to have parent  
> document. Therefore, I want only to get the children who have a parent  
> document.
> 
> Is this functionality possible? How is possible to perform such query? I  
> work with latest version 1.0.0.RC1
> 
> Thank you  
> Thomas

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/05e42115-b73f-40fe-81ef-332ca0ab75b1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/05e42115-b73f-40fe-81ef-332ca0ab75b1%40googlegroups.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, 1:55am UTC](https://discuss.elastic.co/t/parent-child-where-parent-exists-query/15384/5 "2017-07-06T01:55:01Z")

</div>


