您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Elasticsearch中知道地理坐标是否在地理多边形内?

如何在Elasticsearch中知道地理坐标是否在地理多边形内?

是的,可以使用Percolator解决此问题。

与Elasticsearch的正常使用情况一样,我们将文档索引到elasticsearch中,然后对索引数据运行查询以检索匹配的/必需的文档。

但是渗滤器的工作方式却不同。

在渗滤器中,您注册查询,然后通过注册查询对文档进行渗滤,并取回与您的文档匹配的查询

经过无数的Google搜索结果和许多博客之后,我找不到任何可以解释我如何使用渗滤器解决此问题的东西。

因此,我以一个示例进行说明,以便其他面临相同问题的人可以从我的问题和找到的解决方案中得到一些提示。我希望有人可以改善我的答案,或者可以分享一种更好的方法

例如:-

首先,我们需要创建一个索引。

PUT /city/

然后,我们需要为用户文档添加一个映射,其中包含用户的纬度-经度,以对注册查询进行渗透。

PUT /city/user/_mapping
{
    "user" : {
        "properties" : {
            "location" : {
                "type" : "geo_point"
            }
        }
    }
}

现在,我们可以将地理多边形查询注册为渗滤器,其ID为城市名称或您想要的任何其他标识符。

PUT /city/.percolator/mumbai
{
    "query":{
        "filtered" : {
            "query" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_polygon" : {
                    "location" : {
                        "points" : [
                            [72.776491, 19.259634],
                            [72.955705, 19.268060],
                            [72.945406, 19.189611],
                            [72.987291, 19.169507],
                            [72.963945, 19.069596],
                            [72.914506, 18.994300],
                            [72.873994, 19.007933],
                            [72.817689, 18.896882],
                            [72.816316, 18.941052],
                            [72.816316, 19.113720],
                            [72.816316, 19.113720],
                            [72.790224, 19.192205],
                            [72.776491, 19.259634]
                        ]
                    }
                }
            }
        }
    }
}

让我们为另一个城市注册一个地理多边形过滤器

PUT /city/.percolator/delhi
{
    "query":{
        "filtered" : {
            "query" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_polygon" : {
                    "location" : {
                        "points" : [
                            [76.846998, 28.865160],
                            [77.274092, 28.841104],
                            [77.282331, 28.753252],
                            [77.482832, 28.596619],
                            [77.131269, 28.395064],
                            [76.846998, 28.865160]
                        ]
                    }
                }
            }
        }
    }
}

现在,我们已将2个查询注册为渗滤器,并且可以通过进行此API调用来确保。

GET /city/.percolator/_count

现在要知道在任何注册城市中是否都存在地理点,我们可以使用以下查询用户文档进行过滤。

GET /city/user/_percolate
{
  "doc": {
        "location" : {
            "lat" : 19.088415,
            "lon" : 72.871248
             }
          }
}

这将返回:_id为“孟买”

{
   "took": 25,
   "_shards": {
      "total": 5,
      "successful": 5,
      "Failed": 0
   },
   "total": 1,
   "matches": [
      {
         "_index": "city",
         "_id": "mumbai"
      }
   ]
}

用不同的纬度尝试另一个查询

GET /city/user/_percolate
{
  "doc": {
        "location" : {
            "lat" : 28.539933,
            "lon" : 77.331770
             }
          }
    }

这将返回:_id as“ delhi”

{
   "took": 25,
   "_shards": {
      "total": 5,
      "successful": 5,
      "Failed": 0
   },
   "total": 1,
   "matches": [
      {
         "_index": "city",
         "_id": "delhi"
      }
   ]
}

让我们用随机纬度运行另一个查询

GET /city/user/_percolate
{
  "doc": {
        "location" : {
            "lat" : 18.539933,
            "lon" : 45.331770
             }
          }
}

查询将不返回匹配结果。

{
   "took": 5,
   "_shards": {
      "total": 5,
      "successful": 5,
      "Failed": 0
   },
   "total": 0,
   "matches": []
}
其他 2022/1/1 18:20:32 有545人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶