Multipolygon support for elastic search

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

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 field. For querying, you can use the geo_shape query which supports using multi-polygons.

In case you are indexing just points, it is better to use a geo_point field. Then you can use a geo_polygon 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.

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
]
}
}

                                            }
                         }
    }
}

}

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.

Thanks for update

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