V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tumayun
V2EX  ›  Elasticsearch

Elasticsearch 如何用 script_fields 计算出来的字段进行排序?

  •  
  •   tumayun · 2015-08-31 14:39:09 +08:00 · 14415 次点击
    这是一个创建于 3133 天前的主题,其中的信息可能已经有所发展或是发生改变。

    示例如下:

    curl -X GET 'http://localhost:9200/users/user/_search?pretty' -d '{
      "script_fields":{
        "distance":{
          "params":{
            "lat":23.1191,
            "lon":113.31
          },
          "script":"doc[\"location\"].distanceInKm (lat, lon )",
          "lang":"groovy"
        }
      }
    }
    '
    

    返回如下结果:

    # 2015-08-31T14:29:46+08:00 [200] (0.005s )
    #
    # {
    #   "took":2,
    #   "timed_out":false,
    #   "_shards":{
    #     "total":5,
    #     "successful":5,
    #     "failed":0
    #   },
    #   "hits":{
    #     "total":11,
    #     "max_score":1.0,
    #     "hits":[
    #       {
    #         "_index":"users",
    #         "_type":"user",
    #         "_id":"4",
    #         "_score":1.0,
    #         "fields":{
    #           "distance":[
    #             12873.486133286819
    #           ]
    #
    # }
    #       },
    #      ...
    

    请问如何用 fields.distance 进行排序?

    在 sort 中,不知道如何取到 distance 的值,所以也就无法写出 sort 子句,求解惑!!

    4 条回复    2016-06-28 09:22:18 +08:00
    tajpure
        1
    tajpure  
       2015-08-31 17:30:45 +08:00
    参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html


    我一般用 Client 做查询,没怎么用过 curl. 不过以上应该可以解决 LZ 的问题.
    EPr2hh6LADQWqRVH
        2
    EPr2hh6LADQWqRVH  
       2015-08-31 19:55:25 +08:00 via iPhone
    es 对地理坐标有专门优化,看下文档。
    看来不是任何时候 Msql 都是最成熟稳定对吧
    yn
        4
    yn  
       2016-06-28 09:22:18 +08:00
    curl -X GET 'http://localhost:9200/users/user/_search?pretty' -d '{
    "sort" : [ {
    "_geo_distance" : {
    "location" : [ {
    "lat" : 23.1191,
    "lon" : 113.31
    } ],
    "unit" : "km",
    "distance_type" : "arc"
    }
    }]
    }'

    使用这个相当于 利用 script_fields 的 distanceInKm 计算 然后排序
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2930 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 14:03 · PVG 22:03 · LAX 07:03 · JFK 10:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.