【问题标题】:Return distance in elasticsearch results?在弹性搜索结果中返回距离?
【发布时间】:2012-03-06 21:31:50
【问题描述】:

我的问题与one类似。

简单地说,有没有办法在不使用_geo_distance排序时返回地理距离?

更新: 澄清一下,我希望结果以随机顺序并包括距离。

【问题讨论】:

    标签: geolocation elasticsearch


    【解决方案1】:

    DrTech 给出了很好的答案……这里是 Elasticsearch 5.x 的更新版本,脚本语言为无痛。我还添加了“store_fields”以在结果中包含_source

    curl -XGET 'http://127.0.0.1:9200/geonames/_search?pretty=1'  -d '
    {
      "stored_fields" : [ "_source" ],
      "script_fields" : {
        "distance" : {
          "script" : {
            "inline": "doc['location'].arcDistance(params.lat,params.lon) * 0.001",
            "lang": "painless",
            "params": {
              "lat": 2.27,
              "lon": 50.3
            }
          }
        }
      }
    }'
    

    【讨论】:

    • 有什么理由可以为doc['venue.coordinates'] 提供Variable [venue] is not defined. 吗? Venue.coordinates 是对象场地中的一个字段,所以我不确定缺少什么。
    • 这也适用于 6.x。您可以省略 "lang": "painless",因为它是默认设置。
    • 相同的代码适用于 7.9 版本 "stored_fields" : [ "_source" ], "script_fields": { "distance": { "script": { "source": "doc['pin .location'].arcDistance(28,77);" } } }
    【解决方案2】:

    要像所有默认字段/源一样返回距离,您也可以这样做:

    为了避免它(主要)按距离排序,您只需先按 _score(或任何您想要的结果排序依据)排序。

    {
       "sort": [
        "_score",
        {
          "_geo_distance": {
            "location": { 
              "lat":  40.715,
              "lon": -73.998
            },
            "order":         "asc",
            "unit":          "km", 
            "distance_type": "plane" 
          }
        }
      ]
    }
    

    【讨论】:

    • 这个解决方案也可以在禁用脚本支持的情况下工作,就像在许多托管服务器上一样。谢谢!
    【解决方案3】:

    由于 ES 1.3 默认禁用 MVEL,因此请使用如下查询:

    GET some-index/_search
    {
      "sort": [
        {
          "_geo_distance": {
            "geo_location": "47.1, 8.1",
            "order": "asc",
            "unit": "m"
          }
        }
      ],
      "query": {
        "match_all": {}
      },
       "script_fields" : {
          "distance" : {
             "lang": "groovy",
             "params" : {
                "lat" : 47.1,
                "lon" : 8.1
             },
             "script" : "doc[\u0027geo_location\u0027].distanceInKm(lat,lon)"
          }
       }
    }
    

    见:"lang": "groovy", 部分

    【讨论】:

    • 你能告诉我你是如何启用脚本的吗我的意思是你在你的 yml 文件中写了什么或者任何东西请告诉我我正在搜索这个很长时间??
    • 能否设置为源字段之一?
    【解决方案4】:

    是的,您可以使用script field

    例如,假设您的文档有一个名为 location 的地理点字段,您可以使用以下内容:

    (注意\u0027 只是一个转义的单引号,所以\u0027location\u0027 真的是'location'

    curl -XGET 'http://127.0.0.1:9200/geonames/_search?pretty=1'  -d '
    {
       "script_fields" : {
          "distance" : {
             "params" : {
                "lat" : 2.27,
                "lon" : 50.3
             },
             "script" : "doc[\u0027location\u0027].distanceInKm(lat,lon)"
          }
       }
    }
    '
    
    # [Thu Feb 16 11:20:29 2012] Response:
    # {
    #    "hits" : {
    #       "hits" : [
    #          {
    #             "_score" : 1,
    #             "fields" : {
    #                "distance" : 466.844095463887
    #             },
    #             "_index" : "geonames_1318324623",
    #             "_id" : "6436641_en",
    #             "_type" : "place"
    #          },
    ... etc
    

    如果您还希望返回 _source 字段,则可以指定如下:

    curl -XGET 'http://127.0.0.1:9200/geonames/_search?pretty=1'  -d '
    {
       "fields" : [ "_source" ],
       "script_fields" : {
          "distance" : {
             "params" : {
                "lat" : 2.27,
                "lon" : 50.3
             },
             "script" : "doc[\u0027location\u0027].distanceInKm(lat,lon)"
          }
       }
    }
    '
    

    【讨论】:

    • 但是,我无法同时返回 _source 和 distance。如果我将 _source 指定为“字段”,我只会得到 _source,如果我不这样做,我只会得到距离。有什么想法吗?
    • 你用的是什么版本的ES?当我写这个答案时,有一个新引入的错误意味着源没有被返回。但是,这已在最新的 RC 中修复
    • 哦,这可能就是为什么,我在 0.17.5。
    • 是否可以在对象字段上运行 distanceInKm?例如:place.point,我也尝试过 _source.place.point,因为我从文档中得知这应该是可能的?其中 place 是一个嵌套对象。非常感谢。
    • 在 elastic 5.x,6.x 中应该使用arcDistance(lat,lon)planeDistance(lat,lon) 方法。
    猜你喜欢
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 2019-07-20
    相关资源
    最近更新 更多