我正在使用ElasticSearch版本5.5.1和NEST 6.2。
我正在使用ScriptField计算距离并将其包含在搜索结果中。以前,此解决方案可以与NEST的较旧版本配合使用。我不确定自己在做错什么,将不胜感激。
List<Item> results = new List<Item>();
var rad = new Distance(radius, Nest.DistanceUnit.Kilometers);
int c = 0;
var geoResult = await _client.SearchAsync<Item>(s => s
.From(0)
.Size(size)
.StoredFields(sf => sf.Field("_source"))
.ScriptFields(sf => sf
.ScriptField("distance", sc => sc
.Source("doc['location'].arcDistance(params.lat, params.lon) * 0.001")
.Lang("painless")
.Params(f => f.Add("lat", lat).Add("lon", lng))))
.Query(q => q
.Bool(b => b
.Filter(filter => filter
.GeoDistance(geo => geo
.Field(f => f.Location)
.Distance(rad).Location(lat, lng)
.DistanceType(GeoDistanceType.Arc)))))
.Sort(sort => sort
.GeoDistance(g => g
.Field(f => f.Location)
.Order(Nest.SortOrder.Ascending)
.Points(new GeoLocation(lat, lng))
.DistanceType(GeoDistanceType.Arc))));
foreach (var item in geoResult.Documents)
{
Item item = new Item
{
Name = item.Name,
Distance = (geoResult.Fields.ToArray())[c++].Values<Item, double>(p => p.Distance)[0],
Latitude = item.Location.Latitude,
Longitude = item.Location.Longitude
};
results.Add(item);
}
return results;