# Apply filter all sub aggregations (for parent-child aggregations)

**URL:** <https://discuss.elastic.co/t/apply-filter-all-sub-aggregations-for-parent-child-aggregations/82427>\
**Category:** Elasticsearch\
**Created:** [April 14, 2017, 3:44pm UTC](https://discuss.elastic.co/t/apply-filter-all-sub-aggregations-for-parent-child-aggregations/82427 "2017-04-14T15:44:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gulcabuk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gulcabuk/32/18508_2.png) [@gulcabuk](https://discuss.elastic.co/u/gulcabuk)\
**Post date:** [April 14, 2017, 3:44pm UTC](https://discuss.elastic.co/t/apply-filter-all-sub-aggregations-for-parent-child-aggregations/82427/1 "2017-04-14T15:44:40Z")

</div>

Hello,

I have parent-child documents like below.

DATA:  
**company** (name, address etc..) -\> as parent  
**invoice** (id, amount, tax, **document\_type** (id, code, type)) --\> as child  
**invoice\_detail** (id, price) --\> as grandchild  
(ps: document\_type is a inner json in invoice but not nested)

I've prepared a query that

1. aggregates my companies according to their names
2. get sum of the prices of invoice\_details.
3. filter second result according to some of document\_type.code of invoice.

The problem is that I've got same result for second and third column.

PART OF RESULT:

```
{
"key": "Company9999",
"doc_count": 1,
"price_sum": {
"doc_count": 41,
"price_sum": {
  "doc_count": 147,
  "price_sum": {
    "value": 5835.800000000001
  }
}
},
"price_sum(filters)": {
"buckets": [
  {
    "doc_count": 1,
    "sales_net_price_sum(filters)": {
      "doc_count": 41,
      "sales_net_price_sum(filters)": {
        "doc_count": 147,
        "sales_net_price_sum(filters)": {
          "value": 5835.800000000001
        }
      }
    }
  }
]
}
}

```

I suppose that filter is not applied to child aggregation of my third aggregation. For the future I want to put multiple filter from different level of types (invoice,company or invoice\_detail) so I've put my filter outside of my aggregations for third column and want this filter is applied to all sub-aggregations.

Is there a way to apply my filter to all aggregations? How can I get right results from my query?

QUERY:

```
{
"size" : 0,
"query" : {
"bool" : {
  "disable_coord" : false,
  "adjust_pure_negative" : true,
  "boost" : 1.0
}
},
"aggregations" : {
"name" : {
  "terms" : {
    "field" : "name.keyword",
    "size" : 2147483647,
    "min_doc_count" : 1,
    "shard_min_doc_count" : 0,
    "show_term_doc_count_error" : false,
    "order" : [
      {
        "_count" : "desc"
      },
      {
        "_term" : "asc"
      }
    ]
  },
  "aggregations" : {
    "price_sum" : {
      "children" : {
        "type" : "invoice"
      },
      "aggregations" : {
        "price_sum" : {
          "children" : {
            "type" : "invoice_detail"
          },
          "aggregations" : {
            "price_sum" : {
              "sum" : {
                "field" : "sales_net_price"
              }
            }
          }
        }
      }
    },
    "price_sum(filters)" : {
      "filters" : {
        "filters" : [
          {
            "bool" : {
              "filter" : [
                {
                  "has_child" : {
                    "query" : {
                      "bool" : {
                        "must" : [
                          {
                            "terms" : {
                              "document_type.code.keyword" : [
                                "AAA",
                                "BBB",
                                "CCC"
                              ],
                              "boost" : 1.0
                            }
                          }
                        ],
                        "disable_coord" : false,
                        "adjust_pure_negative" : true,
                        "boost" : 1.0
                      }
                    },
                    "type" : "invoice",
                    "score_mode" : "sum",
                    "min_children" : 0,
                    "max_children" : 2147483647,
                    "ignore_unmapped" : false,
                    "boost" : 1.0
                  }
                }
              ],
              "disable_coord" : false,
              "adjust_pure_negative" : true,
              "boost" : 1.0
            }
          }
        ],
        "other_bucket" : false,
        "other_bucket_key" : "_other_"
      },
      "aggregations" : {
        "price_sum(filters)" : {
          "children" : {
            "type" : "invoice"
          },
          "aggregations" : {
            "price_sum(filters)" : {
              "children" : {
                "type" : "invoice_detail"
              },
              "aggregations" : {
                "price_sum(filters)" : {
                  "sum" : {
                    "field" : "sales_net_price"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
}
}
```

---

<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:** [May 12, 2017, 3:50pm UTC](https://discuss.elastic.co/t/apply-filter-all-sub-aggregations-for-parent-child-aggregations/82427/2 "2017-05-12T15:50:34Z")

</div>

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