elasticsearch - 通过geo_point优先匹配结果,并在Elasticsearch中按最近位置排序

标签 elasticsearch

我有一个GET请求,它与查询字符串匹配。它在地址字符串中搜索,现在从索引中返回可靠且相关的结果。

现在,我想按距离结果进行优先排序,因此首先返回相关字符串,并由最接近geo_point参数进行排序。

sort置于同一级别,就在query参数实际上不返回按距离排序的匹配数据上。它返回奇怪的结果,这绝对不是我想要的。

这是我使用的映射:

{
   "location": {
      "properties": {
         "address": {
            "type": "string"
         },
         "gps": {
            "type": "geo_point"
         }
      }
   }
}

我现在正在执行的请求是:

GET / locations / location / _search
{        
   "query": {
       "match" : {
            "address" : {
                "query": "Churchill Av"                               
            }
        }      
   },
  "sort": [
    {
      "_geo_distance": {
        "gps": { 
          "lat": 51.358599, 
          "lon": 0.531964
        },
        "order":         "asc",
        "unit":          "km", 
        "distance_type": "plane" 
      }
    }
  ]   
}

我知道最好的方法是先通过匹配获取结果,然后再按距离对少数结果进行排序,因此地理距离的计算并不太昂贵。

我尝试了this question,但是没有帮助。

编辑:我需要提一下,我将geo_point数据存储在索引中,如下所示:
"_source": {
    "address" : "Abcdef, ghijk, lmnoprst"
    "gps": [
        51.50,
        1.25
    ]
} 

问题:如何设置地理距离排序/过滤器,以便将结果按匹配查询后的排序,并按最接近的geo_point参数排序?

最佳答案

编辑:

我意识到,由于 geo_point 数据存储为索引数组,这意味着值与[Lon, Lat]一样,而不是与我期望的[Lat, Lon]一样,因此无法在关联的 _geo_distance模式中搜索:

{
    "lat" : Lat,
    "lon" : Long
}

来自elasticsearch.co文件:

Please note that string geo-points are ordered as lat,lon, while array geo-points are ordered as the reverse: lon,lat.



因此,以这种方式正确的排序符号是
"_geo_distance": {
    "gps": [51.358599,0.531964],                      
}

要么
"_geo_distance": {            
    "gps": {
        "lat": 0.531964,
        "lon": 51.358599  
     }  
}                    

...因为我想像的那样,将geo_point数据存储为[LON,LAT],而不是[LAT,LON]。

现在可以正常工作了。现在的问题是,我应该在geo_point数组中以纬度/经度的相反顺序重新索引数据。

我希望这句话可以帮助其他人。

关于elasticsearch - 通过geo_point优先匹配结果,并在Elasticsearch中按最近位置排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40235448/

相关文章:

elasticsearch - 计算Elasticsearch中滑动时间窗口的变化百分比

elasticsearch - curl如何使用数据有效载荷执行获取请求?

python - 导入错误 : cannot import name 'Elasticsearch' from 'elasticsearch'

elasticsearch - 用elasticsearch搜索整个世界

elasticsearch - 我可以在没有 Elasticsearch 的情况下生存吗?

java - 如何在 Java 中使用 JSON 进行 Elasticsearch 查询?

elasticsearch - 模糊匹配查询未考虑空格

elasticsearch - Elastic Search批量索引因日期检测关闭而失败

elasticsearch - 为集群和嗅探正确设置 Elasticsearch publish_host 和 bind_host

search - ElasticSearch更精确地匹配