# Facets on child documents

**URL:** <https://discuss.elastic.co/t/facets-on-child-documents/12726>\
**Category:** Elasticsearch\
**Created:** [July 9, 2013, 6:17pm UTC](https://discuss.elastic.co/t/facets-on-child-documents/12726 "2013-07-09T18:17:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek\_Andhavarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_andhavarapu/32/782_2.png) [@Abhishek\_Andhavarapu](https://discuss.elastic.co/u/Abhishek_Andhavarapu)\
**Post date:** [July 9, 2013, 6:17pm UTC](https://discuss.elastic.co/t/facets-on-child-documents/12726/1 "2013-07-09T18:17:20Z")

</div>

Hi,

I'm trying to move from nested relations to parent/child relationships as I  
have to blowup the entire document if I have to udpate the nested  
documents. I use lots of faceting in the nested documents. In parent/child  
relations can I use has\_children filter and run faceting on the child  
documents ? I couldn't find anything on internet.

For example  
curl -XPOST '[http://esvm1:9200/pc/parent/\_search?pretty=true](http://esvm1:9200/pc/parent/_search?pretty=true)[http://esvm1:9200/pc/map/\_search?pretty=true](http://esvm1:9200/pc/map/_search?pretty=true)'  
-d ' /_/ Run the query on parent type_  
{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"and": [  
{  
"has\_child": {  
"type": "map",  
"filter": {  
"and": [  
{  
"term": {  
"mapid": "100"  
}  
}]  
}  
}  
}  
]  
}  
}  
},  
"facets": { //\*Run facets on child type \*  
"date1": {  
"date\_histogram": {  
"field": "date",  
"interval": "day"  
}  
}  
}  
}

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:** ![mattweber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattweber/32/44940_2.png) [@mattweber](https://discuss.elastic.co/u/mattweber)\
**Post date:** [July 9, 2013, 6:20pm UTC](https://discuss.elastic.co/t/facets-on-child-documents/12726/2 "2013-07-09T18:20:33Z")

</div>

Yes, you need to set your facet to global and then use a facet filter to  
restrict it to just the child docs. See the example here:

> <https://github.com/elastic/elasticsearch/issues/2606>
>
> Remove support for the \`scope\` field in facets and \`\_scope\` field in the nested …and parent/child queries. The scope support for nested queries will be replaced by the \`nested\` facet option and a facet filter with a nested filter. The nested filters will now support the a \`join\` option. Which controls whether to perform the block join. By default this enabled, but when disabled it returns the nested documents as hits instead of the joined root document.
> 
> Search request with the current scope support.
> 
> \`\`\`
> curl -s -XPOST 'localhost:9200/products/\_search' -d '{
> "query" : {
> "nested" : {
> "path" : "offers",
> "query" : {
> "match" : {
> "offers.color" : "blue"     
> }
> },
> "\_scope" : "my\_scope"
> }
> },
> "facets" : {
> "size" : {
> "terms" : {
> "field" : "offers.size"
> },
> "scope" : "my\_scope"
> }
> }
> }'
> \`\`\`
> 
> The following will be functional equivalent of using the scope support: 
> 
> \`\`\`
> curl -s -XPOST 'localhost:9200/products/\_search?search\_type=count' -d '{
> "query" : {
> "nested" : {
> "path" : "offers",
> "query" : {
> "match" : {
> "offers.color" : "blue"     
> }
> }
> }
> },
> "facets" : {
> "size" : {
> "terms" : {
> "field" : "offers.size"
> },
> "facet\_filter" : {
> "nested" : {
> "path" : "offers",
> "query" : {
> "match" : {
> "offers.color" : "blue"     
> }
> },
> "join" : false
> }   
> },
> "nested" : "offers"
> }
> }
> }'
> \`\`\`
> 
> The scope support for parent/child queries will be replaced by running the child query as filter in a global facet.
> 
> Search request with the current scope support:
> 
> \`\`\`
> curl -s -XPOST 'localhost:9200/products/\_search' -d '{
> "query" : {
> "has\_child" : {
> "type" : "offer",
> "query" : {
> "match" : {
> "color" : "blue"        
> }
> },
> "\_scope" : "my\_scope"
> }
> },
> "facets" : {
> "size" : {
> "terms" : {
> "field" : "size"
> },
> "scope" : "my\_scope"
> }
> }
> }'
> \`\`\`
> 
> The following is the functional equivalent of using the scope support with parent/child queries:
> 
> \`\`\`
> curl -s -XPOST 'localhost:9200/products/\_search' -d '{
> "query" : {
> "has\_child" : {
> "type" : "offer",
> "query" : {
> "match" : {
> "color" : "blue"        
> }
> }
> }
> },
> "facets" : {
> "size" : {
> "terms" : {
> "field" : "size"
> },
> "global" : true,
> "facet\_filter" : {
> "term" : {
> "color" : "blue"    
> }
> }
> }
> }
> }'
> \`\`\`

Thanks,  
Matt Weber

On Tue, Jul 9, 2013 at 11:17 AM, Abhishek Andhavarapu \<[abhishek376@gmail.com](mailto:abhishek376@gmail.com)

> wrote:

> Hi,
> 
> I'm trying to move from nested relations to parent/child relationships as  
> I have to blowup the entire document if I have to udpate the nested  
> documents. I use lots of faceting in the nested documents. In parent/child  
> relations can I use has\_children filter and run faceting on the child  
> documents ? I couldn't find anything on internet.
> 
> For example  
> curl -XPOST '[http://esvm1:9200/pc/parent/\_\*\*search?pretty=true](http://esvm1:9200/pc/parent/_**search?pretty=true)[http://esvm1:9200/pc/map/\_search?pretty=true](http://esvm1:9200/pc/map/_search?pretty=true)'  
> -d ' /_/ Run the query on parent type_  
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "and": [  
> {  
> "has\_child": {  
> "type": "map",  
> "filter": {  
> "and": [  
> {  
> "term": {  
> "mapid": "100"  
> }  
> }]  
> }  
> }  
> }  
> ]  
> }  
> }  
> },  
> "facets": { //\*Run facets on child type \*  
> "date1": {  
> "date\_histogram": {  
> "field": "date",  
> "interval": "day"  
> }  
> }  
> }  
> }
> 
> 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:** ![Abhishek\_Andhavarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_andhavarapu/32/782_2.png) [@Abhishek\_Andhavarapu](https://discuss.elastic.co/u/Abhishek_Andhavarapu)\
**Post date:** [July 9, 2013, 6:49pm UTC](https://discuss.elastic.co/t/facets-on-child-documents/12726/3 "2013-07-09T18:49:26Z")

</div>

Hi Matt,

Thanks for the link. So this is my complete example

Parent Docs  
curl -XPUT '[http://esvm1:9200/pc/respondent/1](http://esvm1:9200/pc/respondent/1)' -d '{"rid": 1, "date":  
"2011-11-18"}'  
curl -XPUT '[http://esvm1:9200/pc/respondent/2](http://esvm1:9200/pc/respondent/2)' -d '{"rid": 2, "date":  
"2011-11-19"}'  
curl -XPUT '[http://esvm1:9200/pc/respondent/3](http://esvm1:9200/pc/respondent/3)' -d '{"rid": 3, "date":  
"2011-11-17"}'  
curl -XPUT '[http://esvm1:9200/pc/respondent/4](http://esvm1:9200/pc/respondent/4)' -d '{"rid": 4, "date":  
"2011-11-18"}'  
curl -XPUT '[http://esvm1:9200/pc/respondent/5](http://esvm1:9200/pc/respondent/5)' -d '{"rid": 5, "date":  
"2011-11-18"}'  
curl -XPUT '[http://esvm1:9200/pc/respondent/6](http://esvm1:9200/pc/respondent/6)' -d '{"rid": 6, "date":  
"2011-11-19"}'

# Insert some maps (Child docs) that happened to those respondents:

# 

curl -XPOST '[http://esvm1:9200/pc/map/1\_100?parent=1](http://esvm1:9200/pc/map/1_100?parent=1)' -d '{"mapid": 100,  
"value": 1}'  
curl -XPOST '[http://esvm1:9200/pc/map/1\_102?parent=1](http://esvm1:9200/pc/map/1_102?parent=1)' -d '{"mapid": 102,  
"text": "hi"}'  
curl -XPOST '[http://esvm1:9200/pc/map/2\_100?parent=2](http://esvm1:9200/pc/map/2_100?parent=2)' -d '{"mapid": 100,  
"value": 2}'  
curl -XPOST '[http://esvm1:9200/pc/map/2\_101?parent=2](http://esvm1:9200/pc/map/2_101?parent=2)' -d '{"mapid": 101,  
"value": 1}'  
curl -XPOST '[http://esvm1:9200/pc/map/3\_101?parent=3](http://esvm1:9200/pc/map/3_101?parent=3)' -d '{"mapid": 101,  
"value": 1}'  
curl -XPOST '[http://esvm1:9200/pc/map/3\_102?parent=3](http://esvm1:9200/pc/map/3_102?parent=3)' -d '{"mapid": 102,  
"text": "hello"}'  
curl -XPOST '[http://esvm1:9200/pc/map/4\_100?parent=4](http://esvm1:9200/pc/map/4_100?parent=4)' -d '{"mapid": 100,  
"value": 1}'  
curl -XPOST '[http://esvm1:9200/pc/map/5\_100?parent=5](http://esvm1:9200/pc/map/5_100?parent=5)' -d '{"mapid": 100,  
"value": 2}'  
curl -XPOST '[http://esvm1:9200/pc/map/6\_100?parent=6](http://esvm1:9200/pc/map/6_100?parent=6)' -d '{"mapid": 100,  
"value": 1}'

My query as I understand

{  
"query" : {  
"has\_child" : {  
"type" : "map",  
"query" : {  
"match" : {  
"mapid" : "101"  
}  
}  
}  
},  
"facets" : {  
"term1" : {  
"terms" : {  
"field" : "value"  
},  
"global" : true,  
"facet\_filter" : {  
"term" : {  
"mapid" : "100"  
}  
}  
}  
}  
}

My response

- hits: [

- terms: [

As the scope is global it run the facets on the child documents so it comes  
back with term 1 is count 3. But my hits are just 1,3

- 

I want to run the facets only on child documents whose parent id is 1,3 so  
the facet result would be

- terms: [
  - {
    - term: 1
    - count: 1  
}

  - {
    - term: 2
    - count: 1  
}  
]

- 

Is this possible ?

Thanks,  
Abhishek

On Tue, Jul 9, 2013 at 12:20 PM, Matt Weber [matt.weber@gmail.com](mailto:matt.weber@gmail.com) wrote:

> Yes, you need to set your facet to global and then use a facet filter to  
> restrict it to just the child docs. See the example here:
> 
> [Remove scope support in query and facet dsl. · Issue #2606 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2606)
> 
> Thanks,  
> Matt Weber
> 
> On Tue, Jul 9, 2013 at 11:17 AM, Abhishek Andhavarapu \<  
> [abhishek376@gmail.com](mailto:abhishek376@gmail.com)\> wrote:
> 
> > Hi,
> > 
> > I'm trying to move from nested relations to parent/child relationships as  
> > I have to blowup the entire document if I have to udpate the nested  
> > documents. I use lots of faceting in the nested documents. In parent/child  
> > relations can I use has\_children filter and run faceting on the child  
> > documents ? I couldn't find anything on internet.
> > 
> > For example  
> > curl -XPOST '[http://esvm1:9200/pc/parent/\_\*\*search?pretty=true](http://esvm1:9200/pc/parent/_**search?pretty=true)[http://esvm1:9200/pc/map/\_search?pretty=true](http://esvm1:9200/pc/map/_search?pretty=true)'  
> > -d ' /_/ Run the query on parent type_  
> > {  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "filter": {  
> > "and": [  
> > {  
> > "has\_child": {  
> > "type": "map",  
> > "filter": {  
> > "and": [  
> > {  
> > "term": {  
> > "mapid": "100"  
> > }  
> > }]  
> > }  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > },  
> > "facets": { //\*Run facets on child type \*  
> > "date1": {  
> > "date\_histogram": {  
> > "field": "date",  
> > "interval": "day"  
> > }  
> > }  
> > }  
> > }
> > 
> > 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 a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe](https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe).  
> To unsubscribe from this group and all its topics, 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:** ![mattweber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattweber/32/44940_2.png) [@mattweber](https://discuss.elastic.co/u/mattweber)\
**Post date:** [July 9, 2013, 6:59pm UTC](https://discuss.elastic.co/t/facets-on-child-documents/12726/4 "2013-07-09T18:59:08Z")

</div>

You are querying on mapid 101 but filtering on mapid 100. Those should be  
the same mapid.

On Tue, Jul 9, 2013 at 11:49 AM, Abhishek [abhishek376@gmail.com](mailto:abhishek376@gmail.com) wrote:

> Hi Matt,
> 
> Thanks for the link. So this is my complete example
> 
> Parent Docs  
> curl -XPUT '[http://esvm1:9200/pc/respondent/1](http://esvm1:9200/pc/respondent/1)' -d '{"rid": 1, "date":  
> "2011-11-18"}'  
> curl -XPUT '[http://esvm1:9200/pc/respondent/2](http://esvm1:9200/pc/respondent/2)' -d '{"rid": 2, "date":  
> "2011-11-19"}'  
> curl -XPUT '[http://esvm1:9200/pc/respondent/3](http://esvm1:9200/pc/respondent/3)' -d '{"rid": 3, "date":  
> "2011-11-17"}'  
> curl -XPUT '[http://esvm1:9200/pc/respondent/4](http://esvm1:9200/pc/respondent/4)' -d '{"rid": 4, "date":  
> "2011-11-18"}'  
> curl -XPUT '[http://esvm1:9200/pc/respondent/5](http://esvm1:9200/pc/respondent/5)' -d '{"rid": 5, "date":  
> "2011-11-18"}'  
> curl -XPUT '[http://esvm1:9200/pc/respondent/6](http://esvm1:9200/pc/respondent/6)' -d '{"rid": 6, "date":  
> "2011-11-19"}'
> 
> # Insert some maps (Child docs) that happened to those respondents:
> 
> # 
> 
> curl -XPOST '[http://esvm1:9200/pc/map/1\_100?parent=1](http://esvm1:9200/pc/map/1_100?parent=1)' -d '{"mapid": 100,  
> "value": 1}'  
> curl -XPOST '[http://esvm1:9200/pc/map/1\_102?parent=1](http://esvm1:9200/pc/map/1_102?parent=1)' -d '{"mapid": 102,  
> "text": "hi"}'  
> curl -XPOST '[http://esvm1:9200/pc/map/2\_100?parent=2](http://esvm1:9200/pc/map/2_100?parent=2)' -d '{"mapid": 100,  
> "value": 2}'  
> curl -XPOST '[http://esvm1:9200/pc/map/2\_101?parent=2](http://esvm1:9200/pc/map/2_101?parent=2)' -d '{"mapid": 101,  
> "value": 1}'  
> curl -XPOST '[http://esvm1:9200/pc/map/3\_101?parent=3](http://esvm1:9200/pc/map/3_101?parent=3)' -d '{"mapid": 101,  
> "value": 1}'  
> curl -XPOST '[http://esvm1:9200/pc/map/3\_102?parent=3](http://esvm1:9200/pc/map/3_102?parent=3)' -d '{"mapid": 102,  
> "text": "hello"}'  
> curl -XPOST '[http://esvm1:9200/pc/map/4\_100?parent=4](http://esvm1:9200/pc/map/4_100?parent=4)' -d '{"mapid": 100,  
> "value": 1}'  
> curl -XPOST '[http://esvm1:9200/pc/map/5\_100?parent=5](http://esvm1:9200/pc/map/5_100?parent=5)' -d '{"mapid": 100,  
> "value": 2}'  
> curl -XPOST '[http://esvm1:9200/pc/map/6\_100?parent=6](http://esvm1:9200/pc/map/6_100?parent=6)' -d '{"mapid": 100,  
> "value": 1}'
> 
> My query as I understand
> 
> {  
> "query" : {  
> "has\_child" : {  
> "type" : "map",  
> "query" : {  
> "match" : {  
> "mapid" : "101"  
> }  
> }  
> }  
> },  
> "facets" : {  
> "term1" : {  
> "terms" : {  
> "field" : "value"  
> },  
> "global" : true,  
> "facet\_filter" : {  
> "term" : {  
> "mapid" : "100"  
> }  
> }  
> }  
> }  
> }
> 
> My response
> 
> - hits: [
> 
> - terms: [
> 
> As the scope is global it run the facets on the child documents so it  
> comes back with term 1 is count 3. But my hits are just 1,3
> 
> - 
> 
> I want to run the facets only on child documents whose parent id is 1,3  
> so the facet result would be
> 
> - terms: [
> - {
> - term: 1
> - count: 1  
> }
> 
> - {
> - term: 2
> - count: 1  
> }  
> ]
> 
> - 
> 
> Is this possible ?
> 
> Thanks,  
> Abhishek
> 
> On Tue, Jul 9, 2013 at 12:20 PM, Matt Weber [matt.weber@gmail.com](mailto:matt.weber@gmail.com) wrote:
> 
> > Yes, you need to set your facet to global and then use a facet filter  
> > to restrict it to just the child docs. See the example here:
> > 
> > [Remove scope support in query and facet dsl. · Issue #2606 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2606)
> > 
> > Thanks,  
> > Matt Weber
> > 
> > On Tue, Jul 9, 2013 at 11:17 AM, Abhishek Andhavarapu \<  
> > [abhishek376@gmail.com](mailto:abhishek376@gmail.com)\> wrote:
> > 
> > > Hi,
> > > 
> > > I'm trying to move from nested relations to parent/child relationships  
> > > as I have to blowup the entire document if I have to udpate the nested  
> > > documents. I use lots of faceting in the nested documents. In parent/child  
> > > relations can I use has\_children filter and run faceting on the child  
> > > documents ? I couldn't find anything on internet.
> > > 
> > > For example  
> > > curl -XPOST '[http://esvm1:9200/pc/parent/\_\*\*search?pretty=true](http://esvm1:9200/pc/parent/_**search?pretty=true)[http://esvm1:9200/pc/map/\_search?pretty=true](http://esvm1:9200/pc/map/_search?pretty=true)'  
> > > -d ' /_/ Run the query on parent type_  
> > > {  
> > > "query": {  
> > > "filtered": {  
> > > "query": {  
> > > "match\_all": {}  
> > > },  
> > > "filter": {  
> > > "and": [  
> > > {  
> > > "has\_child": {  
> > > "type": "map",  
> > > "filter": {  
> > > "and": [  
> > > {  
> > > "term": {  
> > > "mapid": "100"  
> > > }  
> > > }]  
> > > }  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > },  
> > > "facets": { //\*Run facets on child type \*  
> > > "date1": {  
> > > "date\_histogram": {  
> > > "field": "date",  
> > > "interval": "day"  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > 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 a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe](https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe).  
> > To unsubscribe from this group and all its topics, 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).

--  
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:** ![Abhishek\_Andhavarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_andhavarapu/32/782_2.png) [@Abhishek\_Andhavarapu](https://discuss.elastic.co/u/Abhishek_Andhavarapu)\
**Post date:** [July 9, 2013, 7:07pm UTC](https://discuss.elastic.co/t/facets-on-child-documents/12726/5 "2013-07-09T19:07:13Z")

</div>

So I want to run facets on mapid (100) of the respondents who has documents  
with mapid 101. Then I guess I'm looking at facet filter which is  
has\_parent and has\_parent filter has a has\_child (mapid: 101) to run  
faceting on the mapid 100 value. So I'm guessing even though I some how I  
achieve this the performance sucks.

Matt, correct me if I'm wrong.

Thanks.  
Abhishek

On Tue, Jul 9, 2013 at 12:59 PM, Matt Weber [matt.weber@gmail.com](mailto:matt.weber@gmail.com) wrote:

> You are querying on mapid 101 but filtering on mapid 100. Those should be  
> the same mapid.
> 
> On Tue, Jul 9, 2013 at 11:49 AM, Abhishek [abhishek376@gmail.com](mailto:abhishek376@gmail.com) wrote:
> 
> > Hi Matt,
> > 
> > Thanks for the link. So this is my complete example
> > 
> > Parent Docs  
> > curl -XPUT '[http://esvm1:9200/pc/respondent/1](http://esvm1:9200/pc/respondent/1)' -d '{"rid": 1, "date":  
> > "2011-11-18"}'  
> > curl -XPUT '[http://esvm1:9200/pc/respondent/2](http://esvm1:9200/pc/respondent/2)' -d '{"rid": 2, "date":  
> > "2011-11-19"}'  
> > curl -XPUT '[http://esvm1:9200/pc/respondent/3](http://esvm1:9200/pc/respondent/3)' -d '{"rid": 3, "date":  
> > "2011-11-17"}'  
> > curl -XPUT '[http://esvm1:9200/pc/respondent/4](http://esvm1:9200/pc/respondent/4)' -d '{"rid": 4, "date":  
> > "2011-11-18"}'  
> > curl -XPUT '[http://esvm1:9200/pc/respondent/5](http://esvm1:9200/pc/respondent/5)' -d '{"rid": 5, "date":  
> > "2011-11-18"}'  
> > curl -XPUT '[http://esvm1:9200/pc/respondent/6](http://esvm1:9200/pc/respondent/6)' -d '{"rid": 6, "date":  
> > "2011-11-19"}'
> > 
> > # Insert some maps (Child docs) that happened to those respondents:
> > 
> > # 
> > 
> > curl -XPOST '[http://esvm1:9200/pc/map/1\_100?parent=1](http://esvm1:9200/pc/map/1_100?parent=1)' -d '{"mapid": 100,  
> > "value": 1}'  
> > curl -XPOST '[http://esvm1:9200/pc/map/1\_102?parent=1](http://esvm1:9200/pc/map/1_102?parent=1)' -d '{"mapid": 102,  
> > "text": "hi"}'  
> > curl -XPOST '[http://esvm1:9200/pc/map/2\_100?parent=2](http://esvm1:9200/pc/map/2_100?parent=2)' -d '{"mapid": 100,  
> > "value": 2}'  
> > curl -XPOST '[http://esvm1:9200/pc/map/2\_101?parent=2](http://esvm1:9200/pc/map/2_101?parent=2)' -d '{"mapid": 101,  
> > "value": 1}'  
> > curl -XPOST '[http://esvm1:9200/pc/map/3\_101?parent=3](http://esvm1:9200/pc/map/3_101?parent=3)' -d '{"mapid": 101,  
> > "value": 1}'  
> > curl -XPOST '[http://esvm1:9200/pc/map/3\_102?parent=3](http://esvm1:9200/pc/map/3_102?parent=3)' -d '{"mapid": 102,  
> > "text": "hello"}'  
> > curl -XPOST '[http://esvm1:9200/pc/map/4\_100?parent=4](http://esvm1:9200/pc/map/4_100?parent=4)' -d '{"mapid": 100,  
> > "value": 1}'  
> > curl -XPOST '[http://esvm1:9200/pc/map/5\_100?parent=5](http://esvm1:9200/pc/map/5_100?parent=5)' -d '{"mapid": 100,  
> > "value": 2}'  
> > curl -XPOST '[http://esvm1:9200/pc/map/6\_100?parent=6](http://esvm1:9200/pc/map/6_100?parent=6)' -d '{"mapid": 100,  
> > "value": 1}'
> > 
> > My query as I understand
> > 
> > {  
> > "query" : {  
> > "has\_child" : {  
> > "type" : "map",  
> > "query" : {  
> > "match" : {  
> > "mapid" : "101"  
> > }  
> > }  
> > }  
> > },  
> > "facets" : {  
> > "term1" : {  
> > "terms" : {  
> > "field" : "value"  
> > },  
> > "global" : true,  
> > "facet\_filter" : {  
> > "term" : {  
> > "mapid" : "100"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > My response
> > 
> > - hits: [
> > 
> > - terms: [
> > 
> > As the scope is global it run the facets on the child documents so it  
> > comes back with term 1 is count 3. But my hits are just 1,3
> > 
> > - 
> > 
> > I want to run the facets only on child documents whose parent id is 1,3  
> > so the facet result would be
> > 
> > - terms: [
> > - {
> > - term: 1
> > - count: 1  
> > }
> > 
> > - {
> > - term: 2
> > - count: 1  
> > }  
> > ]
> > 
> > - 
> > 
> > Is this possible ?
> > 
> > Thanks,  
> > Abhishek
> > 
> > On Tue, Jul 9, 2013 at 12:20 PM, Matt Weber [matt.weber@gmail.com](mailto:matt.weber@gmail.com) wrote:
> > 
> > > Yes, you need to set your facet to global and then use a facet filter  
> > > to restrict it to just the child docs. See the example here:
> > > 
> > > [Remove scope support in query and facet dsl. · Issue #2606 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2606)
> > > 
> > > Thanks,  
> > > Matt Weber
> > > 
> > > On Tue, Jul 9, 2013 at 11:17 AM, Abhishek Andhavarapu \<  
> > > [abhishek376@gmail.com](mailto:abhishek376@gmail.com)\> wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I'm trying to move from nested relations to parent/child relationships  
> > > > as I have to blowup the entire document if I have to udpate the nested  
> > > > documents. I use lots of faceting in the nested documents. In parent/child  
> > > > relations can I use has\_children filter and run faceting on the child  
> > > > documents ? I couldn't find anything on internet.
> > > > 
> > > > For example  
> > > > curl -XPOST '[http://esvm1:9200/pc/parent/\_\*\*search?pretty=true](http://esvm1:9200/pc/parent/_**search?pretty=true)[http://esvm1:9200/pc/map/\_search?pretty=true](http://esvm1:9200/pc/map/_search?pretty=true)'  
> > > > -d ' /_/ Run the query on parent type_  
> > > > {  
> > > > "query": {  
> > > > "filtered": {  
> > > > "query": {  
> > > > "match\_all": {}  
> > > > },  
> > > > "filter": {  
> > > > "and": [  
> > > > {  
> > > > "has\_child": {  
> > > > "type": "map",  
> > > > "filter": {  
> > > > "and": [  
> > > > {  
> > > > "term": {  
> > > > "mapid": "100"  
> > > > }  
> > > > }]  
> > > > }  
> > > > }  
> > > > }  
> > > > ]  
> > > > }  
> > > > }  
> > > > },  
> > > > "facets": { //\*Run facets on child type \*  
> > > > "date1": {  
> > > > "date\_histogram": {  
> > > > "field": "date",  
> > > > "interval": "day"  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > 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 a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe](https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe).  
> > > To unsubscribe from this group and all its topics, 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).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe](https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe).  
> To unsubscribe from this group and all its topics, 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:** ![mattweber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattweber/32/44940_2.png) [@mattweber](https://discuss.elastic.co/u/mattweber)\
**Post date:** [July 9, 2013, 7:42pm UTC](https://discuss.elastic.co/t/facets-on-child-documents/12726/6 "2013-07-09T19:42:09Z")

</div>

If you know the id of the specific parents you want, you can do a term  
filter against the \_parent field within the facet filter. Something like  
this....

{  
"query" : {  
"has\_child" : {  
"type" : "map",  
"query" : {  
"match" : {  
"mapid" : "101"  
}  
}  
}  
},  
"facets" : {  
"term1" : {  
"terms" : {  
"field" : "value"  
},  
"global" : true,  
"facet\_filter" : {  
"bool": {  
"must": [  
{"terms:{"\_parent": ["1","3"]}},  
{"term":{"mapid":"100"}}  
]  
}  
}  
}  
}  
}

Thanks,  
Matt Weber

On Tue, Jul 9, 2013 at 12:07 PM, Abhishek [abhishek376@gmail.com](mailto:abhishek376@gmail.com) wrote:

> So I want to run facets on mapid (100) of the respondents who has  
> documents with mapid 101. Then I guess I'm looking at facet filter which is  
> has\_parent and has\_parent filter has a has\_child (mapid: 101) to run  
> faceting on the mapid 100 value. So I'm guessing even though I some how I  
> achieve this the performance sucks.
> 
> Matt, correct me if I'm wrong.
> 
> Thanks.  
> Abhishek
> 
> On Tue, Jul 9, 2013 at 12:59 PM, Matt Weber [matt.weber@gmail.com](mailto:matt.weber@gmail.com) wrote:
> 
> > You are querying on mapid 101 but filtering on mapid 100. Those should  
> > be the same mapid.
> > 
> > On Tue, Jul 9, 2013 at 11:49 AM, Abhishek [abhishek376@gmail.com](mailto:abhishek376@gmail.com) wrote:
> > 
> > > Hi Matt,
> > > 
> > > Thanks for the link. So this is my complete example
> > > 
> > > Parent Docs  
> > > curl -XPUT '[http://esvm1:9200/pc/respondent/1](http://esvm1:9200/pc/respondent/1)' -d '{"rid": 1, "date":  
> > > "2011-11-18"}'  
> > > curl -XPUT '[http://esvm1:9200/pc/respondent/2](http://esvm1:9200/pc/respondent/2)' -d '{"rid": 2, "date":  
> > > "2011-11-19"}'  
> > > curl -XPUT '[http://esvm1:9200/pc/respondent/3](http://esvm1:9200/pc/respondent/3)' -d '{"rid": 3, "date":  
> > > "2011-11-17"}'  
> > > curl -XPUT '[http://esvm1:9200/pc/respondent/4](http://esvm1:9200/pc/respondent/4)' -d '{"rid": 4, "date":  
> > > "2011-11-18"}'  
> > > curl -XPUT '[http://esvm1:9200/pc/respondent/5](http://esvm1:9200/pc/respondent/5)' -d '{"rid": 5, "date":  
> > > "2011-11-18"}'  
> > > curl -XPUT '[http://esvm1:9200/pc/respondent/6](http://esvm1:9200/pc/respondent/6)' -d '{"rid": 6, "date":  
> > > "2011-11-19"}'
> > > 
> > > # Insert some maps (Child docs) that happened to those respondents:
> > > 
> > > # 
> > > 
> > > curl -XPOST '[http://esvm1:9200/pc/map/1\_100?parent=1](http://esvm1:9200/pc/map/1_100?parent=1)' -d '{"mapid":  
> > > 100, "value": 1}'  
> > > curl -XPOST '[http://esvm1:9200/pc/map/1\_102?parent=1](http://esvm1:9200/pc/map/1_102?parent=1)' -d '{"mapid":  
> > > 102, "text": "hi"}'  
> > > curl -XPOST '[http://esvm1:9200/pc/map/2\_100?parent=2](http://esvm1:9200/pc/map/2_100?parent=2)' -d '{"mapid":  
> > > 100, "value": 2}'  
> > > curl -XPOST '[http://esvm1:9200/pc/map/2\_101?parent=2](http://esvm1:9200/pc/map/2_101?parent=2)' -d '{"mapid":  
> > > 101, "value": 1}'  
> > > curl -XPOST '[http://esvm1:9200/pc/map/3\_101?parent=3](http://esvm1:9200/pc/map/3_101?parent=3)' -d '{"mapid":  
> > > 101, "value": 1}'  
> > > curl -XPOST '[http://esvm1:9200/pc/map/3\_102?parent=3](http://esvm1:9200/pc/map/3_102?parent=3)' -d '{"mapid":  
> > > 102, "text": "hello"}'  
> > > curl -XPOST '[http://esvm1:9200/pc/map/4\_100?parent=4](http://esvm1:9200/pc/map/4_100?parent=4)' -d '{"mapid":  
> > > 100, "value": 1}'  
> > > curl -XPOST '[http://esvm1:9200/pc/map/5\_100?parent=5](http://esvm1:9200/pc/map/5_100?parent=5)' -d '{"mapid":  
> > > 100, "value": 2}'  
> > > curl -XPOST '[http://esvm1:9200/pc/map/6\_100?parent=6](http://esvm1:9200/pc/map/6_100?parent=6)' -d '{"mapid":  
> > > 100, "value": 1}'
> > > 
> > > My query as I understand
> > > 
> > > {  
> > > "query" : {  
> > > "has\_child" : {  
> > > "type" : "map",  
> > > "query" : {  
> > > "match" : {  
> > > "mapid" : "101"  
> > > }  
> > > }  
> > > }  
> > > },  
> > > "facets" : {  
> > > "term1" : {  
> > > "terms" : {  
> > > "field" : "value"  
> > > },  
> > > "global" : true,  
> > > "facet\_filter" : {  
> > > "term" : {  
> > > "mapid" : "100"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > My response
> > > 
> > > - hits: [
> > > 
> > > - terms: [
> > > 
> > > As the scope is global it run the facets on the child documents so it  
> > > comes back with term 1 is count 3. But my hits are just 1,3
> > > 
> > > - 
> > > 
> > > I want to run the facets only on child documents whose parent id is 1,3  
> > > so the facet result would be
> > > 
> > > - terms: [
> > > - {
> > > - term: 1
> > > - count: 1  
> > > }
> > > 
> > > - {
> > > - term: 2
> > > - count: 1  
> > > }  
> > > ]
> > > 
> > > - 
> > > 
> > > Is this possible ?
> > > 
> > > Thanks,  
> > > Abhishek
> > > 
> > > On Tue, Jul 9, 2013 at 12:20 PM, Matt Weber [matt.weber@gmail.com](mailto:matt.weber@gmail.com)wrote:
> > > 
> > > > Yes, you need to set your facet to global and then use a facet filter  
> > > > to restrict it to just the child docs. See the example here:
> > > > 
> > > > [Remove scope support in query and facet dsl. · Issue #2606 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2606)
> > > > 
> > > > Thanks,  
> > > > Matt Weber
> > > > 
> > > > On Tue, Jul 9, 2013 at 11:17 AM, Abhishek Andhavarapu \<  
> > > > [abhishek376@gmail.com](mailto:abhishek376@gmail.com)\> wrote:
> > > > 
> > > > > Hi,
> > > > > 
> > > > > I'm trying to move from nested relations to parent/child relationships  
> > > > > as I have to blowup the entire document if I have to udpate the nested  
> > > > > documents. I use lots of faceting in the nested documents. In parent/child  
> > > > > relations can I use has\_children filter and run faceting on the child  
> > > > > documents ? I couldn't find anything on internet.
> > > > > 
> > > > > For example  
> > > > > curl -XPOST '[http://esvm1:9200/pc/parent/\_\*\*search?pretty=true](http://esvm1:9200/pc/parent/_**search?pretty=true)[http://esvm1:9200/pc/map/\_search?pretty=true](http://esvm1:9200/pc/map/_search?pretty=true)'  
> > > > > -d ' /_/ Run the query on parent type_  
> > > > > {  
> > > > > "query": {  
> > > > > "filtered": {  
> > > > > "query": {  
> > > > > "match\_all": {}  
> > > > > },  
> > > > > "filter": {  
> > > > > "and": [  
> > > > > {  
> > > > > "has\_child": {  
> > > > > "type": "map",  
> > > > > "filter": {  
> > > > > "and": [  
> > > > > {  
> > > > > "term": {  
> > > > > "mapid": "100"  
> > > > > }  
> > > > > }]  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > }  
> > > > > },  
> > > > > "facets": { //\*Run facets on child type \*  
> > > > > "date1": {  
> > > > > "date\_histogram": {  
> > > > > "field": "date",  
> > > > > "interval": "day"  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }
> > > > > 
> > > > > 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 a topic in the  
> > > > Google Groups "elasticsearch" group.  
> > > > To unsubscribe from this topic, visit  
> > > > [https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe](https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe)  
> > > > .  
> > > > To unsubscribe from this group and all its topics, 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).
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe](https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe).  
> > To unsubscribe from this group and all its topics, 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).

--  
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:** ![Abhishek\_Andhavarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_andhavarapu/32/782_2.png) [@Abhishek\_Andhavarapu](https://discuss.elastic.co/u/Abhishek_Andhavarapu)\
**Post date:** [July 9, 2013, 8:10pm UTC](https://discuss.elastic.co/t/facets-on-child-documents/12726/7 "2013-07-09T20:10:25Z")

</div>

Thanks Matt. I don't have the parent id. I guess I have to stick to nested  
documents.

On Tue, Jul 9, 2013 at 1:42 PM, Matt Weber [matt.weber@gmail.com](mailto:matt.weber@gmail.com) wrote:

> If you know the id of the specific parents you want, you can do a term  
> filter against the \_parent field within the facet filter. Something like  
> this....
> 
> {  
> "query" : {  
> "has\_child" : {  
> "type" : "map",  
> "query" : {  
> "match" : {  
> "mapid" : "101"  
> }  
> }  
> }  
> },  
> "facets" : {  
> "term1" : {  
> "terms" : {  
> "field" : "value"  
> },  
> "global" : true,  
> "facet\_filter" : {  
> "bool": {  
> "must": [  
> {"terms:{"\_parent": ["1","3"]}},  
> {"term":{"mapid":"100"}}  
> ]  
> }  
> }  
> }  
> }  
> }
> 
> Thanks,  
> Matt Weber
> 
> On Tue, Jul 9, 2013 at 12:07 PM, Abhishek [abhishek376@gmail.com](mailto:abhishek376@gmail.com) wrote:
> 
> > So I want to run facets on mapid (100) of the respondents who has  
> > documents with mapid 101. Then I guess I'm looking at facet filter which is  
> > has\_parent and has\_parent filter has a has\_child (mapid: 101) to run  
> > faceting on the mapid 100 value. So I'm guessing even though I some how I  
> > achieve this the performance sucks.
> > 
> > Matt, correct me if I'm wrong.
> > 
> > Thanks.  
> > Abhishek
> > 
> > On Tue, Jul 9, 2013 at 12:59 PM, Matt Weber [matt.weber@gmail.com](mailto:matt.weber@gmail.com) wrote:
> > 
> > > You are querying on mapid 101 but filtering on mapid 100. Those should  
> > > be the same mapid.
> > > 
> > > On Tue, Jul 9, 2013 at 11:49 AM, Abhishek [abhishek376@gmail.com](mailto:abhishek376@gmail.com) wrote:
> > > 
> > > > Hi Matt,
> > > > 
> > > > Thanks for the link. So this is my complete example
> > > > 
> > > > Parent Docs  
> > > > curl -XPUT '[http://esvm1:9200/pc/respondent/1](http://esvm1:9200/pc/respondent/1)' -d '{"rid": 1, "date":  
> > > > "2011-11-18"}'  
> > > > curl -XPUT '[http://esvm1:9200/pc/respondent/2](http://esvm1:9200/pc/respondent/2)' -d '{"rid": 2, "date":  
> > > > "2011-11-19"}'  
> > > > curl -XPUT '[http://esvm1:9200/pc/respondent/3](http://esvm1:9200/pc/respondent/3)' -d '{"rid": 3, "date":  
> > > > "2011-11-17"}'  
> > > > curl -XPUT '[http://esvm1:9200/pc/respondent/4](http://esvm1:9200/pc/respondent/4)' -d '{"rid": 4, "date":  
> > > > "2011-11-18"}'  
> > > > curl -XPUT '[http://esvm1:9200/pc/respondent/5](http://esvm1:9200/pc/respondent/5)' -d '{"rid": 5, "date":  
> > > > "2011-11-18"}'  
> > > > curl -XPUT '[http://esvm1:9200/pc/respondent/6](http://esvm1:9200/pc/respondent/6)' -d '{"rid": 6, "date":  
> > > > "2011-11-19"}'
> > > > 
> > > > # Insert some maps (Child docs) that happened to those respondents:
> > > > 
> > > > # 
> > > > 
> > > > curl -XPOST '[http://esvm1:9200/pc/map/1\_100?parent=1](http://esvm1:9200/pc/map/1_100?parent=1)' -d '{"mapid":  
> > > > 100, "value": 1}'  
> > > > curl -XPOST '[http://esvm1:9200/pc/map/1\_102?parent=1](http://esvm1:9200/pc/map/1_102?parent=1)' -d '{"mapid":  
> > > > 102, "text": "hi"}'  
> > > > curl -XPOST '[http://esvm1:9200/pc/map/2\_100?parent=2](http://esvm1:9200/pc/map/2_100?parent=2)' -d '{"mapid":  
> > > > 100, "value": 2}'  
> > > > curl -XPOST '[http://esvm1:9200/pc/map/2\_101?parent=2](http://esvm1:9200/pc/map/2_101?parent=2)' -d '{"mapid":  
> > > > 101, "value": 1}'  
> > > > curl -XPOST '[http://esvm1:9200/pc/map/3\_101?parent=3](http://esvm1:9200/pc/map/3_101?parent=3)' -d '{"mapid":  
> > > > 101, "value": 1}'  
> > > > curl -XPOST '[http://esvm1:9200/pc/map/3\_102?parent=3](http://esvm1:9200/pc/map/3_102?parent=3)' -d '{"mapid":  
> > > > 102, "text": "hello"}'  
> > > > curl -XPOST '[http://esvm1:9200/pc/map/4\_100?parent=4](http://esvm1:9200/pc/map/4_100?parent=4)' -d '{"mapid":  
> > > > 100, "value": 1}'  
> > > > curl -XPOST '[http://esvm1:9200/pc/map/5\_100?parent=5](http://esvm1:9200/pc/map/5_100?parent=5)' -d '{"mapid":  
> > > > 100, "value": 2}'  
> > > > curl -XPOST '[http://esvm1:9200/pc/map/6\_100?parent=6](http://esvm1:9200/pc/map/6_100?parent=6)' -d '{"mapid":  
> > > > 100, "value": 1}'
> > > > 
> > > > My query as I understand
> > > > 
> > > > {  
> > > > "query" : {  
> > > > "has\_child" : {  
> > > > "type" : "map",  
> > > > "query" : {  
> > > > "match" : {  
> > > > "mapid" : "101"  
> > > > }  
> > > > }  
> > > > }  
> > > > },  
> > > > "facets" : {  
> > > > "term1" : {  
> > > > "terms" : {  
> > > > "field" : "value"  
> > > > },  
> > > > "global" : true,  
> > > > "facet\_filter" : {  
> > > > "term" : {  
> > > > "mapid" : "100"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > My response
> > > > 
> > > > - hits: [
> > > > 
> > > > - terms: [
> > > > 
> > > > As the scope is global it run the facets on the child documents so it  
> > > > comes back with term 1 is count 3. But my hits are just 1,3
> > > > 
> > > > - 
> > > > 
> > > > I want to run the facets only on child documents whose parent id is  
> > > > 1,3 so the facet result would be
> > > > 
> > > > - terms: [
> > > > - {
> > > > - term: 1
> > > > - count: 1  
> > > > }
> > > > 
> > > > - {
> > > > - term: 2
> > > > - count: 1  
> > > > }  
> > > > ]
> > > > 
> > > > - 
> > > > 
> > > > Is this possible ?
> > > > 
> > > > Thanks,  
> > > > Abhishek
> > > > 
> > > > On Tue, Jul 9, 2013 at 12:20 PM, Matt Weber [matt.weber@gmail.com](mailto:matt.weber@gmail.com)wrote:
> > > > 
> > > > > Yes, you need to set your facet to global and then use a facet  
> > > > > filter to restrict it to just the child docs. See the example here:
> > > > > 
> > > > > [Remove scope support in query and facet dsl. · Issue #2606 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2606)
> > > > > 
> > > > > Thanks,  
> > > > > Matt Weber
> > > > > 
> > > > > On Tue, Jul 9, 2013 at 11:17 AM, Abhishek Andhavarapu \<  
> > > > > [abhishek376@gmail.com](mailto:abhishek376@gmail.com)\> wrote:
> > > > > 
> > > > > > Hi,
> > > > > > 
> > > > > > I'm trying to move from nested relations to parent/child  
> > > > > > relationships as I have to blowup the entire document if I have to udpate  
> > > > > > the nested documents. I use lots of faceting in the nested documents. In  
> > > > > > parent/child relations can I use has\_children filter and run faceting on  
> > > > > > the child documents ? I couldn't find anything on internet.
> > > > > > 
> > > > > > For example  
> > > > > > curl -XPOST '[http://esvm1:9200/pc/parent/\_\*\*search?pretty=true](http://esvm1:9200/pc/parent/_**search?pretty=true)[http://esvm1:9200/pc/map/\_search?pretty=true](http://esvm1:9200/pc/map/_search?pretty=true)'  
> > > > > > -d ' /_/ Run the query on parent type_  
> > > > > > {  
> > > > > > "query": {  
> > > > > > "filtered": {  
> > > > > > "query": {  
> > > > > > "match\_all": {}  
> > > > > > },  
> > > > > > "filter": {  
> > > > > > "and": [  
> > > > > > {  
> > > > > > "has\_child": {  
> > > > > > "type": "map",  
> > > > > > "filter": {  
> > > > > > "and": [  
> > > > > > {  
> > > > > > "term": {  
> > > > > > "mapid": "100"  
> > > > > > }  
> > > > > > }]  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > ]  
> > > > > > }  
> > > > > > }  
> > > > > > },  
> > > > > > "facets": { //\*Run facets on child type \*  
> > > > > > "date1": {  
> > > > > > "date\_histogram": {  
> > > > > > "field": "date",  
> > > > > > "interval": "day"  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }
> > > > > > 
> > > > > > 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 a topic in the  
> > > > > Google Groups "elasticsearch" group.  
> > > > > To unsubscribe from this topic, visit  
> > > > > [https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe](https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe)  
> > > > > .  
> > > > > To unsubscribe from this group and all its topics, 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).
> > > 
> > > --  
> > > You received this message because you are subscribed to a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe](https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe).  
> > > To unsubscribe from this group and all its topics, 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).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe](https://groups.google.com/d/topic/elasticsearch/tuKEqjsSkwY/unsubscribe).  
> To unsubscribe from this group and all its topics, 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:** ![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:27am UTC](https://discuss.elastic.co/t/facets-on-child-documents/12726/8 "2017-07-06T02:27:25Z")

</div>


