# Neighbouring Geo Shapes / Polygons

**URL:** <https://discuss.elastic.co/t/neighbouring-geo-shapes-polygons/32656>\
**Category:** Elasticsearch\
**Created:** [October 21, 2015, 10:39am UTC](https://discuss.elastic.co/t/neighbouring-geo-shapes-polygons/32656 "2015-10-21T10:39:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt\_lethargic](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@matt\_lethargic](https://discuss.elastic.co/u/matt_lethargic)\
**Post date:** [October 21, 2015, 10:39am UTC](https://discuss.elastic.co/t/neighbouring-geo-shapes-polygons/32656/1 "2015-10-21T10:39:37Z")

</div>

Hi, I have an index of items with a geoshape field, I need to find all neighbouring polygons, is there a way to do this?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 22, 2015, 4:50am UTC](https://discuss.elastic.co/t/neighbouring-geo-shapes-polygons/32656/2 "2015-10-22T04:50:29Z")

</div>

I don't think so, but I may be wrong. What sort of use case would this be for?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 22, 2015, 11:07am UTC](https://discuss.elastic.co/t/neighbouring-geo-shapes-polygons/32656/3 "2015-10-22T11:07:00Z")

</div>

> [@matt\_lethargic](#):
>
> I need to find all neighbouring polygons, is there a way to do this?

Example here: [Shape intersects example · GitHub](https://gist.github.com/markharwood/c90f7abe1daa3640128b)

This is using spatial operator "intersects" although strictly speaking I guess you want "touches" (see [1] ) but this operator may be enough for your needs.

Cheers  
Mark

[1][DE-9IM - Wikipedia](https://en.wikipedia.org/wiki/DE-9IM#Spatial_predicates)

---

<div class="post-metadata">

**Author:** ![stephen84s](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephen84s/32/15789_2.png) [@stephen84s](https://discuss.elastic.co/u/stephen84s)\
**Post date:** [March 22, 2017, 11:30pm UTC](https://discuss.elastic.co/t/neighbouring-geo-shapes-polygons/32656/4 "2017-03-22T23:30:15Z")

</div>

I know this is a very old thread, but just responding since I encountered the same problem and found a work around.

Elastic Search for GeoShapes only supports three ways of querying:-

1. Intersects
2. Disjoint
3. Within

But the geo spatial operator to find neighbours as mentioned by @Mark_Harwood is touching.  
So the best way to get that same working would be "not" with "disjoint".  
Ex:-

```
GET index_name/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "geo_shape": {
            "shape_field_in_index_we_are_querying": {
              "relation": "disjoint",
              "indexed_shape": {
                "index": "index_with_your_geo_shape_to_compare_against",
                "type": "type",
                "id": "id_of_doc_with_shape_to_compare_against",
                "path": "Shape_key_in_doc_to_compare_with"
              }
            }
          }
        }
      ]
    }
  }
}

```

Sources:-  
[https://www.elastic.co/guide/en/elasticsearch/guide/current/indexed-geo-shapes.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/indexed-geo-shapes.html)  
[https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-shapes.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-shapes.html)

---

<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:** [July 5, 2017, 10:02pm UTC](https://discuss.elastic.co/t/neighbouring-geo-shapes-polygons/32656/5 "2017-07-05T22:02:00Z")

</div>


