Aggregation with geo bounding box filter using using Elasticsearch DSL Python

I have the following query which is working fine, test it in kibana with my data:

GET my_index/_search?size=0
{
  "aggs": {
    "geo_bounding_box": {
      "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": 5,
            "size": 10000
          }
        }
      }
    }
  }
}

I try use elasticsearch_dsl for building this query but i have malformed query after this. Here is snippet of my code:

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
)

Here is snippet of result:

{
  "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": 5,
              "size": 10000
            }
          }
        }
      }
    }
  }
}

How i can get a aggregation query, like query in top of question with help of elasticsearch_dsl?

Source: Python Questions

LEAVE A COMMENT