# Has\_child and matching children

**URL:** <https://discuss.elastic.co/t/has-child-and-matching-children/11190>\
**Category:** Elasticsearch\
**Created:** [March 18, 2013, 2:40pm UTC](https://discuss.elastic.co/t/has-child-and-matching-children/11190 "2013-03-18T14:40:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pawel\_Mlynarczyk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pawel_mlynarczyk/32/1568_2.png) [@Pawel\_Mlynarczyk](https://discuss.elastic.co/u/Pawel_Mlynarczyk)\
**Post date:** [March 18, 2013, 2:40pm UTC](https://discuss.elastic.co/t/has-child-and-matching-children/11190/1 "2013-03-18T14:40:13Z")

</div>

Hello everybody

I'm wondering if there is a way of getting some information about the  
children (a list of ids would be ok) matching the has\_child query along  
with the parent object.

Example here:

I've got a parent object:

curl -XPUT 'localhost:9200/products' -d '  
{  
"mappings" : {  
"Root" : {  
"dynamic" : false,  
"properties" : {  
"id" : { "type" : "integer" }  
}  
},  
"Child" : {  
"\_parent" : { "type" : "Root" },  
"dynamic" : false,  
"properties" : {  
"id" : { "type" : "integer" },  
"name" : { "type" : "string", "index" : "not\_analyzed" }  
}  
}  
}  
}  
'

And I have a query like this:

curl -X POST 'localhost:9200/products/\_search?pretty=true' -d '  
{  
"query" : {  
"match\_all" : {}  
},  
"filter" : {  
"has\_child" : {  
"type" : "Child",  
"query" : {  
"prefix" : { "name" : "AS" }  
}  
}  
}  
}  
'

As a result I get parent documents that contains children which name starts  
with 'AS'. So is there a way of getting the information about which  
children matched the filter?

Thanks in advance  
Paweł

--  
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:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [March 25, 2013, 11:55am UTC](https://discuss.elastic.co/t/has-child-and-matching-children/11190/2 "2013-03-25T11:55:10Z")

</div>

At the moment something like this is only possible by doing subsequent  
request(s).  
For each parent documents that you want to know the top matching child docs  
for you can execute the following request:  
curl -X POST 'localhost:9200/products/Child/\_search?pretty=true' -d '  
{  
"query" : {  
"match\_all" : {}  
},  
"filter" : {  
"bool" : {  
"must" : [  
{  
"term" : {"\_parent" : "THE\_PARENT\_ID"}  
},  
{  
"prefix" : { "name" : "AS" }  
}  
]  
}  
}  
}  
'

In order to optimise the above approach you can combine the search requests  
into one request use the multi search api:

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

Hope it helps.

Martijn

On 18 March 2013 15:40, Paweł Młynarczyk [zwarios@gmail.com](mailto:zwarios@gmail.com) wrote:

> Hello everybody
> 
> I'm wondering if there is a way of getting some information about the  
> children (a list of ids would be ok) matching the has\_child query along  
> with the parent object.
> 
> Example here:
> 
> I've got a parent object:
> 
> curl -XPUT 'localhost:9200/products' -d '  
> {  
> "mappings" : {  
> "Root" : {  
> "dynamic" : false,  
> "properties" : {  
> "id" : { "type" : "integer" }  
> }  
> },  
> "Child" : {  
> "\_parent" : { "type" : "Root" },  
> "dynamic" : false,  
> "properties" : {  
> "id" : { "type" : "integer" },  
> "name" : { "type" : "string", "index" : "not\_analyzed" }  
> }  
> }  
> }  
> }  
> '
> 
> And I have a query like this:
> 
> curl -X POST 'localhost:9200/products/\_search?pretty=true' -d '  
> {  
> "query" : {  
> "match\_all" : {}  
> },  
> "filter" : {  
> "has\_child" : {  
> "type" : "Child",  
> "query" : {  
> "prefix" : { "name" : "AS" }  
> }  
> }  
> }  
> }  
> '
> 
> As a result I get parent documents that contains children which name  
> starts with 'AS'. So is there a way of getting the information about which  
> children matched the filter?
> 
> Thanks in advance  
> Paweł
> 
> --  
> 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).

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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:44am UTC](https://discuss.elastic.co/t/has-child-and-matching-children/11190/3 "2017-07-06T02:44:49Z")

</div>


