# Multipolygon support for elastic search

**URL:** <https://discuss.elastic.co/t/multipolygon-support-for-elastic-search/223042>\
**Category:** Elasticsearch\
**Created:** [March 11, 2020, 4:58am UTC](https://discuss.elastic.co/t/multipolygon-support-for-elastic-search/223042 "2020-03-11T04:58:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tamil5ag](https://avatars.discourse-cdn.com/v4/letter/t/f1d935/32.png) [@tamil5ag](https://discuss.elastic.co/u/tamil5ag)\
**Post date:** [March 11, 2020, 4:58am UTC](https://discuss.elastic.co/t/multipolygon-support-for-elastic-search/223042/1 "2020-03-11T04:58:17Z")

</div>

Hi,

Elastic search support multipolygon query?  
If yes, how to form a query to get coordinates under different polygon(An array of separate polygons - multipolygon). And also which type need to define to create schema in DB , how to post the coordinates and how to get the coordinates under different polygon?

Share below query format also to identify the multipolygon flow

1. define schema in DB
2. Post data
3. get the coordinates under 2 different polygon in single query

Thanks,  
Tamil

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [March 11, 2020, 10:51am UTC](https://discuss.elastic.co/t/multipolygon-support-for-elastic-search/223042/2 "2020-03-11T10:51:48Z")

</div>

It depends on the data you want to index. Are you indexing only positions (points) or you want to index more complex geometries?

In case you are indexing complex geometries, you should index them in a [geo\_shape](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html) field. For querying, you can use the [geo\_shape](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html) query which supports using multi-polygons.

In case you are indexing just points, it is better to use a [geo\_point](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html) field. Then you can use a [geo\_polygon](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-query.html) query but unfortunately it currently does not support multi-polygons. Therefore you need to create a boolean query with the polygons.

Note we are actively working in making `geo_shape` queries to work on `geo_point` fields, so this won't be an issue anymore.

---

<div class="post-metadata">

**Author:** ![tamil5ag](https://avatars.discourse-cdn.com/v4/letter/t/f1d935/32.png) [@tamil5ag](https://discuss.elastic.co/u/tamil5ag)\
**Post date:** [March 11, 2020, 1:34pm UTC](https://discuss.elastic.co/t/multipolygon-support-for-elastic-search/223042/3 "2020-03-11T13:34:05Z")

</div>

Thanks for update.

I have used geo\_shape since it supports for multipolygon.

I have tried with below query to get coordinates under 2 different polygon and its working.  
{  
"query": {  
"bool" : {  
"must" : {  
"match\_all" : {}  
},  
"filter" :  
{  
"geo\_shape": {  
"\_name": "named\_query",  
"boost": 1.1,  
"ignore\_unmapped": true,  
"location": {  
"relation": "intersects",  
"shape": {  
"type": "multipolygon",  
"coordinates": [  
[[[2,3],[2,6],[5,7],[5,4],[2,3]] ],  
[[[-2,3],[-2,6],[-5,7],[-5,4],[-2,3]] ] ]

```
			              ]
		        }
		}
		   		
	          }
        		}
    }
}

```

}

**And have one more query - with data type geo\_shape, get the common coordinates between 3 polygon**

E.g : I have attached the diagram to understand the same and used a below query. Even though used relation as intersects in GET I got all the coordinates under all polygon. Now how we filter out common coordinates under ( (polygon1 and polygon2) or (polygon2 and polygon3) ) i.e need to get the 3,3and -3,4 coordinates only?

Schema:  
"location": {  
"type": "geo\_shape"  
},

Created below data in DB: (created multiple coordinates under polygon)  
Polygon 3:  
"location": {  
"type": "point",  
"coordinates": [3,3]  
},  
"location": {  
"type": "point",  
"coordinates": [5,3]  
},

Polygon 1:

"location": {  
"type": "point",  
"coordinates": [-3,4]  
},  
"location": {  
"type": "point",  
"coordinates": [-5,4]  
}

GET query:  
{  
"query": {  
"bool" : {  
"must" : {  
"match\_all" : {}  
},  
"filter" :  
{  
"geo\_shape": {  
"\_name": "named\_query",  
"boost": 1.1,  
"ignore\_unmapped": true,  
"location": {  
"relation": "intersects",  
"shape": {  
"type": "multipolygon",  
"coordinates": [  
[[[2,3],[2,6],[5,7],[5,4],[2,3]] ], - polygon3  
[[[-2,3],[-2,6],[-5,7],[-5,4],[-2,3]] ] – polygon1  
[[-3.5,2],[-3.5,7],[3.5,7],[3.5,2],[-3.5,2]] – polygon2  
]  
}  
}

```
                                            }
                         }
    }
}

```

}

 ![multipolygon](https://us1.discourse-cdn.com/elastic/original/3X/3/3/33075c13e972a71cc5dd04425065a98c276f87a6.png)

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [March 11, 2020, 2:04pm UTC](https://discuss.elastic.co/t/multipolygon-support-for-elastic-search/223042/4 "2020-03-11T14:04:34Z")

</div>

Although Elasticsearch allows to have polygons intersecting each other in a multi-polygon, it is actually a bad idea and in many other tools it would be an illegal shape.

You need to treat your polygons separately (use a query for each polygon) and use boolean conditions to match your requirements query . I would strongly recommend to use` geo_point` for this case.

---

<div class="post-metadata">

**Author:** ![tamil5ag](https://avatars.discourse-cdn.com/v4/letter/t/f1d935/32.png) [@tamil5ag](https://discuss.elastic.co/u/tamil5ag)\
**Post date:** [March 12, 2020, 12:31pm UTC](https://discuss.elastic.co/t/multipolygon-support-for-elastic-search/223042/5 "2020-03-12T12:31:00Z")

</div>

Thanks for update

---

<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:** [April 9, 2020, 12:31pm UTC](https://discuss.elastic.co/t/multipolygon-support-for-elastic-search/223042/6 "2020-04-09T12:31:04Z")

</div>

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