elasticsearchelasticsearch-geo-shape

Find coordinates in a polygon


How can I find polygons that stored in elastic index. Simple mapping:

PUT /regions
{
    "mappings": {
        "properties": {
            "location": {
                "type": "geo_shape"
            }
        }
    }
}

And simple polygon:

/regions/_doc/1
{
    "location" : {
        "type" : "polygon",
        "coordinates" : [
            [ 
                [53.847332102970626,27.485155519098047],
                [53.84626875748117,27.487134989351038],
                [53.8449047241684,27.48501067981124],
                [53.84612634308789,27.482945378869765],
                [53.847411219859,27.48502677306532],
                [53.847332102970626,27.485155519098047] 
            ]
        ]
    }
}

According to documentation I can only search coordinates within polygon only if the polygon is contained in the request Geo-polygon query, but I need to find polygons by coordinates in query. Elasticsearch 7.6 version.

Query:

{
  "query": {
    "match_all": {}
  },
  "filter": {
    "geo_shape": {
      "geometry": {
        "shape": {
          "coordinates": [
            53.846415,
            27.485756
          ],
          "type": "point"
        },
        "relation": "whithin"
      }
    }
  }
}

Solution

  • You were on the right path but your query was heavily malformed. Here's the fix:

    {
      "query": {
        "bool": {
          "filter": {
            "geo_shape": {
              "location": {
                "shape": {
                  "coordinates": [
                    53.846415,
                    27.485756
                  ],
                  "type": "point"
                },
                "relation": "intersects"
              }
              
            }
          }
        }
      }
    }
    

    Notice how I used intersects instead of within. The reason is explained in this GIS StackExchange answer.