# Geo spatial query - Using NEST (version 7) to Query WKT Field

**URL:** <https://discuss.elastic.co/t/geo-spatial-query-using-nest-version-7-to-query-wkt-field/196673>\
**Category:** Elasticsearch\
**Created:** [August 25, 2019, 6:09pm UTC](https://discuss.elastic.co/t/geo-spatial-query-using-nest-version-7-to-query-wkt-field/196673 "2019-08-25T18:09:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [August 25, 2019, 6:09pm UTC](https://discuss.elastic.co/t/geo-spatial-query-using-nest-version-7-to-query-wkt-field/196673/1 "2019-08-25T18:09:52Z")

</div>

We have an index with a GeoShape field that contains boundaries of counties in WKT format such as MULTIPOLYGON (((-9.50189821688122 51.6790656404768, -9.52975113548365 51.6846028881403, -9.49436480898325 51.7002020861983, -9.501898216881 etc. etc.

We also have an index that has properties each with a Lat/Lng co-ordinates in another index.

How can we find which properties are within a particular county using the NEST syntax

Many thanks

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [August 26, 2019, 12:44am UTC](https://discuss.elastic.co/t/geo-spatial-query-using-nest-version-7-to-query-wkt-field/196673/2 "2019-08-26T00:44:31Z")

</div>

Here's an example with Elasticsearch 7.3.0 and NEST 7.1.0

```auto
private static void Main()
{
	var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));	
    var settings = new ConnectionSettings(pool);

    var client = new ElasticClient(settings);
	
	var shapeIndex = "shapes";
	var pointsIndex = "points";

    var createIndexResponse = client.Indices.Create(shapeIndex, c => c
		.Map(m => m
			.Properties(p => p
				.GeoShape(g => g
					.Name("geo_shape")
				)
			)
		)
	);
	
	var indexResponse = client.Index(new {
		geo_shape = @"POLYGON(
			(150.6190465104703 -33.56599572510962,
			 150.71655017257967 -33.500746335741475,
			 150.83190661789217 -33.49960117013259,
			 150.93627673507967 -33.52937055164989,
			 150.91842395187655 -33.64491590448187,
			 150.9815953385953 -33.71462572482956,
			 151.06673938156405 -33.708913931968546,
			 151.10107165695467 -33.72947460810994,
			 151.19170886398592 -33.70662910845036,
			 151.26998645187655 -33.664348911935804,
			 151.2837193620328 -33.64377264974756,
			 151.3056920182828 -33.659776834387586,
			 151.31805163742342 -33.62090436726444,
			 151.32629138351717 -33.57858204255641,
			 151.34689074875155 -33.63576944153703,
			 151.30294543625155 -33.72490599453071,
			 151.29882556320467 -33.79112708083764,
			 151.30706530929842 -33.83904833451899,
			 151.24664050461092 -34.00542268051012,
			 151.16561633468905 -34.08621324029038,
			 150.9870885026578 -34.07483894596873,
			 151.00494128586092 -33.96898487914849,
			 150.83602649093905 -33.94962094389403,
			 150.82641345382967 -33.81166804830615,
			 150.63689929367342 -33.783137595510226,
			 150.6574986589078 -33.633482673997584,
			 150.6190465104703 -33.56599572510962))"
	}, i => i.Index(shapeIndex).Id("sydney"));
	
	var shapeId = indexResponse.Id;

	createIndexResponse = client.Indices.Create(pointsIndex, c => c
		.Map(m => m
			.Properties(p => p
				.GeoShape(g => g
					.Name("geo_shape")
				)
			)
		)
	);
	
	indexResponse = client.Index(new {
		geo_shape = new PointGeoShape(new GeoCoordinate(-33.85705389650999, 151.21507894264175))
	}, i => i.Index(pointsIndex).Id("sydney_opera_house")
	);
	
	var refreshResponse = client.Indices.Refresh(new [] { shapeIndex, pointsIndex });
	
	var searchResponse = client.Search<dynamic>(s => s
		.Index(pointsIndex)
		.Query(q => q
			.GeoShape(g => g
				.Field("geo_shape")
				.Relation(GeoShapeRelation.Within)
				.IndexedShape(f => f
					.Id(shapeId)
					.Index(shapeIndex)
					.Path("geo_shape")
				)
			)
		)
	);
	
	foreach(var hit in searchResponse.Hits)
	{
		Console.WriteLine($"doc with id {hit.Id} found within shape with id {shapeId}");
	}
}

```

which prints

```auto
doc with id sydney_opera_house found within shape with id sydney

```

Your points will also need to be mapped as point geo\_shapes to perform this query. [Check out the .NET client documentation for an example.](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/geo-shape-query-usage.html#geo-shape-query-indexedshape)

---

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [August 26, 2019, 2:02am UTC](https://discuss.elastic.co/t/geo-spatial-query-using-nest-version-7-to-query-wkt-field/196673/3 "2019-08-26T02:02:37Z")

</div>

Many thanks for your reply.  
To save us re-indexing:  
1-Do we have to make our location (points i.e. lat/lng) as GeoShape?  
2-Is there anyway to keep points(lat/lng) as GeoPoints?

Many thanks

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [August 26, 2019, 2:40am UTC](https://discuss.elastic.co/t/geo-spatial-query-using-nest-version-7-to-query-wkt-field/196673/4 "2019-08-26T02:40:26Z")

</div>

> [@han1](#):
>
> 1-Do we have to make our location (points i.e. lat/lng) as GeoShape?

Yes, if you want to use them with a `geo_shape` query

> [@han1](#):
>
> 2-Is there anyway to keep points(lat/lng) as GeoPoints?

I would suggest keeping points as `geo_point` if you need to run queries and aggregations that accept `geo_point`, and also indexing as `geo_shape` for `geo_shape` queries.

If you have already indexed the data, you can

1. Add a new field to the mapping of type `geo_shape` e.g. field named `shape`

2. Run an `update_by_query` to index the `geo_point` data into the new `geo_shape` field, using a script. Something like

---

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [August 26, 2019, 2:43am UTC](https://discuss.elastic.co/t/geo-spatial-query-using-nest-version-7-to-query-wkt-field/196673/5 "2019-08-26T02:43:34Z")

</div>

Perfect.  
many thanks.

---

<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:** [September 23, 2019, 2:43am UTC](https://discuss.elastic.co/t/geo-spatial-query-using-nest-version-7-to-query-wkt-field/196673/6 "2019-09-23T02:43:39Z")

</div>

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