是的,过滤器可以用来解决这个问题。
与Elasticsearch的正常使用情况一样,我们将文档编入elasticsearch,然后对索引数据运行查询以检索匹配的/必需的文档。
但过滤器的工作方式不同。
在Percolators中,您注册了您的查询,然后通过注册查询渗透您的文档,并取回与您的文档相匹配的查询。
经历无数的谷歌搜索结果和许多博客后,我无法找到任何可以解释我如何使用percolator来解决这个问题的事情。
所以我用一个例子来解释这一点,以便其他面临同样问题的人可以从我的问题和我找到的解决方案中获得提示。我想如果有人可以改进我的答案或者可以分享一个更好的方法来做到这一点。
例如: -
首先我们需要创建一个索引。
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为 “新德里”
{
"took": 25,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"total": 1,
"matches": [
{
"_index": "city",
"_id": "delhi"
}
]
}
让我们运行另一个查询random lat-lon
GET /city/user/_percolate
{
"doc": {
"location" : {
"lat" : 18.539933,
"lon" : 45.331770
}
}
}
并且此查询将返回无匹配结果。
{
"took": 5,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"total": 0,
"matches": []
}
你是说你希望能够发布一个具有某个地理位置的查询,并获取一个返回值,指出它是否位于此前发布的多边形内? – GlenRSmith 2015-04-03 12:31:54
是的。 @GlenRSmith – 2015-04-03 13:09:15
然后,您需要调查渗流。 基本上,您会将实际查询编入索引器,就像多边形过滤器查询一样,然后您将文档发布到_percolate端点,并返回返回的结果(如您拥有地理坐标的文档)将会指出哪些过滤后的查询会因此而返回您的文档。 – GlenRSmith 2015-04-03 18:46:44