gpt4 book ai didi

elasticsearch - 带有聚合的Elasticsearch地理查询

转载 作者:行者123 更新时间:2023-12-03 02:25:45 25 4
gpt4 key购买 nike

我有一个包含用户位置的elasticsearch索引。
我需要使用geohash网格通过地理边界框执行汇总查询,对于文档计数少于某个值的存储桶,我需要返回所有文档。

我怎样才能做到这一点?

最佳答案

由于您尚未提供有关所创建索引和用户位置的任何相关信息。

我正在考虑以下数据:

索引Def

{
"mappings": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}

索引样本文档
POST _bulk

{"index":{"_id":1}}
{"location":"52.37408,4.912350","name":"The golden dragon"}
{"index":{"_id":2}}
{"location":"52.369219,4.901618","name":"Burger King"}
{"index":{"_id":3}}
{"location":"52.371667,4.914722","name":"Wendys"}
{"index":{"_id":4}}
{"location":"51.222900,4.405200","name":"Taco Bell"}
{"index":{"_id":5}}
{"location":"48.861111,2.336389","name":"McDonalds"}
{"index":{"_id":6}}
{"location":"48.860000,2.327000","name":"KFC"}

根据您的问题:

When requesting detailed buckets a filter like geo_bounding_box should be applied to narrow the subject area



要了解更多信息,可以引用此 official ES doc
  • 现在,为了过滤基于doc_count的聚合数据,我们可以使用bucket_selector管道聚合。

  • documentation

    Pipeline aggregations work on the outputs produced from other aggregations rather than from document sets, adding information to the output tree.



    因此,计算doc_count所需完成的工作量将是相同的。

    查询
        {
    "aggs": {
    "location": {
    "filter": {
    "geo_bounding_box": {
    "location": {
    "top_left": {
    "lat": 52.5225,
    "lon": 4.5552
    },
    "bottom_right": {
    "lat": 52.2291,
    "lon": 5.2322
    }
    }
    }
    },
    "aggs": {
    "around_amsterdam": {
    "geohash_grid": {
    "field": "location",
    "precision": 8
    },
    "aggs": {
    "the_filter": {
    "bucket_selector": {
    "buckets_path": {
    "the_doc_count": "_count"
    },
    "script": "params.the_doc_count < 2"
    }
    }
    }
    }
    }
    }
    }
    }

    搜索结果
    "hits": {
    "total": {
    "value": 6,
    "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
    {
    "_index": "restaurant",
    "_type": "_doc",
    "_id": "1",
    "_score": 1.0,
    "_source": {
    "location": "52.37408,4.912350",
    "name": "The golden dragon"
    }
    },
    {
    "_index": "restaurant",
    "_type": "_doc",
    "_id": "2",
    "_score": 1.0,
    "_source": {
    "location": "52.369219,4.901618",
    "name": "Burger King"
    }
    },
    {
    "_index": "restaurant",
    "_type": "_doc",
    "_id": "3",
    "_score": 1.0,
    "_source": {
    "location": "52.371667,4.914722",
    "name": "Wendys"
    }
    },
    {
    "_index": "restaurant",
    "_type": "_doc",
    "_id": "4",
    "_score": 1.0,
    "_source": {
    "location": "51.222900,4.405200",
    "name": "Taco Bell"
    }
    },
    {
    "_index": "restaurant",
    "_type": "_doc",
    "_id": "5",
    "_score": 1.0,
    "_source": {
    "location": "48.861111,2.336389",
    "name": "McDonalds"
    }
    },
    {
    "_index": "restaurant",
    "_type": "_doc",
    "_id": "6",
    "_score": 1.0,
    "_source": {
    "location": "48.860000,2.327000",
    "name": "KFC"
    }
    }
    ]
    },
    "aggregations": {
    "location": {
    "doc_count": 3,
    "around_amsterdam": {
    "buckets": [
    {
    "key": "u173zy3j",
    "doc_count": 1
    },
    {
    "key": "u173zvfz",
    "doc_count": 1
    },
    {
    "key": "u173zt90",
    "doc_count": 1
    }
    ]
    }
    }
    }
    }

    它将根据 "params.the_doc_count < 2"筛选出所有少于2个文档

    关于elasticsearch - 带有聚合的Elasticsearch地理查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61104746/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com