Neighbouring Geo Shapes / Polygons


(Matthew Hunt) #1

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?


(Mark Walkom) #2

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


(Mark Harwood) #3

Example here: 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]https://en.wikipedia.org/wiki/DE-9IM#Spatial_predicates


(Stephen) #4

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/geo-shapes.html


(system) #5