弹性搜索:搜索一组求和字段



我有一个基准指数,其中包含来自医院病房的数据,我想知道每周病房位置的药物使用情况。

现在的问题是,当我选择一周的数据时,具有多个选区的位置的人口将计算错误。我想知道该周某个地点的平均人口。该位置的人口是该位置内各行政区的人口总和

{
    "_index": "development_benchmark",
    "_type": "score",
    "_id": "3147603",
    "_score": null,
    "_source": {
      "id": 3147603,
      "score": 86,
      "amount": 1758.7,
      "population": 2045,
      "date": "2018-01-12T03:00:00+01:00",
      "benchmark_id": 10,
      "ward": {
        "id": 24170,
        "ward_types": [
          3
        ]
      },
      "location_id": 24285,
      "cluster_id": 72997
    }
}

我的查询如下所示:

GET development_benchmark/_search
{
 "query": {
    "bool": {
        "must": [
        {
            "range": {
            "date": {
                "gte": "2017-12-20",
              "lte": "2017-12-20"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "benchmarks": {
        "terms": {
            "field": "benchmark_id",
        "size": 100
      },
      "aggs": {
            "clusterScore": {
                "filter": {
                    "bool": {
                        "must": [
                {
                    "term": {
                    "cluster_id": 72997
                  }
                }
              ]
            }
          },
          "aggs": {
              "amount": {
                  "sum": {
                      "field": "amount"
                  }
              },
              "population": {
                  "sum": {
                      "field": "population"
                  }
              },
              "locations": {
                  "terms": {
                            "field": "location_id",
                "size": 1000
              },
              "aggs": {
                            "amount": {
                                "sum": {
                                    "field": "amount"
                  }
                },
                "population": {
                                "sum": {
                                    "field": "population"
                  }
                }
              }
            }
          }
      }
    }
  }
}

答案是聚合位置,例如:

"locations"  => [
        "terms" => [
            "field" => "location_id",
            "size"  => 1000
        ],
        "aggs" => [
            "wards"      => [
                "terms" => [
                    "field" => "ward.id",
                    "size"  => 1000
                ],
                "aggs"  => [
                    "amount"     => [
                        "avg" => [
                            "field" => "amount"
                        ]
                    ],
                    "population" => [
                        "avg" => [
                            "field" => "population"
                        ]
                    ]
                ]
            ],
            "amount"     => [
                "sum_bucket" => [
                    "buckets_path" => "wards>amount"
                ]
            ],
            "population" => [
                "sum_bucket" => [
                    "buckets_path" => "wards>population"
                ]
            ]
        ]
    ]
]

这将计算所选时间内选区的平均人口。然后将每个位置的所有平均病房人口相加

最新更新