# Return facets across matched children in a has\_child search over parent?

**URL:** https://discuss.elastic.co/t/return-facets-across-matched-children-in-a-has-child-search-over-parent/13896
**Category:** Elasticsearch
**Created:** [October 9, 2013, 11:15pm UTC](https://discuss.elastic.co/t/return-facets-across-matched-children-in-a-has-child-search-over-parent/13896 "2013-10-09T23:15:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Jonathan\_Trevor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_trevor/32/2056_2.png) [@Jonathan\_Trevor](https://discuss.elastic.co/u/Jonathan_Trevor)
#### Post date: [October 9, 2013, 11:15pm UTC](https://discuss.elastic.co/t/return-facets-across-matched-children-in-a-has-child-search-over-parent/13896/1 "2013-10-09T23:15:43Z")

</div>

Im running a \_search request that uses combination of a simple term match  
in the "sink" parent object and has\_child query filter to get me all "sink"  
parent objects with a certain myfield term value that have "source"  
children that DONT have the same myfield term, e.g.:

curl -XPOST localhost:9200/test/sink/\_search?pretty=true -d '{  
"query": {  
"filtered": {  
"filter":{  
"and": [  
{ "term": {"myfield": "myfieldvalue1"} },  
{ "has\_child": {  
"type": "source",  
"query" : {  
"filtered": {  
"filter" : {  
"not":  
{"term": {"myfield": "myfieldvalue1"}}  
}  
}  
}  
}  
}  
]  
}  
}  
},  
...

This returns me the "sink" documents as desired, which is great.

Now I'd like to get facets back in this search for a different field in all  
the "source" children that were matched. For example, if each "source"  
child had a field "type" I'd like to get all the different types for  
"sources" that were children to these matched "sink" objects.

I've read this  
solution: [https://github.com/elasticsearch/elasticsearch/issues/2606](https://github.com/elasticsearch/elasticsearch/issues/2606) but Im  
unable to get something like this working.

I can probably write a second query to get me the facets by searching over  
the "sources" and use "has\_parent" (I think, still getting up to speed with  
the search syntax and capabilities) but is there anything I can do with  
this example above to get me those source "type" facets. In essence, a set  
of facets for the children matching the parent's query.

Thanks  
Jonathan

--  
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: ![Jonathan\_Trevor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_trevor/32/2056_2.png) [@Jonathan\_Trevor](https://discuss.elastic.co/u/Jonathan_Trevor)
#### Post date: [October 9, 2013, 11:41pm UTC](https://discuss.elastic.co/t/return-facets-across-matched-children-in-a-has-child-search-over-parent/13896/2 "2013-10-09T23:41:21Z")

</div>

I'll just add that this is the "facet" query results I'd like to come back  
with the above (its the same the above but run over the child objects, thus  
the has\_parent usage rather than has\_child).. but without running this  
secondary query to get it if possible.

curl -XPOST localhost:9200/everlink/source/\_search -d '{

"query": {  
"filtered": {  
"filter":{  
"and": [  
{ "not": { "term": {"myfield": "myvalue"} } },  
{ "has\_parent": {  
"type": "page",  
"query" : {  
"filtered": {  
"filter" :  
{"term": {"myfield": "myvalue"}}  
}  
}  
}  
}  
]  
}  
}  
},  
"facets" : {  
"types" : { "terms" : {"field" : "type"} }  
},  
"size":0  
}'

--  
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: ![Jonathan\_Trevor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_trevor/32/2056_2.png) [@Jonathan\_Trevor](https://discuss.elastic.co/u/Jonathan_Trevor)
#### Post date: [October 9, 2013, 11:42pm UTC](https://discuss.elastic.co/t/return-facets-across-matched-children-in-a-has-child-search-over-parent/13896/3 "2013-10-09T23:42:30Z")

</div>

I'll just add that this is the "facet" query results I'd like to come back  
with the above (its the same the above but run over the child objects, thus  
the has\_parent usage rather than has\_child).. but without running this  
secondary query to get it if possible.

curl -XPOST localhost:9200/everlink/source/\_search -d '{

"query": {  
"filtered": {  
"filter":{  
"and": [  
{ "not": { "term": {"myfield": "myvalue"} } },  
{ "has\_parent": {  
"type": "sink",  
"query" : {  
"filtered": {  
"filter" :  
{"term": {"myfield": "myvalue"}}  
}  
}  
}  
}  
]  
}  
}  
},  
"facets" : {  
"types" : { "terms" : {"field" : "type"} }  
},  
"size":0  
}'

--  
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:13am UTC](https://discuss.elastic.co/t/return-facets-across-matched-children-in-a-has-child-search-over-parent/13896/4 "2017-07-06T02:13:08Z")

</div>


