# GeoSpatial Query using NEST and ES 7.14

**URL:** <https://discuss.elastic.co/t/geospatial-query-using-nest-and-es-7-14/300206>\
**Category:** Elasticsearch\
**Created:** [March 21, 2022, 12:54pm UTC](https://discuss.elastic.co/t/geospatial-query-using-nest-and-es-7-14/300206 "2022-03-21T12:54:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xef](https://avatars.discourse-cdn.com/v4/letter/x/898d66/32.png) [@xef](https://discuss.elastic.co/u/xef)\
**Post date:** [March 21, 2022, 12:54pm UTC](https://discuss.elastic.co/t/geospatial-query-using-nest-and-es-7-14/300206/1 "2022-03-21T12:54:41Z")

</div>

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.  
Thanks

---

<div class="post-metadata">

**Author:** ![jsanz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsanz/32/53734_2.png) [@jsanz](https://discuss.elastic.co/u/jsanz)\
**Post date:** [March 22, 2022, 12:19pm UTC](https://discuss.elastic.co/t/geospatial-query-using-nest-and-es-7-14/300206/2 "2022-03-22T12:19:53Z")

</div>

`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](https://www.elastic.co/guide/en/elasticsearch/reference/8.1/query-dsl-script-query.html).

The script would be something like this assuming `indexB` has a `my_geom_field` of geo\_point type, and a `radius` field.

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

def lat1 = params.lat;
def lon1 = params.lon;
def lat2 = dest.lat;
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) * 
            Math.sin(lonDistance/2.0);

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

emit(R * c < radius) 

```

---

<div class="post-metadata">

**Author:** ![xef](https://avatars.discourse-cdn.com/v4/letter/x/898d66/32.png) [@xef](https://discuss.elastic.co/u/xef)\
**Post date:** [March 22, 2022, 2:54pm UTC](https://discuss.elastic.co/t/geospatial-query-using-nest-and-es-7-14/300206/3 "2022-03-22T14:54:48Z")

</div>

Hi  
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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 19, 2022, 2:55pm UTC](https://discuss.elastic.co/t/geospatial-query-using-nest-and-es-7-14/300206/4 "2022-04-19T14:55:21Z")

</div>

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