# Stats aggregation on a parent field within a children aggregation

**URL:** <https://discuss.elastic.co/t/stats-aggregation-on-a-parent-field-within-a-children-aggregation/150021>\
**Category:** Elasticsearch\
**Created:** [September 26, 2018, 1:28pm UTC](https://discuss.elastic.co/t/stats-aggregation-on-a-parent-field-within-a-children-aggregation/150021 "2018-09-26T13:28:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![voharunado](https://avatars.discourse-cdn.com/v4/letter/v/c0e974/32.png) [@voharunado](https://discuss.elastic.co/u/voharunado)\
**Post date:** [September 26, 2018, 1:28pm UTC](https://discuss.elastic.co/t/stats-aggregation-on-a-parent-field-within-a-children-aggregation/150021/1 "2018-09-26T13:28:22Z")

</div>

Hi,

I am using a Parent-Child structure and I need to aggregate some data from one to another.  
I need to use a Date Histogram aggregation first, on a field that's in the child. I've made a query on the parent (with some filters), then a children aggregation with the Date Histogram in it. The issue is that I need, depending on the dates from the Date Histogram, some data stored in the parent.  
Is there a way to do this in one query? Or maybe I've got things wrong?

Here is my mapping:

```
curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "offer": {
      "properties": {
        "beds": { "type": "integer" },
        "location": { "type": "geo_point" }
      }
    },
    "occupancy": {
      "properties": {
        "period_start_at": { "type": "date" },
        "extract_at": { "type": "date" },
        "price": { "type": "integer" }
      }
    }
  }
}
'

```

And the request I'm trying to do:

```
curl -X POST "localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "filter": [
        {
          "type": {
            "value": "offer"
          }
        },
        {
          "geo_polygon": {
            "location": {
              "points": [...]
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "occupancy": {
      "children": {
        "type": "occupancy"
      },
      "aggs": {
        "filtered": {
          "filter": {
            "bool": {
              "filter": [
                {
                  "type": {
                    "value": "occupancy"
                  }
                },
                {
                  "range": {
                    "extract_at": {
                      "lte": "2018-09-24T00:00:00+00:00"
                    }
                  }
                }
              ]
            }
          },
          "aggs": {
            "period": {
              "date_histogram": {
                "field": "period_start_at",
                "interval": "1w"
              },
              "aggs": {
                "extract": {
                  "terms": {
                    "field": "extract_at",
                    "order": {
                      "_term": "desc"
                    },
                    "size": 1
                  },
                  "aggs": {
                    "beds_filtered": {
                      "filter": {
                        "range": {
                          "beds": {
                            "gt": 0
                          }
                        }
                      },
                      "aggs": {
                        "beds_stats": {
                          "stats": {
                            "field": "beds"
                          }
                        },
                        "beds_percentiles": {
                          "percentiles": {
                            "field": "beds"
                          }
                        }
                      }
                    },
                    "price_filtered": {
                      "filter": {
                        "range": {
                          "price": {
                            "gt": 0
                          }
                        }
                      },
                      "aggs": {
                        "price_stats": {
                          "stats": {
                            "field": "price"
                          }
                        },
                        "price_percentiles": {
                          "percentiles": {
                            "field": "price"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "size": 0
}
'

```

The issue being that the aggregation on "price" will work, but not the one on " **beds**" because beds is stored in the parent.  
I've seen things like \_parent#offer but this only contains the id right? I haven't found a way to do something like "\_parent#offer.beds" and I doubt it is even possible.

---

<div class="post-metadata">

**Author:** ![voharunado](https://avatars.discourse-cdn.com/v4/letter/v/c0e974/32.png) [@voharunado](https://discuss.elastic.co/u/voharunado)\
**Post date:** [October 22, 2018, 9:05am UTC](https://discuss.elastic.co/t/stats-aggregation-on-a-parent-field-within-a-children-aggregation/150021/2 "2018-10-22T09:05:20Z")

</div>

Up, does anyone has a clue?  
Thanks.

---

<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:** [November 19, 2018, 9:05am UTC](https://discuss.elastic.co/t/stats-aggregation-on-a-parent-field-within-a-children-aggregation/150021/3 "2018-11-19T09:05:23Z")

</div>

Closed on November 19, 2018, 9:05am UTC

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