如何知道地理坐标是否位于弹性搜索中的地理多边形内? [英] How to know if a geo coordinate lies within a geo polygon in elasticsearch?

查看:158
本文介绍了如何知道地理坐标是否位于弹性搜索中的地理多边形内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  GET / city / _search 
{
query:{
filtered:{
查询:{
match_all:{}
},
过滤器:{
geo_polygon:{
location:{
分:[
[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]
]
}
}
}
}
}
}

上面的geo多边形过滤器我可以得到所有索引的地理坐标位于所描述的多边形内,但是我还需要知道一个非索引的地理坐标是否在这个地形多边形中。我的疑问是,如果在弹性搜索中有可能,1.4.1。

解决方案

是的,Percolator可以用来解决这个问题。



在弹性搜索的正常使用情况下,我们将我们的文档索引到弹性搜索,然后我们对索引数据运行查询以检索匹配/必需的文档。



但是渗透者的工作方式与其它方式不同。



在渗透者中,您注册您的查询,然后您渗透您的文档通过注册查询,并返回与您的文档匹配的查询。



经过无数的google结果和许多博客,我无法找到任何这可以解释我如何使用渗滤器来解决这个问题。



所以我用一个例子来解释这个问题,以便面对同样问题的其他人可以提示我的问题和解决方案我发现。我想,如果有人可以改善我的答案,或者可以分享一个更好的做法。



例如: -



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

  PUT / city / 

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

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

现在,我们可以注册我们的地理多边形查询作为城市名称或任何其他标识符的渗透者。

  PUT /city/.percolator/mumbai 
{
查询:{
filtered:{
查询:{
match_all:{}
},
过滤器: {
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为mumbai

  {
:25,
_shards:{
total:5,
success:5,
failed:0
},
total:1,
matches:[
{
_index:city,
_id:mumbai
}
]
}

尝试使用不同的lat-lon另一个查询

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

这将返回:_id为delhi

  {
take:25,
_shards:{
total:5,
successful:5,
failed
},
total:1,
matches:[
{
_index:city,
_id delhi
}
]
}

查询随机拉特朗

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

,此查询将不会返回匹配结果。

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


I am using elastic search 1.4.1 - 1.4.4. I'm trying to index a geo polygon shape (document) into my index and now when the shape is indexed i want to know if a geo coordinate lies within the boundaries of that particular indexed geo-polygon shape.

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

With above geo polygon filter i'm able get all indexed geo-coordinates lies within described polygon but i also need to know if a non-indexed geo-coordinate lies with in this geo polygon or not. My doubt is that if that is possible in the elastic search 1.4.1.

解决方案

Yes, Percolator can be used to solve this problem.

As in normal use case of Elasticsearch, we index our docs into elasticsearch and then we run queries on indexed data to retrieve matched/ required documents.

But percolators works in a different way of it.

In percolators you register your queries and then you percolate your documents through registered queries and gets back the queries which matches your documents.

After going through infinite number of google results and many of blogs i wasn't able to find any thing which could explain how i can use percolators to solve this problem.

So i'm explaining this with an example so that other people facing same problem can take a hint from my problem and the solution i found. I would like if someone can improve my answer or can share a better approach of doing it.

e.g:-

First of all we need to create an index.

PUT /city/

then, we need to add a mapping for user document which consist a user's latitude-longitude for percolating against registered queries.

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

Now, we can register our geo polygon queries as percolators with id as city name or any other identifier you want to.

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

Let's register another geo polygon filter for another city

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

Now we have registered 2 queries as percolators and we can make sure by making this API call.

GET /city/.percolator/_count

Now to know if a geo point exist with any of registered cities we can percolate a user document using below query.

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

This will return : _id as "mumbai"

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

trying another query with different lat-lon

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

This will return : _id as "delhi"

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

Let's run another query with random lat-lon

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

and this query will return no matched results.

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

这篇关于如何知道地理坐标是否位于弹性搜索中的地理多边形内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆