Search code examples
elasticsearchnestgeo

NEST 2.0 with Elasticsearch for GeoDistance always returns all records


I have the below code using C# .NET 4.5 and NEST 2.0 via nuget. This query always returns my type 'trackpointes' with the total number of documents with this distance search code. I have 2,790 documents and the count return is just that. Even for 1 centimeter as the distance unit it returns all 2,790 documents. My type of 'trackpointes' has a location field, type of geo_point, geohash true, and geohash_precision of 9.

I am just trying to filter results based on distance without any other search terms and for my 2,790 records it returns them all regardless of the unit of measurement. So I have to be missing something (hopefully small). Any help is appreciated. The NEST examples I can find are a year or two old and that syntax does not seem to work any more.

double distance = 4.0;
var geoResult = client.Search<TrackPointES>(s => s.From(0).Size(10000).Type("trackpointes")
    .Query(query => query
    .Bool( b => b.Filter(filter => filter
                .GeoDistance(geo => geo
                .Distance(distance, Nest.DistanceUnit.Kilometers).Location(35, -82)))
             )
           )
       );

If I use POSTMAN to connect to my instance of ES and POST a search w/ the below JSON, I get a return of 143 total documents out of 2,790. So I know the data is right as that is a realistic return.

{
"query" : {
    "filtered" : {
        "filter" : {
            "geo_distance" : {
                "distance" : "4km",
                "location" : {
                    "top_left": {
                        "lat" : 35,
                        "lon" : -82
                    }
                }
            }
        }
    }
  }
}

Solution

  • Looks like you didn't specify field in your query. Try this one:

    var geoResult = client.Search<Document>(s => s.From(0).Size(10000)
        .Query(query => query
            .Bool(b => b.Filter(filter => filter
                .GeoDistance(geo => geo
                    .Field(f => f.Location) //<- this 
                    .Distance(distance, Nest.DistanceUnit.Kilometers).Location(35, -82)))
            )
        )
        );