# How to do a parent document count on a child condition in a parent child mapping?

**URL:** <https://discuss.elastic.co/t/how-to-do-a-parent-document-count-on-a-child-condition-in-a-parent-child-mapping/184785>\
**Category:** Elasticsearch\
**Created:** [June 7, 2019, 4:18pm UTC](https://discuss.elastic.co/t/how-to-do-a-parent-document-count-on-a-child-condition-in-a-parent-child-mapping/184785 "2019-06-07T16:18:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stevens\_Laurent](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevens_laurent/32/42789_2.png) [@Stevens\_Laurent](https://discuss.elastic.co/u/Stevens_Laurent)\
**Post date:** [June 7, 2019, 4:18pm UTC](https://discuss.elastic.co/t/how-to-do-a-parent-document-count-on-a-child-condition-in-a-parent-child-mapping/184785/1 "2019-06-07T16:18:55Z")

</div>

Hello,

I'm in a Parent Child mapping and I'm actually trying to do a parent document count on a child condition.  
My mapping says Parent is a Treatment and child is an event. Event document has a TYP\_EVE that is an error level and i want to know how many treatment has a child that has a TYP\_EVE in 1 or several values that i Specify.

I'v red the doc about improving aggregation performances, and it led me to use the filters aggregation. In fact I've tried to use this kind of aggregation but performance were too low :

```
   {
  "aggs": {
    "top-erreur": {
      "terms": {
        "field": "TYP_EVE.keyword",
        "size": 10
      },
      "aggs": {
        "to-answers": {
          "children": {
            "type" : "evenement" 
          }
        }
      }
    }
  }
}

```

`So I'm trying to do this kind of requests but i'm struggling. Anyone has an idea of how i can do this or what can i experiment ?`

```
    {
      "size": 0,
      "aggs" : {
        "messages" : {
          "filters" : {
            "other_bucket_key": "other_messages",
            "filters" : {
              "errors" : { 
              	"match" : { 
              		"query":{
              		"has_child" : {
            			"type" : "evenement",
            			"query":{
            				"bool" : {
            					"must" : 
            					[{
            						"match":{
            						"STA_EVE" : "A"
            					}}]
            				}
            			}
            			
              }
              }}
            }
          }
        }}
      }
    }

```

Thanks for your help

---

<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 5, 2019, 4:18pm UTC](https://discuss.elastic.co/t/how-to-do-a-parent-document-count-on-a-child-condition-in-a-parent-child-mapping/184785/2 "2019-07-05T16:18:57Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
