Alessandro Desantis
Alessandro Desantis

Reputation: 14343

Filter parent by children aggregation in Elasticsearch

I have a parent-children relationship in my ES mapping and I want to filter the parents by the value of an aggregation (avg) on their children. That is, I only want to retrieve parents where that value is within a given range.

I tried to do it with aggs and post-filters but couldn't get it to work.

{
  "apartments" : {
    "mappings" : {
      "apartment_availability" : {
        "_parent" : {
          "type" : "apartment"
        },
        "_routing" : {
          "required" : true
        },
        "properties" : {
          "availability_date" : {
            "type" : "date"
          },
          "apartment_id" : {
            "type" : "long"
          },
          "id" : {
            "type" : "long"
          },
          "price_cents" : {
            "type" : "long"
          },
          "status" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      },
      "apartment" : {
        "properties" : {
          "id" : {
            "type" : "long"
          },
        }
      }
    }
  }
}

If oru users select a period of March 24th-March 31st and a price range of €150-€300, then we want to show them all apartments that are free in that period and whose average price for that period is in the €150-€300 range.

Here's what we have so far:

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [{
            "has_child": {
              "type": "apartment_availability",
              "min_children": 8,
              "max_children": 8,
              "query": {
                "bool": {
                  "must": [{
                    "term": {
                      "status": "available"
                    }
                  }, {
                    "range": {
                      "availability_date": {
                        "gte": "2017-03-24",
                        "lte": "2017-03-31"
                      }
                    }
                  }]
                }
              }
            }
          }]
        }
      }
    }
  }
}

Upvotes: 1

Views: 1010

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

My suggestion, using bucket_selector aggregation to choose between apartments:

GET /apartments/apartment/_search
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "has_child": {
                "type": "apartment_availability",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "status": "available"
                        }
                      },
                      {
                        "range": {
                          "availability_date": {
                            "gte": "2017-04-01",
                            "lte": "2017-04-03"
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "apartments_ids": {
      "terms": {
        "field": "id",
        "size": 10
      },
      "aggs": {
        "avails": {
          "children": {
            "type": "apartment_availability"
          },
          "aggs": {
            "filter_avails": {
              "filter": {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "status": "available"
                      }
                    },
                    {
                      "range": {
                        "availability_date": {
                          "gte": "2017-04-01",
                          "lte": "2017-04-03"
                        }
                      }
                    }
                  ]
                }
              },
              "aggs": {
                "average": {
                  "avg": {
                    "field": "price_cents"
                  }
                }
              }
            }
          }
        },
        "avg_bucket_filter": {
          "bucket_selector": {
            "buckets_path": {
              "avg": "avails>filter_avails.average"
            },
            "script": "params.avg > 150 && params.avg < 300"
          }
        }
      }
    }
  }
}

Upvotes: 1

Related Questions