sqlcte
sqlcte

Reputation: 425

Ravendb Spatial search with ORderBydistance

I need to get the nearest 15 locations from a point.I have first created a Spatial Index and then Im trying to order the result by distance by calling OrderByDistance method but Im getting this exception

Cannot execute query method 'OrderByDistance'. Field 'spatial.point(Latitude, Longitude)' cannot be used when static index 'SupplierCoordinatesLookup' is queried. Dynamic spatial fields can only be used with dynamic queries, for static index queries please use valid spatial fields defined in index definition.

This is my query

List<SupplierCoordinates> suplierCoordinates = _documentSession
           .Query<SupplierCoordinates, SupplierCoordinatesLookup>()
            .Spatial(
                "Coordinates",
                criteria => criteria.WithinRadius(0, supplierCoordinates.Latitude, supplierCoordinates.Longitude))
            .OrderByDistance(factory => factory.Point(x => x.Latitude, x => x.Longitude), supplierCoordinates.Latitude, supplierCoordinates.Longitude)
            .Take(15)
            .ToList();

and here is the code for my index

Map = collection => from doc in collection
                            select new
                            {
                                Coordinates = CreateSpatialField(doc.Latitude, doc.Longitude)
                            };
        Store(x => x.Id, FieldStorage.Yes);

Upvotes: 1

Views: 74

Answers (1)

garay
garay

Reputation: 666

Try to use:

    _documentSession
           .Query<SupplierCoordinates, SupplierCoordinatesLookup>()
            .OrderByDistance("Coordinates", supplierCoordinates.Latitude, supplierCoordinates.Longitude)
            .Take(15)
            .ToList();

Your index already contains points, so you only have to supply a field from index ("Coordinates") to your query

Upvotes: 1

Related Questions