
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 子句,求解惑!!
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 的问题. |
2 EPr2hh6LADQWqRVH 2015-08-31 19:55:25 +08:00 via iPhone es 对地理坐标有专门优化,看下文档。 看来不是任何时候 Msql 都是最成熟稳定对吧 |
3 sunchen 2015-09-01 03:28:50 +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 计算 然后排序 |