Single query to sort documents by geodistance based on another documents id

I have an index in elasticsearch which contains a Id field and a geopoint.

right now in order to get the nearest documents I have to make two queries, one to get the original document by its id and after that use its coordinates to do a geosort. I was wondering if there is anyway to execute this as a single query.

public IEnumerable<RestaurantSearchItem> GetNearbyRestaurants(double latitude, double longitude)
        {
            var query = _elasticClient.Search<RestaurantSearchItem>(s =>
                s.Index(RestaurantSearchItem.IndexName)
                .Sort(
                    ss =>ss.GeoDistance(
                        g => g
                            .Field(p => p.Location)
                            .DistanceType(GeoDistanceType.Plane)
                            .Unit(DistanceUnit.Meters)
                            .Order(SortOrder.Ascending)
                            .Points(new GeoLocation(latitude,longitude)))));

            var nearByRestaurants = query.Documents;

            foreach (var restaurant in nearByRestaurants)
            {
                restaurant.Distance = Convert.ToDouble(query.Hits.Single(x => x.Id == restaurant.Id).Sorts.Single());
            }

            return nearByRestaurants;
        }

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.