【问题标题】:Elasticsearch DSL. Build bounding box filter inside aggregation弹性搜索 DSL。在聚合内构建边界框过滤器
【发布时间】:2021-10-08 12:14:23
【问题描述】:

我需要用 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] 格式错误的查询,应为 [END_OBJECT] 但找到 [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
}

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

【问题讨论】:

    标签: python elasticsearch elasticsearch-aggregation elasticsearch-dsl-py


    【解决方案1】:

    您需要链接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
            }
         )
    

    【讨论】:

    • 酷,很高兴它成功了!还有什么需要的吗?
    • 不,谢谢。你帮了我很多。坚持了几天。感谢您的帮助。
    猜你喜欢
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 2014-07-11
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多