简体   繁体   中英

C# calculate geodistance with elastic search (nest 2)

I'm implementing a program with the ability to receive the 10 nearest object. As database I use ElasticSearch, my model looks like this.

[Nest.ElasticsearchType(Name = "eventelastic", IdProperty = "Id")]
public class EventElastic:BaseElastic
{
    [Nest.String]
    public string EventName { get; set; }
    [Nest.Date]
    public DateTime CreateTime { get; set; }
    [Nest.String]
    public string Country { get; set; }
    [Nest.String]
    public string City { get; set; }
    [Nest.String]
    public string Place { get; set; }
    [Nest.Boolean]
    public bool IsPrivate { get; set; }
    [Nest.Boolean]
    public bool IsSponsored { get; set; }
    [Nest.String]
    public string ImageWeb { get; set; }
    [Nest.Number]
    public int MaxPersons { get; set; }
    [Nest.GeoPoint]
    public LocationModel Location { get; set; }


}

[Nest.ElasticsearchType(Name = "location", IdProperty = "Id")]
public class LocationModel
{
    public LocationModel(double lon, double lat)
    {
        Lon = lon;
        Lat = lat;
    }

    public double Lon { get; set; }

    public double Lat { get; set; }
}

I created an index like this

connection.CreateIndex("index", s => s.Mappings(f => f.Map<EventElastic>(m => m.AutoMap().Properties(p=> p.GeoPoint(g => g.Name(n => n.Location))))));

And I insert new documents like this

var response = connection.IndexAsync<EventElastic>(model, idx => idx.Index("index"));
            response.ContinueWith(t =>
            {
                if (!t.Result.IsValid)
                {
                    log.Error(t.Result.ServerError.Error.Reason);
                }
            });

All values of the model are set, and it works without problems.

But now i want to get the 10 nearest in a range of 10 km. I query this like this

var geoResult = connection.Search<EventElastic>(s => s.From(0).Size(10)
            .Query(query => query.Bool(b => b.Filter(filter => filter
                    .GeoDistance(geo => geo
                        .Field(f => f.Location) //<- this 
                        .Distance("10km").Location(lon, lat)
                        .DistanceType(GeoDistanceType.SloppyArc)
                        ))
                )
            )
        );

but it doesnt return any document but it returns a document if i set the distance value to 10000km.

The data i used in the model is: LAT: 47.4595248 LON: 9.6385962

The location i use in the search: LAT: 47.4640298 LON: 9.6389685

Those places are 100 meter away from each other. Can someone help me to find the mistake in my code? And is it possible to get the calculated distance from the elasticsearch server?

btw: i'm setting a default index

var settings = new ConnectionSettings(pool).DefaultIndex("index");

Edit I found the mistake:

var geoResult = connection.Search<EventElastic>(s => s.From(0).Size(10)
        .Query(query => query.Bool(b => b.Filter(filter => filter
                .GeoDistance(geo => geo
                    .Field(f => f.Location) 
                    .Distance("10km").Location(lat, lon) //wrong parameter
                    .DistanceType(GeoDistanceType.SloppyArc)
                    ))
            )
        )
    );

i had to change position of lat and lon.

But i do not get the calculated distance, do somebody know how i can get the distance?

best regards

You can add Distance to your document and use sctipt fields with your query to update this value.

var geoResult = client.Search<EventElastic>(s => s.From(0).Size(10)
    .ScriptFields(sf => sf
        .ScriptField("distance", descriptor => descriptor
            .Inline("doc[\u0027location\u0027].distanceInKm(lat,lon)")
            .Lang("groovy")
            .Params(f => f.Add("lat", lat).Add("lon", lon))))
    .Query(query => query.Bool(b => b.Filter(filter => filter
        .GeoDistance(geo => geo
            .Field(f => f.Location)
            .Distance("10km").Location(lat, lon)
            .DistanceType(GeoDistanceType.SloppyArc)
        ))
        )
    )
    .Sort(sort => sort
        .GeoDistance(g => g
            .Field(f => f.Location)
            .Order(SortOrder.Ascending)
            .PinTo(new GeoLocation(1.5, 1))
            .DistanceType(GeoDistanceType.SloppyArc)))
    );


public class EventElastic
{
    ..
    public double Distance {get;set;}
}

Response from elasticsearch:

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": null,
      "hits": [
         {
            "_index": "my_index",
            "_type": "data",
            "_id": "1",
            "_score": null,
            "fields": {
               "distance": [
                  55.65975203179589
               ]
            },
            "sort": [
               55659.66677843493
            ]
         },
         {
            "_index": "my_index",
            "_type": "data",
            "_id": "2",
            "_score": null,
            "fields": {
               "distance": [
                  124.45896068602408
               ]
            },
            "sort": [
               124411.81715215484
            ]
         }
      ]
   }
}

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM