Stats aggregation on a parent field within a children aggregation

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.

Up, does anyone has a clue?
Thanks.

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