如何知道地理坐标是否位于弹性搜索中的地理多边形内?

How to know if a geo coordinate lies within a geo polygon in elasticsearch?

我正在使用弹性搜索 1.4.1 - 1.4.4。我正在尝试将地理多边形形状(文档)索引到我的索引中,现在当形状被索引时我想知道地理坐标是否位于该特定索引地理多边形形状的边界内。

GET /city/_search
{
"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]
                    ]
                }
            }
        }
    }
}
}

使用上面的地理多边形过滤器,我能够获得所有索引地理坐标都位于所描述的多边形内,但我还需要知道非索引地理坐标是否位于该地理多边形中。我怀疑在弹性搜索 1.4.1 中是否可行。

是的,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
             }
          }
}

这将 return : _id 为 "mumbai"

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

这将 return : _id 为 "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
             }
          }
}

并且此查询将return没有匹配的结果。

{
   "took": 5,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "total": 0,
   "matches": []
}