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

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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
}
}

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