如何从 elasticsearch.net / NEST 获取 geo_point 字段的距离

How to get distance from elasticsearch.net / NEST for a geo_point field

我想在搜索请求中获取 geo_point..

的距离

我已经写了这个请求,它给我最接近我的搜索参数的点。

ConnectionSettings elasticSettings = new ConnectionSettings(new Uri("http://localhost:9200"));
        ElasticClient client = new ElasticClient(elasticSettings);

        var searchResults = client.Search<dynamic>(s => s.Index("index1,index2,index3").From(0).Size(10).Query(
           q => q.Bool(
           b => b.Must(
                    f => f.GeoDistance(
                     g => g.Distance(20, DistanceUnit.Kilometers).DistanceType(GeoDistanceType.Arc).Field("geo").Location(lat, lon))))));

我尝试了很多在网上找到的代码,但我无法将其改编为我的代码.. 我只想要 elasticsearch return 我每个点的距离。

我在elasticsearch中的字段是这样的(简单的字符串):

    geo 74.875,-179.875 

在另一个索引测试中,是这样的(结构化的):搜索不是这样工作的

geo {
"lat": 74.875,
"lon": -178.625
}

第一个或第二个映射会对查询产生影响吗?

这是我的索引映射:

{
"index1": {
"aliases": {},
"mappings": {
  "properties": {
    "Date": { "type": "date" },
    "Value": { "type": "text" },
    "geo": { "type": "geo_point" }
  }
},
"settings": {
  "index": {
    "refresh_interval": "1s",
    "number_of_shards": "4",
    "provided_name": "index1",
    "creation_date": "1569420798736",
    "number_of_replicas": "0",
    "uuid": "jqc1RRhxSC2e5yJJX2lyzw",
    "version": { "created": "7030199" }
  }
}

} }

我像这样在我的查询中集成了一个 scripfield :

  var searchResults = client.Search<dynamic>(s => s.Index("index").From(0).Size(100).ScriptFields(sf => sf.ScriptField("distance", d => d.Source("if(doc['geo'].size(){doc['geo'].arcDistance("+ lat+","+ lon + ")}"))).Query(
       q => q.Bool(
       b => b.Must(
                f => f.GeoDistance(
                 g => g.Distance(20, DistanceUnit.Kilometers).DistanceType(GeoDistanceType.Arc).Field("geo").Location(lat, lon))))));

对于这个请求,我得到了“200 次成功”的响应,似乎我 return 是距离而不是其他字段,并且 100 个文档是空的。

    Valid NEST response built from a successful (200) low level call on 
POST: /index1/_search?typed_keys=true
# Audit trail of this API call:
 - [1] HealthyResponse: Node: http://localhost:9200/ Took: 
00:00:01.0670113
# Request:
{"from":0,"query":{"bool":{"must":[{"geo_distance": 
{"distance":"200km","distance_type":"arc","geo": 
{"lat":57.123,"lon":-20.876}}}]}},"script_fields":{"distance":{"script": 
{"source":"doc['geo'].arcDistance(57.123,-20.876)"}}},"size":100}
# Response:
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 4,
    "successful": 4,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
     "value": 1203,
      "relation": "eq"
    },
    "max_score": 1.0,
     "hits": [
      {
        "_index": "index1",
        "_type": "_doc",
        "_id": "121197",
        "_score": 1.0,
        "fields": { "distance": [ 198251.11868760435 ] }
       },
      {
         "_index": "index1",
         "_type": "_doc",
        "_id": "121198",
        "_score": 1.0,
        "fields": { "distance": [ 197018.831847128 ] }
      },
       ...98 more
    ]
  }
}

谢谢。

您需要使用脚本字段return距离

"script_fields":{
     "distance":{
            "script":"doc['latlng'].arcDistance(params.lat,params.lng)",
            "params":{
                    "lat":<some value>,
                    "lng":<some value>
             }
      }
}

鸟巢

 var scriptFields = new ScriptFields
            {
               {
                   "distance", new ScriptField {
                       Script = new InlineScript( "if(doc['"+field+"'].size() > 0) { doc['"+field+"'].arcDistance(params.lat,params.lon) }")
                       {
                           Params=new FluentDictionary<string, object>
                           {
                              { "lat", latitude},
                              { "lon", longitude}
                           }
                       }
                   }
                }
            };