# NEST Geo shape filter to shapes containing a point

**URL:** <https://discuss.elastic.co/t/nest-geo-shape-filter-to-shapes-containing-a-point/285414>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 28, 2021, 10:20pm UTC](https://discuss.elastic.co/t/nest-geo-shape-filter-to-shapes-containing-a-point/285414 "2021-09-28T22:20:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chm/32/78258_2.png) [@chm](https://discuss.elastic.co/u/chm)\
**Post date:** [September 28, 2021, 10:20pm UTC](https://discuss.elastic.co/t/nest-geo-shape-filter-to-shapes-containing-a-point/285414/1 "2021-09-28T22:20:08Z")

</div>

Hi,

I'm trying to filter based on location with NEST using the docs [here](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/geo-shape-query-usage.html) and post [here](https://discuss.elastic.co/t/geo-spatial-query-using-nest-version-7-to-query-wkt-field/196673). But I think my use case is different to those examples in two ways.

1. I want to filter based on a point being Within a polygon/shape, but I want to filter the document associated with the shapes, not the point. As in "return only documents with a shape surrounding the point value given in the query".
2. The documents with shapes can have a list of multiple shapes, and I want to filter out only those that have NO shape surrounding the point.

In general my setup is:

- A PointDocument index, each PointDocument has a lat/long point field.
- A ShapeDocument index, each ShapeDocument has a field with a list of shapes.
- We need to query the ShapeDocuments (based on other properties). For each query we have the ID of a PointDocument the results are for, and we have to filter the ShapeDocument search results to only those that have a shape surrounding the PointDocument's point field.

Is that possible? In NEST or in general?

What I'm trying at the moment is to use Intersects with a Polygon shape but I'm not even able to index the ShapeDocuments. What I have so far:

```auto
			var shapeDocument = new ShapeDocument 
			{
                // "shapes" is a List<List<GeoCoordinate>>
				Shapes = new PolygonGeoShape(shapes)
			};

```

I can successfully index documents with no GeoCoordinates and they look like:

```auto
 {
                "_index": "ShapeDocumentIndex",
                "_type": "_doc",
                "_id": "someid",
                "_routing": "somerouting",
                "_source": {
                    [other fields]
                    "shapes": {
                        "type": "polygon",
                        "coordinates": []
                    }
                }

```

The error I get is:

```auto
Type: mapper_parsing_exception Reason: "object mapping for [shapeDocument.coordinates] tried to parse field [null] as object, but found a concrete value"

```

I've looked at a few posts around with that error message but I couldn't find any where the field being parsed is as "null", and I don't see why the PolygonGeoShape would be a concrete value?

Thanks very much for your help.

---

<div class="post-metadata">

**Author:** ![chm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chm/32/78258_2.png) [@chm](https://discuss.elastic.co/u/chm)\
**Post date:** [October 6, 2021, 11:05pm UTC](https://discuss.elastic.co/t/nest-geo-shape-filter-to-shapes-containing-a-point/285414/2 "2021-10-06T23:05:27Z")

</div>

I never figured out what that error message meant, but some of this is working now

1. Filtering the document with the shapes was using the "Contains" relation, not "Within".
2. I haven't tried handling multiple shapes yet.

Changes this needed:

Mapping the geoshape property when creating the index

```auto
var result = await client.Indices.CreateAsync(indexDefinition.Name,
				x => x
					.Settings(settings => settings
						// etc, all the settings here...
					.Map<ShapeDocument>(doc => doc
						.Properties(it => it.GeoShape(g => g.Name("shapes")))
						.AutoMap()));

```

Indexing the shape

```auto
            // assigned elsewhere
            IReadOnlyCollection<List<NetTopologySuite.Geometries.Coordinate>> shapes; 
            var doc = new ShapeDocument
			{
				Id = someId,
				Shapes = new PolygonGeoShape(
					shapes.Select(s => s.Select(point => new Nest.GeoCoordinate(point.Y, 
                                        point.X))))
			};

            var descriptor = new BulkDescriptor();
    		descriptor.Index<DocumentBase>(
						o => o.Document(doc)
							.Id(doc.Id)
							.Index(index))
					.Refresh(Refresh.True);
            var response = await _elasticClient.BulkAsync(descriptor);

```

Making a filter to query with

```auto
            NotTopologySuite.Geometries.Point point = // get from the database
			QueryContainer pointWithinShapes = new GeoShapeQuery
			{
				Field = Field<ShapeDocument>(it => it.Shapes),
				Shape = new PointGeoShape(point.Y, point.X),
				Relation = GeoShapeRelation.Contains
			};

            // if the doc has no shapes, it doesn't need to be filtered out
			QueryContainer documentHasNoShapes = new BoolQuery
			{
				MustNot = new QueryContainer[]
				{
					new ExistsQuery
					{
						Field = Field<ShapeDocument>(it => it.Shapes)
					}
				}
			};

           // say that either match is ok but neither should affect the score
			QueryContainer shouldFilter = new BoolQuery
			{
				Should = new[] { pointWithinShapes , documentHasNoShapes },
				MinimumShouldMatch = 1
             };
		}

```

---

<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:** [November 3, 2021, 11:06pm UTC](https://discuss.elastic.co/t/nest-geo-shape-filter-to-shapes-containing-a-point/285414/3 "2021-11-03T23:06:16Z")

</div>

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