Shape field type
editShape field type
editThe shape
data type facilitates the indexing of and searching
with arbitrary x, y
cartesian shapes such as rectangles and polygons. It can be
used to index and query geometries whose coordinates fall in a 2-dimensional planar
coordinate system.
You can query documents using this type using shape Query.
Mapping Options
editLike the geo_shape
field type, the shape
field mapping maps
GeoJSON or Well-Known Text
(WKT) geometry objects to the shape type. To enable it, users must explicitly map
fields to the shape type.
Option | Description | Default |
---|---|---|
|
Optionally define how to interpret vertex order for
polygons / multipolygons. This parameter defines one of two coordinate
system rules (Right-hand or Left-hand) each of which can be specified in three
different ways. 1. Right-hand rule: |
|
|
If true, malformed GeoJSON or WKT shapes are ignored. If false (default), malformed GeoJSON and WKT shapes throw an exception and reject the entire document. |
|
|
If |
|
|
If |
|
Indexing approach
editLike geo_shape
, the shape
field type is indexed by decomposing geometries into
a triangular mesh and indexing each triangle as a 7 dimension point in a BKD tree.
The coordinates provided to the indexer are single precision floating point values so
the field guarantees the same accuracy provided by the java virtual machine (typically
1E-38
). For polygons/multi-polygons the performance of the tessellator primarily
depends on the number of vertices that define the geometry.
IMPORTANT NOTES
CONTAINS
relation query - shape
queries with relation
defined as contains
are supported
for indices created with ElasticSearch 7.5.0 or higher.
Example
editresp = client.indices.create( index="example", mappings={ "properties": { "geometry": { "type": "shape" } } }, ) print(resp)
response = client.indices.create( index: 'example', body: { mappings: { properties: { geometry: { type: 'shape' } } } } ) puts response
const response = await client.indices.create({ index: "example", mappings: { properties: { geometry: { type: "shape", }, }, }, }); console.log(response);
PUT /example { "mappings": { "properties": { "geometry": { "type": "shape" } } } }
This mapping definition maps the geometry field to the shape type. The indexer uses single
precision floats for the vertex values so accuracy is guaranteed to the same precision as
float
values provided by the java virtual machine approximately (typically 1E-38).
Input Structure
editShapes can be represented using either the GeoJSON or Well-Known Text (WKT) format. The following table provides a mapping of GeoJSON and WKT to Elasticsearch types:
GeoJSON Type | WKT Type | Elasticsearch Type | Description |
---|---|---|---|
|
|
|
A single |
|
|
|
An arbitrary line given two or more points. |
|
|
|
A closed polygon whose first and last point
must match, thus requiring |
|
|
|
An array of unconnected, but likely related points. |
|
|
|
An array of separate linestrings. |
|
|
|
An array of separate polygons. |
|
|
|
A shape collection similar to the
|
|
|
|
A bounding rectangle, or envelope, specified by specifying only the top left and bottom right points. |
For all types, both the inner type
and coordinates
fields are required.
In GeoJSON and WKT, and therefore Elasticsearch, the correct coordinate order is (X, Y)
within coordinate arrays. This differs from many Geospatial APIs (e.g., geo_shape
) that
typically use the colloquial latitude, longitude (Y, X) ordering.
A point is a single coordinate in cartesian x, y
space. It may represent the
location of an item of interest in a virtual world or projected space. The
following is an example of a point in GeoJSON.
resp = client.index( index="example", document={ "location": { "type": "point", "coordinates": [ -377.03653, 389.897676 ] } }, ) print(resp)
response = client.index( index: 'example', body: { location: { type: 'point', coordinates: [ -377.03653, 389.897676 ] } } ) puts response
const response = await client.index({ index: "example", document: { location: { type: "point", coordinates: [-377.03653, 389.897676], }, }, }); console.log(response);
POST /example/_doc { "location" : { "type" : "point", "coordinates" : [-377.03653, 389.897676] } }
The following is an example of a point in WKT:
resp = client.index( index="example", document={ "location": "POINT (-377.03653 389.897676)" }, ) print(resp)
response = client.index( index: 'example', body: { location: 'POINT (-377.03653 389.897676)' } ) puts response
const response = await client.index({ index: "example", document: { location: "POINT (-377.03653 389.897676)", }, }); console.log(response);
POST /example/_doc { "location" : "POINT (-377.03653 389.897676)" }
A linestring
defined by an array of two or more positions. By
specifying only two points, the linestring
will represent a straight
line. Specifying more than two points creates an arbitrary path. The
following is an example of a LineString in GeoJSON.
resp = client.index( index="example", document={ "location": { "type": "linestring", "coordinates": [ [ -377.03653, 389.897676 ], [ -377.009051, 389.889939 ] ] } }, ) print(resp)
response = client.index( index: 'example', body: { location: { type: 'linestring', coordinates: [ [ -377.03653, 389.897676 ], [ -377.009051, 389.889939 ] ] } } ) puts response
const response = await client.index({ index: "example", document: { location: { type: "linestring", coordinates: [ [-377.03653, 389.897676], [-377.009051, 389.889939], ], }, }, }); console.log(response);
POST /example/_doc { "location" : { "type" : "linestring", "coordinates" : [[-377.03653, 389.897676], [-377.009051, 389.889939]] } }
The following is an example of a LineString in WKT:
resp = client.index( index="example", document={ "location": "LINESTRING (-377.03653 389.897676, -377.009051 389.889939)" }, ) print(resp)
response = client.index( index: 'example', body: { location: 'LINESTRING (-377.03653 389.897676, -377.009051 389.889939)' } ) puts response
const response = await client.index({ index: "example", document: { location: "LINESTRING (-377.03653 389.897676, -377.009051 389.889939)", }, }); console.log(response);
POST /example/_doc { "location" : "LINESTRING (-377.03653 389.897676, -377.009051 389.889939)" }
A polygon is defined by a list of a list of points. The first and last points in each (outer) list must be the same (the polygon must be closed). The following is an example of a Polygon in GeoJSON.
resp = client.index( index="example", document={ "location": { "type": "polygon", "coordinates": [ [ [ 1000, -1001 ], [ 1001, -1001 ], [ 1001, -1000 ], [ 1000, -1000 ], [ 1000, -1001 ] ] ] } }, ) print(resp)
response = client.index( index: 'example', body: { location: { type: 'polygon', coordinates: [ [ [ 1000, -1001 ], [ 1001, -1001 ], [ 1001, -1000 ], [ 1000, -1000 ], [ 1000, -1001 ] ] ] } } ) puts response
const response = await client.index({ index: "example", document: { location: { type: "polygon", coordinates: [ [ [1000, -1001], [1001, -1001], [1001, -1000], [1000, -1000], [1000, -1001], ], ], }, }, }); console.log(response);
POST /example/_doc { "location" : { "type" : "polygon", "coordinates" : [ [ [1000.0, -1001.0], [1001.0, -1001.0], [1001.0, -1000.0], [1000.0, -1000.0], [1000.0, -1001.0] ] ] } }
The following is an example of a Polygon in WKT:
resp = client.index( index="example", document={ "location": "POLYGON ((1000.0 -1001.0, 1001.0 -1001.0, 1001.0 -1000.0, 1000.0 -1000.0, 1000.0 -1001.0))" }, ) print(resp)
response = client.index( index: 'example', body: { location: 'POLYGON ((1000.0 -1001.0, 1001.0 -1001.0, 1001.0 -1000.0, 1000.0 -1000.0, 1000.0 -1001.0))' } ) puts response
const response = await client.index({ index: "example", document: { location: "POLYGON ((1000.0 -1001.0, 1001.0 -1001.0, 1001.0 -1000.0, 1000.0 -1000.0, 1000.0 -1001.0))", }, }); console.log(response);
POST /example/_doc { "location" : "POLYGON ((1000.0 -1001.0, 1001.0 -1001.0, 1001.0 -1000.0, 1000.0 -1000.0, 1000.0 -1001.0))" }
The first array represents the outer boundary of the polygon, the other arrays represent the interior shapes ("holes"). The following is a GeoJSON example of a polygon with a hole:
resp = client.index( index="example", document={ "location": { "type": "polygon", "coordinates": [ [ [ 1000, -1001 ], [ 1001, -1001 ], [ 1001, -1000 ], [ 1000, -1000 ], [ 1000, -1001 ] ], [ [ 1000.2, -1001.2 ], [ 1000.8, -1001.2 ], [ 1000.8, -1001.8 ], [ 1000.2, -1001.8 ], [ 1000.2, -1001.2 ] ] ] } }, ) print(resp)
response = client.index( index: 'example', body: { location: { type: 'polygon', coordinates: [ [ [ 1000, -1001 ], [ 1001, -1001 ], [ 1001, -1000 ], [ 1000, -1000 ], [ 1000, -1001 ] ], [ [ 1000.2, -1001.2 ], [ 1000.8, -1001.2 ], [ 1000.8, -1001.8 ], [ 1000.2, -1001.8 ], [ 1000.2, -1001.2 ] ] ] } } ) puts response
const response = await client.index({ index: "example", document: { location: { type: "polygon", coordinates: [ [ [1000, -1001], [1001, -1001], [1001, -1000], [1000, -1000], [1000, -1001], ], [ [1000.2, -1001.2], [1000.8, -1001.2], [1000.8, -1001.8], [1000.2, -1001.8], [1000.2, -1001.2], ], ], }, }, }); console.log(response);
POST /example/_doc { "location" : { "type" : "polygon", "coordinates" : [ [ [1000.0, -1001.0], [1001.0, -1001.0], [1001.0, -1000.0], [1000.0, -1000.0], [1000.0, -1001.0] ], [ [1000.2, -1001.2], [1000.8, -1001.2], [1000.8, -1001.8], [1000.2, -1001.8], [1000.2, -1001.2] ] ] } }
The following is an example of a Polygon with a hole in WKT:
resp = client.index( index="example", document={ "location": "POLYGON ((1000.0 1000.0, 1001.0 1000.0, 1001.0 1001.0, 1000.0 1001.0, 1000.0 1000.0), (1000.2 1000.2, 1000.8 1000.2, 1000.8 1000.8, 1000.2 1000.8, 1000.2 1000.2))" }, ) print(resp)
response = client.index( index: 'example', body: { location: 'POLYGON ((1000.0 1000.0, 1001.0 1000.0, 1001.0 1001.0, 1000.0 1001.0, 1000.0 1000.0), (1000.2 1000.2, 1000.8 1000.2, 1000.8 1000.8, 1000.2 1000.8, 1000.2 1000.2))' } ) puts response
const response = await client.index({ index: "example", document: { location: "POLYGON ((1000.0 1000.0, 1001.0 1000.0, 1001.0 1001.0, 1000.0 1001.0, 1000.0 1000.0), (1000.2 1000.2, 1000.8 1000.2, 1000.8 1000.8, 1000.2 1000.8, 1000.2 1000.2))", }, }); console.log(response);
POST /example/_doc { "location" : "POLYGON ((1000.0 1000.0, 1001.0 1000.0, 1001.0 1001.0, 1000.0 1001.0, 1000.0 1000.0), (1000.2 1000.2, 1000.8 1000.2, 1000.8 1000.8, 1000.2 1000.8, 1000.2 1000.2))" }
IMPORTANT NOTE: WKT does not enforce a specific order for vertices. GeoJSON mandates that the outer polygon must be counterclockwise and interior shapes must be clockwise, which agrees with the Open Geospatial Consortium (OGC) Simple Feature Access specification for vertex ordering.
By default Elasticsearch expects vertices in counterclockwise (right hand rule)
order. If data is provided in clockwise order (left hand rule) the user can change
the orientation
parameter either in the field mapping, or as a parameter provided
with the document.
The following is an example of overriding the orientation
parameters on a document:
resp = client.index( index="example", document={ "location": { "type": "polygon", "orientation": "clockwise", "coordinates": [ [ [ 1000, 1000 ], [ 1000, 1001 ], [ 1001, 1001 ], [ 1001, 1000 ], [ 1000, 1000 ] ] ] } }, ) print(resp)
response = client.index( index: 'example', body: { location: { type: 'polygon', orientation: 'clockwise', coordinates: [ [ [ 1000, 1000 ], [ 1000, 1001 ], [ 1001, 1001 ], [ 1001, 1000 ], [ 1000, 1000 ] ] ] } } ) puts response
const response = await client.index({ index: "example", document: { location: { type: "polygon", orientation: "clockwise", coordinates: [ [ [1000, 1000], [1000, 1001], [1001, 1001], [1001, 1000], [1000, 1000], ], ], }, }, }); console.log(response);
POST /example/_doc { "location" : { "type" : "polygon", "orientation" : "clockwise", "coordinates" : [ [ [1000.0, 1000.0], [1000.0, 1001.0], [1001.0, 1001.0], [1001.0, 1000.0], [1000.0, 1000.0] ] ] } }
The following is an example of a list of GeoJSON points:
resp = client.index( index="example", document={ "location": { "type": "multipoint", "coordinates": [ [ 1002, 1002 ], [ 1003, 2000 ] ] } }, ) print(resp)
response = client.index( index: 'example', body: { location: { type: 'multipoint', coordinates: [ [ 1002, 1002 ], [ 1003, 2000 ] ] } } ) puts response
const response = await client.index({ index: "example", document: { location: { type: "multipoint", coordinates: [ [1002, 1002], [1003, 2000], ], }, }, }); console.log(response);
POST /example/_doc { "location" : { "type" : "multipoint", "coordinates" : [ [1002.0, 1002.0], [1003.0, 2000.0] ] } }
The following is an example of a list of WKT points:
resp = client.index( index="example", document={ "location": "MULTIPOINT (1002.0 2000.0, 1003.0 2000.0)" }, ) print(resp)
response = client.index( index: 'example', body: { location: 'MULTIPOINT (1002.0 2000.0, 1003.0 2000.0)' } ) puts response
const response = await client.index({ index: "example", document: { location: "MULTIPOINT (1002.0 2000.0, 1003.0 2000.0)", }, }); console.log(response);
POST /example/_doc { "location" : "MULTIPOINT (1002.0 2000.0, 1003.0 2000.0)" }
The following is an example of a list of GeoJSON linestrings:
resp = client.index( index="example", document={ "location": { "type": "multilinestring", "coordinates": [ [ [ 1002, 200 ], [ 1003, 200 ], [ 1003, 300 ], [ 1002, 300 ] ], [ [ 1000, 100 ], [ 1001, 100 ], [ 1001, 100 ], [ 1000, 100 ] ], [ [ 1000.2, 100.2 ], [ 1000.8, 100.2 ], [ 1000.8, 100.8 ], [ 1000.2, 100.8 ] ] ] } }, ) print(resp)
response = client.index( index: 'example', body: { location: { type: 'multilinestring', coordinates: [ [ [ 1002, 200 ], [ 1003, 200 ], [ 1003, 300 ], [ 1002, 300 ] ], [ [ 1000, 100 ], [ 1001, 100 ], [ 1001, 100 ], [ 1000, 100 ] ], [ [ 1000.2, 100.2 ], [ 1000.8, 100.2 ], [ 1000.8, 100.8 ], [ 1000.2, 100.8 ] ] ] } } ) puts response
const response = await client.index({ index: "example", document: { location: { type: "multilinestring", coordinates: [ [ [1002, 200], [1003, 200], [1003, 300], [1002, 300], ], [ [1000, 100], [1001, 100], [1001, 100], [1000, 100], ], [ [1000.2, 100.2], [1000.8, 100.2], [1000.8, 100.8], [1000.2, 100.8], ], ], }, }, }); console.log(response);
POST /example/_doc { "location" : { "type" : "multilinestring", "coordinates" : [ [ [1002.0, 200.0], [1003.0, 200.0], [1003.0, 300.0], [1002.0, 300.0] ], [ [1000.0, 100.0], [1001.0, 100.0], [1001.0, 100.0], [1000.0, 100.0] ], [ [1000.2, 100.2], [1000.8, 100.2], [1000.8, 100.8], [1000.2, 100.8] ] ] } }
The following is an example of a list of WKT linestrings:
resp = client.index( index="example", document={ "location": "MULTILINESTRING ((1002.0 200.0, 1003.0 200.0, 1003.0 300.0, 1002.0 300.0), (1000.0 100.0, 1001.0 100.0, 1001.0 100.0, 1000.0 100.0), (1000.2 0.2, 1000.8 100.2, 1000.8 100.8, 1000.2 100.8))" }, ) print(resp)
response = client.index( index: 'example', body: { location: 'MULTILINESTRING ((1002.0 200.0, 1003.0 200.0, 1003.0 300.0, 1002.0 300.0), (1000.0 100.0, 1001.0 100.0, 1001.0 100.0, 1000.0 100.0), (1000.2 0.2, 1000.8 100.2, 1000.8 100.8, 1000.2 100.8))' } ) puts response
const response = await client.index({ index: "example", document: { location: "MULTILINESTRING ((1002.0 200.0, 1003.0 200.0, 1003.0 300.0, 1002.0 300.0), (1000.0 100.0, 1001.0 100.0, 1001.0 100.0, 1000.0 100.0), (1000.2 0.2, 1000.8 100.2, 1000.8 100.8, 1000.2 100.8))", }, }); console.log(response);
POST /example/_doc { "location" : "MULTILINESTRING ((1002.0 200.0, 1003.0 200.0, 1003.0 300.0, 1002.0 300.0), (1000.0 100.0, 1001.0 100.0, 1001.0 100.0, 1000.0 100.0), (1000.2 0.2, 1000.8 100.2, 1000.8 100.8, 1000.2 100.8))" }
The following is an example of a list of GeoJSON polygons (second polygon contains a hole):
resp = client.index( index="example", document={ "location": { "type": "multipolygon", "coordinates": [ [ [ [ 1002, 200 ], [ 1003, 200 ], [ 1003, 300 ], [ 1002, 300 ], [ 1002, 200 ] ] ], [ [ [ 1000, 200 ], [ 1001, 100 ], [ 1001, 100 ], [ 1000, 100 ], [ 1000, 100 ] ], [ [ 1000.2, 200.2 ], [ 1000.8, 100.2 ], [ 1000.8, 100.8 ], [ 1000.2, 100.8 ], [ 1000.2, 100.2 ] ] ] ] } }, ) print(resp)
const response = await client.index({ index: "example", document: { location: { type: "multipolygon", coordinates: [ [ [ [1002, 200], [1003, 200], [1003, 300], [1002, 300], [1002, 200], ], ], [ [ [1000, 200], [1001, 100], [1001, 100], [1000, 100], [1000, 100], ], [ [1000.2, 200.2], [1000.8, 100.2], [1000.8, 100.8], [1000.2, 100.8], [1000.2, 100.2], ], ], ], }, }, }); console.log(response);
POST /example/_doc { "location" : { "type" : "multipolygon", "coordinates" : [ [ [[1002.0, 200.0], [1003.0, 200.0], [1003.0, 300.0], [1002.0, 300.0], [1002.0, 200.0]] ], [ [[1000.0, 200.0], [1001.0, 100.0], [1001.0, 100.0], [1000.0, 100.0], [1000.0, 100.0]], [[1000.2, 200.2], [1000.8, 100.2], [1000.8, 100.8], [1000.2, 100.8], [1000.2, 100.2]] ] ] } }
The following is an example of a list of WKT polygons (second polygon contains a hole):
resp = client.index( index="example", document={ "location": "MULTIPOLYGON (((1002.0 200.0, 1003.0 200.0, 1003.0 300.0, 1002.0 300.0, 102.0 200.0)), ((1000.0 100.0, 1001.0 100.0, 1001.0 100.0, 1000.0 100.0, 1000.0 100.0), (1000.2 100.2, 1000.8 100.2, 1000.8 100.8, 1000.2 100.8, 1000.2 100.2)))" }, ) print(resp)
const response = await client.index({ index: "example", document: { location: "MULTIPOLYGON (((1002.0 200.0, 1003.0 200.0, 1003.0 300.0, 1002.0 300.0, 102.0 200.0)), ((1000.0 100.0, 1001.0 100.0, 1001.0 100.0, 1000.0 100.0, 1000.0 100.0), (1000.2 100.2, 1000.8 100.2, 1000.8 100.8, 1000.2 100.8, 1000.2 100.2)))", }, }); console.log(response);
POST /example/_doc { "location" : "MULTIPOLYGON (((1002.0 200.0, 1003.0 200.0, 1003.0 300.0, 1002.0 300.0, 102.0 200.0)), ((1000.0 100.0, 1001.0 100.0, 1001.0 100.0, 1000.0 100.0, 1000.0 100.0), (1000.2 100.2, 1000.8 100.2, 1000.8 100.8, 1000.2 100.8, 1000.2 100.2)))" }
The following is an example of a collection of GeoJSON geometry objects:
resp = client.index( index="example", document={ "location": { "type": "geometrycollection", "geometries": [ { "type": "point", "coordinates": [ 1000, 100 ] }, { "type": "linestring", "coordinates": [ [ 1001, 100 ], [ 1002, 100 ] ] } ] } }, ) print(resp)
response = client.index( index: 'example', body: { location: { type: 'geometrycollection', geometries: [ { type: 'point', coordinates: [ 1000, 100 ] }, { type: 'linestring', coordinates: [ [ 1001, 100 ], [ 1002, 100 ] ] } ] } } ) puts response
const response = await client.index({ index: "example", document: { location: { type: "geometrycollection", geometries: [ { type: "point", coordinates: [1000, 100], }, { type: "linestring", coordinates: [ [1001, 100], [1002, 100], ], }, ], }, }, }); console.log(response);
POST /example/_doc { "location" : { "type": "geometrycollection", "geometries": [ { "type": "point", "coordinates": [1000.0, 100.0] }, { "type": "linestring", "coordinates": [ [1001.0, 100.0], [1002.0, 100.0] ] } ] } }
The following is an example of a collection of WKT geometry objects:
resp = client.index( index="example", document={ "location": "GEOMETRYCOLLECTION (POINT (1000.0 100.0), LINESTRING (1001.0 100.0, 1002.0 100.0))" }, ) print(resp)
response = client.index( index: 'example', body: { location: 'GEOMETRYCOLLECTION (POINT (1000.0 100.0), LINESTRING (1001.0 100.0, 1002.0 100.0))' } ) puts response
const response = await client.index({ index: "example", document: { location: "GEOMETRYCOLLECTION (POINT (1000.0 100.0), LINESTRING (1001.0 100.0, 1002.0 100.0))", }, }); console.log(response);
POST /example/_doc { "location" : "GEOMETRYCOLLECTION (POINT (1000.0 100.0), LINESTRING (1001.0 100.0, 1002.0 100.0))" }
Envelope
editElasticsearch supports an envelope
type, which consists of coordinates
for upper left and lower right points of the shape to represent a
bounding rectangle in the format [[minX, maxY], [maxX, minY]]
:
resp = client.index( index="example", document={ "location": { "type": "envelope", "coordinates": [ [ 1000, 100 ], [ 1001, 100 ] ] } }, ) print(resp)
const response = await client.index({ index: "example", document: { location: { type: "envelope", coordinates: [ [1000, 100], [1001, 100], ], }, }, }); console.log(response);
POST /example/_doc { "location" : { "type" : "envelope", "coordinates" : [ [1000.0, 100.0], [1001.0, 100.0] ] } }
The following is an example of an envelope using the WKT BBOX format:
NOTE: WKT specification expects the following order: minLon, maxLon, maxLat, minLat.
resp = client.index( index="example", document={ "location": "BBOX (1000.0, 1002.0, 2000.0, 1000.0)" }, ) print(resp)
const response = await client.index({ index: "example", document: { location: "BBOX (1000.0, 1002.0, 2000.0, 1000.0)", }, }); console.log(response);
POST /example/_doc { "location" : "BBOX (1000.0, 1002.0, 2000.0, 1000.0)" }
Sorting and Retrieving index Shapes
editDue to the complex input structure and index representation of shapes,
it is not currently possible to sort shapes or retrieve their fields
directly. The shape
value is only retrievable through the _source
field.