pythonelasticsearchelasticsearch-aggregationelasticsearch-dsl-py

Elasticsearch DSL. Build bounding box filter inside aggregation


I need build geotitle_grid aggregation with geo_bounding_box

Here is 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
)

But after build this query in elasticsearch_dsl library, i have error:

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

Here is query. which was build:

{
  "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
}

How i can build write aggregation query with bounding box filter and where i have mistake? Will be grateful for the help.


Solution

  • You need to chain AggregationGeotileGrid and AggregationGeotileGridBuckets so you should try this way:

    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
            }
         )