弹性搜索 DSL。在聚合内部构建边界框过滤器

Elasticsearch DSL. Build bounding box filter inside aggregation

我需要使用 geo_bounding_box

构建 geotitle_grid 聚合

这是我的代码:

search_query: DslSearch = DslSearch()

enriched_value = {
    "aggregations": {
        "AggregationGeotileGridBuckets": {
            "geotile_grid": {
                "field": "location",
                "precision": 8,
                "size": 10000
            }
        }
    }
}

bucket_builder = search_query.aggs.bucket(
    name='My bucket',
    agg_type='filter',
    **{
        "geo_bounding_box": {
            "location": {
                "top_left": {
                    "lat": 38.2715027604674,
                    "lon": -121.925823154605
                },
                "bottom_right": {
                    "lat": 37.2876652395326,
                    "lon": -122.91285484539499
                }
            }
        }
    },
    **enriched_value
)

但是在 elasticsearch_dsl 库中构建此查询后,出现错误:

elasticsearch.exceptions.RequestError: RequestError(400, 'parsing_exception', '[geo_bounding_box] malformed query, expected [END_OBJECT] but found [FIELD_NAME]')

这里是查询。这是构建:

{
  "query": {
    "geo_distance": {
      "distance": "497668.76297287986m",
      "location": {
        "lat": 38.99939107394144,
        "lon": -124.2156036484375
      }
    }
  },
  "aggs": {
    "AggregationGeotileGrid": {
      "filter": {
        "geo_bounding_box": {
          "location": {
            "top_left": {
              "lat": 38.2715027604674,
              "lon": -121.925823154605
            },
            "bottom_right": {
              "lat": 37.2876652395326,
              "lon": -122.91285484539499
            }
          }
        },
        "aggregations": {
          "AggregationGeotileGridBuckets": {
            "geotile_grid": {
              "field": "location",
              "precision": 8,
              "size": 10000
            }
          }
        }
      }
    }
  },
  "size": 0
}

我如何使用边界框过滤器构建写入聚合查询以及我哪里出错了? 将不胜感激。

您需要链接 AggregationGeotileGridAggregationGeotileGridBuckets,所以您应该尝试这种方式:

bucket_builder = search_query.aggs.bucket(
        name='AggregationGeotileGrid',
        agg_type='filter',
        **{
            "geo_bounding_box": {
                "location": {
                    "top_left": {
                        "lat": 38.2715027604674,
                        "lon": -121.925823154605
                    },
                    "bottom_right": {
                        "lat": 37.2876652395326,
                        "lon": -122.91285484539499
                    }
                }
            }
        }
     )
     .bucket(
        name='AggregationGeotileGridBuckets',
        agg_type='geotile_grid',
        **{
             "field": "location",
             "precision": 8,
             "size": 10000
        }
     )