GeoSpatial Query using NEST and ES 7.14

We are using ES 17.14 and have looked at the docs but have been unable to create a query and wondered if anyone can shed some light on it.

1- We have an IndexA that has a FieldA of type Geoshape with its latitude and longitude
2- We have another IndexB that has a FieldB of type Number(integer) that represents the radius of an imaginary circle in meters.

What we would like to do is that for a given record in IndexA to find all records in IndexB that the FieldA in IndexA falls within that imaginary circle represented by the FieldB radius value.

IndexB also has a geo_shape field, right? Could you provide a few documents from each index and a sample query with expected result? Just to clarify what you want to achieve.

ES queries can't perform joins, but if you provide the coordinates from your document in indexA you can dynamically compute the distance between that point and each document from indexB and filter those where the radius field is bigger than the distance. You can do this with a script query.

The script would be something like this assuming indexB has a my_geom_field of geo_point type, and a radius field.

def dest = doc['my_geom_field'];
def radius = doc['radius'];

def lat1 =;
def lon1 = params.lon;
def lat2 =;
def lon2 = dest.lon;

double R = 6371000;

double latDistance = (lat2-lat1) * Math.PI / 180;
double lonDistance = (lon2-lon1) * Math.PI / 180;
double latDist2 = latDistance / 2.0;

double a = Math.sin(latDist2) * Math.sin(latDist2) + 
            Math.cos(lat1 * Math.PI / 180) * 
            Math.cos(lat2 * Math.PI / 180) * 
            Math.sin(lonDistance/2.0) * 

double c = 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

emit(R * c < radius) 

Thanks for your response.
We decided that using this technique is too much computation per query. So we decided to actually create a geometry in IndexB and use the patern:

filter.Add(fq => fq.GeoShape(g => g.Field("FieldA in Index A").Relation(GeoShapeRelation.Within).IndexedShape(f => f.Id(Id of record in Index A).Index("IndexB").Path("geometry in IndexB"))));
This pattern uses both indicies whithout requiring any joins.

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