# Nested facet counts for matching parent records

**URL:** <https://discuss.elastic.co/t/nested-facet-counts-for-matching-parent-records/9451>\
**Category:** Elasticsearch\
**Created:** [October 23, 2012, 9:28pm UTC](https://discuss.elastic.co/t/nested-facet-counts-for-matching-parent-records/9451 "2012-10-23T21:28:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discuss.elastic.co/u/Mike)\
**Post date:** [October 23, 2012, 9:28pm UTC](https://discuss.elastic.co/t/nested-facet-counts-for-matching-parent-records/9451/1 "2012-10-23T21:28:54Z")

</div>

Assume I have 2 records:  
{ session:"a", activities:[{action : "jump"}, {action : "run"}] }  
{ session:"b", activities:[ {action : "jump"}, {action : "jog"}, {action :  
"jog"}, {action : "jump"} ] }

If I do a nested facet, I will get counts based on the # of actions that  
matched:  
jump : 3  
run : 1  
jog : 2

How can I get counts for the number of parent records that matched instead?  
ie:  
jump : 2  
run : 1  
jog : 1

--

---

<div class="post-metadata">

**Author:** ![Xiang\_Li](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@Xiang\_Li](https://discuss.elastic.co/u/Xiang_Li)\
**Post date:** [October 24, 2012, 7:46am UTC](https://discuss.elastic.co/t/nested-facet-counts-for-matching-parent-records/9451/2 "2012-10-24T07:46:58Z")

</div>

You can write your query code here ...

在 2012年10月24日星期三UTC+8上午5时28分54秒，Mike写道：

> Assume I have 2 records:  
> { session:"a", activities:[{action : "jump"}, {action : "run"}] }  
> { session:"b", activities:[ {action : "jump"}, {action : "jog"}, {action :  
> "jog"}, {action : "jump"} ] }
> 
> If I do a nested facet, I will get counts based on the # of actions that  
> matched:  
> jump : 3  
> run : 1  
> jog : 2
> 
> How can I get counts for the number of parent records that matched  
> instead? ie:  
> jump : 2  
> run : 1  
> jog : 1

--

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discuss.elastic.co/u/Mike)\
**Post date:** [October 24, 2012, 3:06pm UTC](https://discuss.elastic.co/t/nested-facet-counts-for-matching-parent-records/9451/3 "2012-10-24T15:06:08Z")

</div>

Here are the mappings, records, and query:

{  
"mappings" : {  
"article" : {  
"properties" : {  
"activities" : {  
"type" : "nested",  
"dynamic" : "true",  
"properties" : {  
"action" : { "type" : "string" }  
}  
},  
"session" : { "type" : "string" }  
}  
}  
}  
}

{ session:"a", activities:[{action : "jump"}, {action : "run"}] }  
{ session:"b", activities:[ {action : "jump"}, {action : "jog"}, {action :  
"jog"}, {action : "jump"} ] }

{  
"query" : { "match\_all" : {} },  
"facets": {  
"test" : {  
"nested" : "activities",  
"terms" : { "field" : "action" }  
}  
}  
}

On Tuesday, October 23, 2012 5:28:54 PM UTC-4, Mike wrote:

> Assume I have 2 records:  
> { session:"a", activities:[{action : "jump"}, {action : "run"}] }  
> { session:"b", activities:[ {action : "jump"}, {action : "jog"}, {action :  
> "jog"}, {action : "jump"} ] }
> 
> If I do a nested facet, I will get counts based on the # of actions that  
> matched:  
> jump : 3  
> run : 1  
> jog : 2
> 
> How can I get counts for the number of parent records that matched  
> instead? ie:  
> jump : 2  
> run : 1  
> jog : 1

--

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discuss.elastic.co/u/Mike)\
**Post date:** [October 26, 2012, 4:07pm UTC](https://discuss.elastic.co/t/nested-facet-counts-for-matching-parent-records/9451/4 "2012-10-26T16:07:00Z")

</div>

Does anyone know if there is a way to get back the number of parent records  
that matched instead of the number of child records?

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [October 26, 2012, 5:29pm UTC](https://discuss.elastic.co/t/nested-facet-counts-for-matching-parent-records/9451/5 "2012-10-26T17:29:18Z")

</div>

If you want to get the number of parent records, you need to run your facet  
against parent records. You can do it by including fields from nested  
objects into parent objects:

{  
"mappings" : {  
"article" : {  
"properties" : {  
"activities" : {  
"type" : "nested",  
"dynamic" : true,

- 

```
       "include_in_parent": true,*
      "properties" : {
        "action" : { "type" : "string" }
      }
    },
    "session" : { "type" : "string" }
  }
}

```

}  
}

{  
"query" : { "match\_all" : {} },  
"facets": {  
"test" : {  
"terms" : {  
"field" : "activities.action"  
}  
}  
}  
}

On Friday, October 26, 2012 12:07:01 PM UTC-4, Mike wrote:

> Does anyone know if there is a way to get back the number of parent  
> records that matched instead of the number of child records?

--

---

<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, 3:07am UTC](https://discuss.elastic.co/t/nested-facet-counts-for-matching-parent-records/9451/6 "2017-07-06T03:07:03Z")

</div>

Closed on July 6, 2017, 3:07am UTC


