基於Elasticsearch進行地理檢索,計算距離值


 

實現步驟:
1、定義屬性
    [Serializable]
    public class Coordinate
    {
        public double Lat { get; set; }
 
        public double Lon { get; set; }
    }
 
        /// <summary>
        /// 位置坐標
        /// </summary>
        [ElasticProperty(Type = FieldType.GeoPoint)]
        public Coordinate location { get; set; }
 
        /// <summary>
        /// 返回字段,距離值,適用於地理位置檢索和排序  2016-03-25
        /// </summary>
        public double distance { get; set; }
 

 
2、創建索引時,設置Mapping:
_clientES.CreateIndex(indexName, s => s.AddMapping<DTOCarInfoIndexField>(f => f .MapFromAttributes().Properties(p => p.GeoPoint(g => g.Name(n => n.location).IndexLatLon()).String(fullinfo => fullinfo.Name(n => n.fullinfo).Index(FieldIndexOption.Analyzed).Analyzer("ik").Store(false)).String(description => description.Name(n => n.statedescription).Index(FieldIndexOption.Analyzed).Analyzer("ik").Store(false))).AllField(af => af.Enabled(false))).NumberOfReplicas(0));
 

 
3、向ES寫入數據時,給location屬性賦值:
 
#region 車源坐標  2015-11-30
 
string[] arrValue = ConvertHelper.ToStringArray(value.ToString(), ',');
if (arrValue.Length == 2)
{
    var point = new Coordinate();
    point.Lat = 0.0;
    point.Lon = 0.0;
    double x = ConvertHelper.ToDouble(arrValue[0]);
    double y = ConvertHelper.ToDouble(arrValue[1]);
    if (x > 0 && y > 0)
    {
        point.Lat = x;
        point.Lon = y;
    }
    prop.SetValue(obj, point, null);
}
 
#endregion
 

 
4、搜索過程中按照距離排序並返回距離值(單位:km)(Nest組件):
//構建排序對象
List<KeyValuePair<PropertyPathMarker, ISort>> oneSortList = new List<KeyValuePair<PropertyPathMarker, ISort>>();
SortOrder ordertype = SortOrder.Ascending;
GeoDistanceSort sort = new GeoDistanceSort();
sort.Field = "location";
sort.Order = ordertype;
sort.GeoUnit = GeoUnit.Kilometers;
sort.PinLocation = locationpoint;
oneSortList.Add(new KeyValuePair<PropertyPathMarker, ISort>("_geo_distance", sort));
 
//構建ES檢索對象
string[] returnFields=new[]{"id","distance"};
var searchRequest = new SearchRequest();
searchRequest.From = 0;
searchRequest.Size = 20;
searchRequest.Sort = oneSortList;
 
//定義返回列屬性
searchRequest.Fields = returnFields.Select(f => (PropertyPathMarker)f.ToLower()).ToList();
 
//添加其他檢索條件
//searchRequest.Query = queryList[i];
//searchRequest.Filter = listFilter[i];
 
 
#region 地理檢索,添加距離返回值字段
 
if (!string.IsNullOrEmpty(locationPoint) && returnFields.Contains("distance"))
{
    var distancefield = new Dictionary<string, IScriptFilter>();
    var tempfield = new ScriptFilter();
    tempfield.Params = new Dictionary<string, object>();
    tempfield.Params.Add("lat", 116.403951);
    tempfield.Params.Add("lon", 39.915031);
    tempfield.Script = "doc['location'].arcDistanceInKm(lat,lon)";
    distancefield.Add("distance", tempfield);
    searchRequest.ScriptFields = distancefield;
}
 
#endregion
 
//執行檢索,獲取返回值
var resultSearch = esClient.Search<T>(searchRequest);
foreach (var doc in sResponse.FieldSelections)
{
    var id = doc.FieldValues<object[]>("id").ToList()[0];
    var distancevalue = doc.FieldValues<object[]>("distance").ToList()[0]; 
}
 
 
5、搜索過程中按照距離排序並返回距離值(單位:km)(Linq方式)
ESClient.Instance_TaocheCar().GetElasticClient().Search<DTOCarInfoIndexField>(s => s.From(0).Size(200).Fields(arrField).Filter(filter => filter.Terms("userid", list)).ScriptFields(sf => sf.Add("distance", descriptor => descriptor.Params(p => p.Add("lat", lat).Add("lon", lon)).Script("doc['location'].arcDistanceInKm(lat,lon)"))).SortGeoDistance(sg => sg.OnField("location").PinTo(lat, lon).Unit(GeoUnit.Kilometers).Ascending()));


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM