How do I pass Parent Aggregation key to Child Aggregation Painless Script in Elastic search DSL Query?

Viewed 445

I have a quick question about passing the filters key to the scripted language.

Basically, I have defined the filters as below, In my scripted query, I want to use the filter key month as part of the DOM average calculation. In my example, I have hardcoded a date value, but it should be the key value of the filters definition. Would appreciate it if you could give me some feedback on how that could be worked out.

Snippet of DSL query

Here is the full query:

GET offmarketlistings,listings,soldlistings/_search
  {
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "isRental": {
              "value": false
            }
          }
        },
        {
          "terms": {
            "zip": [
              "20852",
              "20001",
              "20878"
            ]
          }
        },
        {
          "range": {
            "sqft": {
              "gte": 1500.0,
              "lte": 3500.0
            }
          }
        },
        {
          "range": {
            "bed": {
              "gte": 1.0,
              "lte": 7.0
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "zip_bucket": {
      "terms": {
        "field": "zip"
      },
      "aggs": {
        "active_monthly_bucket": {
          "filters": {
            "filters": {
              "7/1/2020": {
                "bool": {
                  "must": [
                    {
                      "range": {
                        "listDate": {
                          "lt": "2020/08/01",
                          "format": "yyyy/MM/dd"
                        }
                      }
                    },
                    {
                      "bool": {
                        "should": [
                          {
                            "bool": {
                              "must_not": [
                                {
                                  "exists": {
                                    "field": "soldDate"
                                  }
                                }
                              ]
                            }
                          },
                          {
                            "bool": {
                              "must": [
                                {
                                  "exists": {
                                    "field": "soldDate"
                                  }
                                },
                                {
                                  "range": {
                                    "soldDate": {
                                      "gt": "2020/07/01",
                                      "format": "yyyy/MM/dd"
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    },
                    {
                      "bool": {
                        "should": [
                          {
                            "bool": {
                              "must_not": [
                                {
                                  "exists": {
                                    "field": "offMarketDate"
                                  }
                                }
                              ]
                            }
                          },
                          {
                            "bool": {
                              "must": [
                                {
                                  "exists": {
                                    "field": "offMarketDate"
                                  }
                                },
                                {
                                  "range": {
                                    "offMarketDate": {
                                      "gt": "2020/07/01",
                                      "format": "yyyy/MM/dd"
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              "9/1/2019": {
                "bool": {
                  "must": [
                    {
                      "range": {
                        "listDate": {
                          "lt": "2019/10/01",
                          "format": "yyyy/MM/dd"
                        }
                      }
                    },
                    {
                      "bool": {
                        "should": [
                          {
                            "bool": {
                              "must_not": [
                                {
                                  "exists": {
                                    "field": "soldDate"
                                  }
                                }
                              ]
                            }
                          },
                          {
                            "bool": {
                              "must": [
                                {
                                  "exists": {
                                    "field": "soldDate"
                                  }
                                },
                                {
                                  "range": {
                                    "soldDate": {
                                      "gt": "2019/09/01",
                                      "format": "yyyy/MM/dd"
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    },
                    {
                      "bool": {
                        "should": [
                          {
                            "bool": {
                              "must_not": [
                                {
                                  "exists": {
                                    "field": "offMarketDate"
                                  }
                                }
                              ]
                            }
                          },
                          {
                            "bool": {
                              "must": [
                                {
                                  "exists": {
                                    "field": "offMarketDate"
                                  }
                                },
                                {
                                  "range": {
                                    "offMarketDate": {
                                      "gt": "2019/09/01",
                                      "format": "yyyy/MM/dd"
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              "8/1/2019": {
                "bool": {
                  "must": [
                    {
                      "range": {
                        "listDate": {
                          "lt": "2019/09/01",
                          "format": "yyyy/MM/dd"
                        }
                      }
                    },
                    {
                      "bool": {
                        "should": [
                          {
                            "bool": {
                              "must_not": [
                                {
                                  "exists": {
                                    "field": "soldDate"
                                  }
                                }
                              ]
                            }
                          },
                          {
                            "bool": {
                              "must": [
                                {
                                  "exists": {
                                    "field": "soldDate"
                                  }
                                },
                                {
                                  "range": {
                                    "soldDate": {
                                      "gt": "2019/08/01",
                                      "format": "yyyy/MM/dd"
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    },
                    {
                      "bool": {
                        "should": [
                          {
                            "bool": {
                              "must_not": [
                                {
                                  "exists": {
                                    "field": "offMarketDate"
                                  }
                                }
                              ]
                            }
                          },
                          {
                            "bool": {
                              "must": [
                                {
                                  "exists": {
                                    "field": "offMarketDate"
                                  }
                                },
                                {
                                  "range": {
                                    "offMarketDate": {
                                      "gt": "2019/08/01",
                                      "format": "yyyy/MM/dd"
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            }
          },
          "aggs": {
            "listPrice_Stats": {
              "stats": {
                "field": "listPrice"
              }
            },
            "listPrice_median": {
              "percentiles": {
                "field": "listPrice",
                "percents": [
                  50.0
                ]
              }
            },
            "dom_stats": {
              "stats": {
                "script": {
                  "inline": "ChronoUnit.DAYS.between(ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc['listDate'].value), ZoneId.of('Z')), ZonedDateTime.parse('2020-07-22T16:45:08Z'))"
                }
              }
            }
          }
        }
      }
    }
  }
}
1 Answers

Scripts don't have access to those filters' names.

They do, however, support parameters. So what's usually done is that when you construct your query in the language of your choice, you pass those 'hard-coded' values to the params part of the script -- as key-value pairs. The values can of course be arrays (all your filters' keys).

Related