# How to find polygons that contain a given point in Elasticsearch

**URL:** https://discuss.elastic.co/t/how-to-find-polygons-that-contain-a-given-point-in-elasticsearch/343769
**Category:** Elasticsearch
**Created:** [September 25, 2023, 1:29pm UTC](https://discuss.elastic.co/t/how-to-find-polygons-that-contain-a-given-point-in-elasticsearch/343769 "2023-09-25T13:29:43Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Pranav\_Kapur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pranav_kapur/32/125984_2.png) [@Pranav\_Kapur](https://discuss.elastic.co/u/Pranav_Kapur)
#### Post date: [September 25, 2023, 1:29pm UTC](https://discuss.elastic.co/t/how-to-find-polygons-that-contain-a-given-point-in-elasticsearch/343769/1 "2023-09-25T13:29:43Z")

</div>

I need to build a query on a database with around 50k terrain polygons (stored as geo\_shape polygons on ES) where I give a point and it returns every polygon that contains this point.

I tried to create it, but getting incorrect result.

// Code

1. Create Index

```auto
{
    "mappings": {
        "TYPE_NAME": {
            "properties": {
                "location": {
                    "type": "geo_shape"
                }
            }
        }
    }
}

```

1. Insert document:

```auto
{
    "location": {
        "type": "polygon",
        "coordinates": [
            [
                [
                    88.357934264,
                    22.55818208
                ],
                [
                    88.365144042,
                    22.556745416
                ],
                [
                    88.363384513,
                    22.551920096
                ],
                [
                    88.360412625,
                    22.552474967
                ],
                [
                    88.360788135,
                    22.553614427
                ],
                [
                    88.35667899,
                    22.55359461
                ],
                [
                    88.357934264,
                    22.55818208
                ]
            ]
        ]
    }
}

```

1. Search for all polygons that contain the point:

```auto
{
    "query": {
        "geo_shape": {
            "location": {
                "shape": {
                    "type": "point",
                    "coordinates": [88.360, 22.555]
                },
                "relation": "within"
            }
        }
    }
}

```

Result:

```auto
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

```

Please let me know if I am missing out something as it is returning WRONG result.

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [September 25, 2023, 2:38pm UTC](https://discuss.elastic.co/t/how-to-find-polygons-that-contain-a-given-point-in-elasticsearch/343769/2 "2023-09-25T14:38:19Z")

</div>

Hi @Pranav_Kapur

Try this

```auto
GET discuss-geo/_search
{
  "query": {
    "geo_shape": {
      "location": {
        "shape": {
          "type": "point",
          "coordinates": [
            88.36,
            22.555
          ]
        },
        "relation": "contains"
      }
    }
  }
}

```

Read the [spatial relationship](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html#geo-shape-spatial-relations) a little closer

> - `WITHIN` - Return all documents whose `geo_shape` or `geo_point` field is within the query geometry. Line geometries are not supported.

When you said `within` I believe you were asking for your shape that was `within` the point 🙂 which is not correct for Point in Polygon

You want to use `contains`

> - `CONTAINS` - Return all documents whose `geo_shape` or `geo_point` field contains the query geometry.

And this is the result

```auto
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0,
    "hits": [
      {
        "_index": "discuss-geo",
        "_id": "Zwy9zIoBC8AxxyqEFesV",
        "_score": 0,
        "_source": {
          "location": {
            "type": "polygon",
            "coordinates": [
              [
                [
                  88.357934264,
                  22.55818208
                ],
                [
                  88.365144042,
                  22.556745416
                ],
                [
                  88.363384513,
                  22.551920096
                ],
                [
                  88.360412625,
                  22.552474967
                ],
                [
                  88.360788135,
                  22.553614427
                ],
                [
                  88.35667899,
                  22.55359461
                ],
                [
                  88.357934264,
                  22.55818208
                ]
              ]
            ]
          }
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

### Author: ![Pranav\_Kapur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pranav_kapur/32/125984_2.png) [@Pranav\_Kapur](https://discuss.elastic.co/u/Pranav_Kapur)
#### Post date: [September 25, 2023, 3:33pm UTC](https://discuss.elastic.co/t/how-to-find-polygons-that-contain-a-given-point-in-elasticsearch/343769/3 "2023-09-25T15:33:29Z")

</div>

Hi @stephenb

I tried using **relation: "contains"** ,

```auto
{
  "query": {
    "geo_shape": {
      "location": {
        "shape": {
          "type": "point",
          "coordinates": [
            88.36,
            22.555
          ]
        },
        "relation": "contains"
      }
    }
  }
}

```

but I am getting the following error:

```auto
{
    "error": {
        "root_cause": [
            {
                "type": "query_shard_exception",
                "reason": "CONTAINS query relation not supported for Field [location]",
                "index_uuid": "a4PebYDsRHiXSJmIU6U5eA",
                "index": "test"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "test",
                "node": "vchTHFfmRVGTtVELK5UquQ",
                "reason": {
                    "type": "query_shard_exception",
                    "reason": "CONTAINS query relation not supported for Field [location]",
                    "index_uuid": "a4PebYDsRHiXSJmIU6U5eA",
                    "index": "test"
                }
            }
        ]
    },
    "status": 400
}

```

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [September 25, 2023, 3:43pm UTC](https://discuss.elastic.co/t/how-to-find-polygons-that-contain-a-given-point-in-elasticsearch/343769/4 "2023-09-25T15:43:00Z")

</div>

@Pranav_Kapur Forgot .. Welcome to the community

My entire code is below, try mine and see what you get.

What version are you on?

```auto
PUT discuss-geo
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

POST discuss-geo/_doc
{
    "location": {
        "type": "polygon",
        "coordinates": [
            [
                [
                    88.357934264,
                    22.55818208
                ],
                [
                    88.365144042,
                    22.556745416
                ],
                [
                    88.363384513,
                    22.551920096
                ],
                [
                    88.360412625,
                    22.552474967
                ],
                [
                    88.360788135,
                    22.553614427
                ],
                [
                    88.35667899,
                    22.55359461
                ],
                [
                    88.357934264,
                    22.55818208
                ]
            ]
        ]
    }
}

GET discuss-geo/_search
{
  "query": {
    "geo_shape": {
      "location": {
        "shape": {
          "type": "point",
          "coordinates": [
            88.36,
            22.555
          ]
        },
        "relation": "contains"
      }
    }
  }
}

# Result

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0,
    "hits": [
      {
        "_index": "discuss-geo",
        "_id": "Zwy9zIoBC8AxxyqEFesV",
        "_score": 0,
        "_source": {
          "location": {
            "type": "polygon",
            "coordinates": [
              [
                [
                  88.357934264,
                  22.55818208
                ],
                [
                  88.365144042,
                  22.556745416
                ],
                [
                  88.363384513,
                  22.551920096
                ],
                [
                  88.360412625,
                  22.552474967
                ],
                [
                  88.360788135,
                  22.553614427
                ],
                [
                  88.35667899,
                  22.55359461
                ],
                [
                  88.357934264,
                  22.55818208
                ]
              ]
            ]
          }
        }
      }
    ]
  }
}

```

You code here do not look correct

```auto
{
    "mappings": {
        "TYPE_NAME": { <!---- What is this?
            "properties": {
                "location": {
                    "type": "geo_shape"
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![Pranav\_Kapur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pranav_kapur/32/125984_2.png) [@Pranav\_Kapur](https://discuss.elastic.co/u/Pranav_Kapur)
#### Post date: [September 25, 2023, 3:52pm UTC](https://discuss.elastic.co/t/how-to-find-polygons-that-contain-a-given-point-in-elasticsearch/343769/5 "2023-09-25T15:52:55Z")

</div>

Hi @stephenb

Elastic search version is 6.8.6

I tried creating index with the code that you shared:

```auto
{
    "mappings": {
        "properties": {
            "location": {
                "type": "geo_shape"
            }
        }
    }
}

```

but, I get the following error:

```auto
{
    "error": {
        "root_cause": [
            {
                "type": "mapper_parsing_exception",
                "reason": "Root mapping definition has unsupported parameters: [location : {type=geo_shape}]"
            }
        ],
        "type": "mapper_parsing_exception",
        "reason": "Failed to parse mapping [properties]: Root mapping definition has unsupported parameters: [location : {type=geo_shape}]",
        "caused_by": {
            "type": "mapper_parsing_exception",
            "reason": "Root mapping definition has unsupported parameters: [location : {type=geo_shape}]"
        }
    },
    "status": 400
}

```

I googled it, and found some link where, the below code was mentioned. I tried it and was able to create the index:

```auto
{
    "mappings": {
        "TYPE_NAME": {
            "properties": {
                "location": {
                    "type": "geo_shape"
                }
            }
        }
    }
}

```

In the link, TYPE\_NAME was mentioned as "type\_Mapping".

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [September 25, 2023, 4:05pm UTC](https://discuss.elastic.co/t/how-to-find-polygons-that-contain-a-given-point-in-elasticsearch/343769/6 "2023-09-25T16:05:57Z")

</div>

Hi @Pranav_Kapur you are on an **INCREDIBILY** old / out of date version of elasticsearch, many years out of date.

There have been massive improvements in the spatial capabilities

I do not even have a cluster to test on...

The reason I do recognize the `TYPE_NAME` as that is no longer valid on modern version... that was taken out in 7.x

You need to read the 6.8 docs super close all the spatial strategy stuff and types etc..etc..

> **[Geo-Shape datatype | Elasticsearch Guide \[6.8\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/geo-shape.html#spatial-strategy)**

**IMPORTANT NOTES**

The following features are not yet supported with the new indexing approach:

> - `geo_shape` query with `MultiPoint` geometry types - Elasticsearch currently prevents searching geo\_shape fields with a MultiPoint geometry type to avoid a brute force linear search over each individual point. For now, if this is absolutely needed, this can be achieved using a `bool` query with each individual point.
> - `CONTAINS` relation query - when using the new default vector indexing strategy, `geo_shape` queries with `relation` defined as `contains` are not yet supported. If this query relation is an absolute necessity, it is recommended to set `strategy` to `quadtree` and use the deprecated PrefixTree strategy indexing approach.

You best approach would be to get on a new version ASAP.

Don't google 🙂 read our docs in detail

Removal of Mapping Type

> **[Removal of mapping types | Elasticsearch Guide \[6.8\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/removal-of-types.html)**

time being replace `TYPE_NAME` with `_doc`

---

<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: [October 23, 2023, 4:06pm UTC](https://discuss.elastic.co/t/how-to-find-polygons-that-contain-a-given-point-in-elasticsearch/343769/7 "2023-10-23T16:06:47Z")

</div>

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