Geoshape field type

Introduced 1.0

A geoshape field type contains a geographic shape, such as a polygon or a collection of geographic points. To index a geoshape, OpenSearch tesselates the shape into a triangular mesh and stores each triangle in a BKD tree. This provides a 10-7decimal degree of precision, which represents near-perfect spatial resolution. Performance of this process is mostly impacted by the number of vertices in a polygon you are indexing.

Example

Create a mapping with a geoshape field type:

  1. PUT testindex
  2. {
  3. "mappings": {
  4. "properties": {
  5. "location": {
  6. "type": "geo_shape"
  7. }
  8. }
  9. }
  10. }

copy

Formats

Geoshapes can be indexed in the following formats:

In both GeoJSON and WKT, the coordinates must be specified in the longitude, latitude order within coordinate arrays. Note that the longitude comes first in this format.

Geoshape types

The following table describes the possible geoshape types and their relationship to the GeoJSON and WKT types.

OpenSearch typeGeoJSON typeWKT typeDescription
pointPointPOINTA geographic point specified by latitude and longitude. OpenSearch uses World Geodetic System (WGS84) coordinates.
linestringLineStringLINESTRINGA line specified by two or more points. May be a straight line or a path of connected line segments.
polygonPolygonPOLYGONA polygon specified by a list of vertices in coordinate form. The polygon must be closed, meaning the last point must be the same as the first point. Therefore, to create an n-gon, n+1 vertices are required. The minimum number of vertices is four, which creates a triangle.
multipointMultiPointMULTIPOINTAn array of discrete related points that are not connected.
multilinestringMultiLineStringMULTILINESTRINGAn array of linestrings.
multipolygonMultiPolygonMULTIPOLYGONAn array of polygons.
geometrycollectionGeometryCollectionGEOMETRYCOLLECTIONA collection of geoshapes that may be of different types.
envelopeN/ABBOXA bounding rectangle specified by upper-left and lower-right vertices.

Point

A point is a single pair of coordinates specified by latitude and longitude.

Index a point in GeoJSON format:

  1. PUT testindex/_doc/1
  2. {
  3. "location" : {
  4. "type" : "point",
  5. "coordinates" : [74.0060, 40.7128]
  6. }
  7. }

copy

Index a point in WKT format:

  1. PUT testindex/_doc/1
  2. {
  3. "location" : "POINT (74.0060 40.7128)"
  4. }

copy

Linestring

A linestring is a line specified by two or more points. If the points are collinear, the linestring is a straight line. Otherwise, the linestring represents a path made of line segments.

Index a linestring in GeoJSON format:

  1. PUT testindex/_doc/2
  2. {
  3. "location" : {
  4. "type" : "linestring",
  5. "coordinates" : [[74.0060, 40.7128], [71.0589, 42.3601]]
  6. }
  7. }

copy

Index a linestring in WKT format:

  1. PUT testindex/_doc/2
  2. {
  3. "location" : "LINESTRING (74.0060 40.7128, 71.0589 42.3601)"
  4. }

copy

Polygon

A polygon is specified by a list of vertices in coordinate form. The polygon must be closed, meaning the last point must be the same as the first point. In the following example, a triangle is created using four points.

GeoJSON requires that you list the vertices of the polygon counterclockwise. WKT does not impose a specific order on vertices.

Index a polygon (triangle) in GeoJSON format:

  1. PUT testindex/_doc/3
  2. {
  3. "location" : {
  4. "type" : "polygon",
  5. "coordinates" : [
  6. [
  7. [74.0060, 40.7128],
  8. [73.7562, 42.6526],
  9. [71.0589, 42.3601],
  10. [74.0060, 40.7128]
  11. ]
  12. ]
  13. }
  14. }

copy

Index a polygon (triangle) in WKT format:

  1. PUT testindex/_doc/3
  2. {
  3. "location" : "POLYGON ((74.0060 40.7128, 71.0589 42.3601, 73.7562 42.6526, 74.0060 40.7128))"
  4. }

copy

The polygon may have holes inside. In this case, the coordinates field will contain multiple arrays. The first array represents the outer polygon, and each subsequent array represents a hole. Holes are represented as polygons and specified as arrays of coordinates.

GeoJSON requires that you list the vertices of the polygon counterclockwise and the vertices of the hole clockwise. WKT does not impose a specific order on vertices.

Index a polygon (triangle) with a triangular hole in GeoJSON format:

  1. PUT testindex/_doc/4
  2. {
  3. "location" : {
  4. "type" : "polygon",
  5. "coordinates" : [
  6. [
  7. [74.0060, 40.7128],
  8. [73.7562, 42.6526],
  9. [71.0589, 42.3601],
  10. [74.0060, 40.7128]
  11. ],
  12. [
  13. [72.6734,41.7658],
  14. [73.0515, 41.5582],
  15. [72.6506, 41.5623],
  16. [72.6734, 41.7658]
  17. ]
  18. ]
  19. }
  20. }

copy

Index a polygon (triangle) with a triangular hole in WKT format:

  1. PUT testindex/_doc/4
  2. {
  3. "location" : "POLYGON ((74.0060 40.7128, 71.0589 42.3601, 73.7562 42.6526, 74.0060 40.7128), (72.6734 41.7658, 72.6506 41.5623, 73.0515 41.5582, 72.6734 41.7658))"
  4. }

copy

You can specify a polygon in OpenSearch by listing its vertices in clockwise or counterclockwise order. This works well for polygons that do not cross the date line (that are narrower than 180°). However, a polygon that crosses the date line (is wider than 180°) might be ambiguous because WKT does not impose a specific order on vertices. Thus, you must specify polygons that cross the date line by listing their vertices in counterclockwise order.

You can define an orientation parameter to specify the vertex traversal order at mapping time:

  1. PUT testindex
  2. {
  3. "mappings": {
  4. "properties": {
  5. "location": {
  6. "type": "geo_shape",
  7. "orientation" : "left"
  8. }
  9. }
  10. }
  11. }

copy

Subsequently indexed documents can override the orientation setting:

  1. PUT testindex/_doc/3
  2. {
  3. "location" : {
  4. "type" : "polygon",
  5. "orientation" : "cw",
  6. "coordinates" : [
  7. [[74.0060, 40.7128],
  8. [71.0589, 42.3601],
  9. [73.7562, 42.6526],
  10. [74.0060, 40.7128]]
  11. ]
  12. }
  13. }

copy

Multipoint

A multipoint is an array of discrete related points that are not connected.

Index a multipoint in GeoJSON format:

  1. PUT testindex/_doc/6
  2. {
  3. "location" : {
  4. "type" : "multipoint",
  5. "coordinates" : [
  6. [74.0060, 40.7128],
  7. [71.0589, 42.3601]
  8. ]
  9. }
  10. }

copy

Index a multipoint in WKT format:

  1. PUT testindex/_doc/6
  2. {
  3. "location" : "MULTIPOINT (74.0060 40.7128, 71.0589 42.3601)"
  4. }

copy

Multilinestring

A multilinestring is an array of linestrings.

Index a linestring in GeoJSON format:

  1. PUT testindex/_doc/2
  2. {
  3. "location" : {
  4. "type" : "multilinestring",
  5. "coordinates" : [
  6. [[74.0060, 40.7128], [71.0589, 42.3601]],
  7. [[73.7562, 42.6526], [72.6734, 41.7658]]
  8. ]
  9. }
  10. }

copy

Index a linestring in WKT format:

  1. PUT testindex/_doc/2
  2. {
  3. "location" : "MULTILINESTRING ((74.0060 40.7128, 71.0589 42.3601), (73.7562 42.6526, 72.6734 41.7658))"
  4. }

copy

Multipolygon

A multipolygon is an array of polygons. In this example, the first polygon contains a hole, and the second does not.

Index a multipolygon in GeoJSON format:

  1. PUT testindex/_doc/4
  2. {
  3. "location" : {
  4. "type" : "multipolygon",
  5. "coordinates" : [
  6. [
  7. [
  8. [74.0060, 40.7128],
  9. [73.7562, 42.6526],
  10. [71.0589, 42.3601],
  11. [74.0060, 40.7128]
  12. ],
  13. [
  14. [73.0515, 41.5582],
  15. [72.6506, 41.5623],
  16. [72.6734, 41.7658],
  17. [73.0515, 41.5582]
  18. ]
  19. ],
  20. [
  21. [
  22. [73.9146, 40.8252],
  23. [73.8871, 41.0389],
  24. [73.6853, 40.9747],
  25. [73.9146, 40.8252]
  26. ]
  27. ]
  28. ]
  29. }
  30. }

copy

Index a multipolygon in WKT format:

  1. PUT testindex/_doc/4
  2. {
  3. "location" : "MULTIPOLYGON (((74.0060 40.7128, 71.0589 42.3601, 73.7562 42.6526, 74.0060 40.7128), (72.6734 41.7658, 72.6506 41.5623, 73.0515 41.5582, 72.6734 41.7658)), ((73.9146 40.8252, 73.6853 40.9747, 73.8871 41.0389, 73.9146 40.8252)))"
  4. }

copy

Geometry collection

A geometry collection is a collection of geoshapes that may be of different types.

Index a geometry collection in GeoJSON format:

  1. PUT testindex/_doc/7
  2. {
  3. "location" : {
  4. "type": "geometrycollection",
  5. "geometries": [
  6. {
  7. "type": "point",
  8. "coordinates": [74.0060, 40.7128]
  9. },
  10. {
  11. "type": "linestring",
  12. "coordinates": [[73.7562, 42.6526], [72.6734, 41.7658]]
  13. }
  14. ]
  15. }
  16. }

copy

Index a geometry collection in WKT format:

  1. PUT testindex/_doc/7
  2. {
  3. "location" : "GEOMETRYCOLLECTION (POINT (74.0060 40.7128), LINESTRING(73.7562 42.6526, 72.6734 41.7658))"
  4. }

copy

Envelope

An envelope is a bounding rectangle specified by upper-left and lower-right vertices. The GeoJSON format is [[minLon, maxLat], [maxLon, minLat]].

Index an envelope in GeoJSON format:

  1. PUT testindex/_doc/2
  2. {
  3. "location" : {
  4. "type" : "envelope",
  5. "coordinates" : [[71.0589, 42.3601], [74.0060, 40.7128]]
  6. }
  7. }

copy

In WKT format, use BBOX (minLon, maxLon, maxLat, minLat).

Index an envelope in WKT BBOX format:

  1. PUT testindex/_doc/8
  2. {
  3. "location" : "BBOX (71.0589, 74.0060, 42.3601, 40.7128)"
  4. }

copy

Parameters

The following table lists the parameters accepted by geoshape field types. All parameters are optional.

ParameterDescription
coerceA Boolean value that specifies whether to automatically close unclosed linear rings. Default is false.
ignore_malformedA Boolean value that specifies to ignore malformed GeoJSON or WKT geoshapes and not to throw an exception. Default is false (throw an exception when geoshapes are malformed).
ignore_z_valueSpecific to points with three coordinates. If ignore_z_value is true, then the third coordinate is not indexed but is still stored in the _source field. If ignore_z_value is false, then an exception is thrown. Default is true.
orientationSpecifies the traversal order of the vertices in the geoshape’s list of coordinates. orientation takes the following values:
1. RIGHT: counterclockwise. Specify RIGHT orientation by using one of the following strings (uppercase or lowercase): right, counterclockwise, ccw.
2. LEFT: clockwise. Specify LEFT orientation by using one of the following strings (uppercase or lowercase): left, clockwise, cw. This value can be overridden by individual documents.
Default is RIGHT.