diff --git a/docs/reference/mapping/types.asciidoc b/docs/reference/mapping/types.asciidoc index 6794118537cab..8cd9e0f44f92c 100644 --- a/docs/reference/mapping/types.asciidoc +++ b/docs/reference/mapping/types.asciidoc @@ -54,6 +54,8 @@ string:: <> and <> <>:: Allows an entire JSON object to be indexed as a single field. +<>:: `shape` for arbitrary cartesian geometries. + [float] [[types-array-handling]] === Arrays @@ -121,4 +123,6 @@ include::types/sparse-vector.asciidoc[] include::types/text.asciidoc[] -include::types/token-count.asciidoc[] \ No newline at end of file +include::types/token-count.asciidoc[] + +include::types/shape.asciidoc[] \ No newline at end of file diff --git a/docs/reference/mapping/types/geo-shape.asciidoc b/docs/reference/mapping/types/geo-shape.asciidoc index 0fd370ec4e835..814fa89fbab66 100644 --- a/docs/reference/mapping/types/geo-shape.asciidoc +++ b/docs/reference/mapping/types/geo-shape.asciidoc @@ -296,7 +296,7 @@ differs from many Geospatial APIs (e.g., Google Maps) that generally use the colloquial latitude, longitude (Y, X). ============================================= -[[point]] +[[geo-point-type]] [float] ===== http://geojson.org/geojson-spec.html#id2[Point] @@ -328,7 +328,7 @@ POST /example/_doc // CONSOLE [float] -[[linestring]] +[[geo-linestring]] ===== http://geojson.org/geojson-spec.html#id3[LineString] A `linestring` defined by an array of two or more positions. By @@ -363,7 +363,7 @@ The above `linestring` would draw a straight line starting at the White House to the US Capitol Building. [float] -[[polygon]] +[[geo-polygon]] ===== http://www.geojson.org/geojson-spec.html#id4[Polygon] A polygon is defined by a list of a list of points. The first and last @@ -480,7 +480,7 @@ POST /example/_doc // CONSOLE [float] -[[multipoint]] +[[geo-multipoint]] ===== http://www.geojson.org/geojson-spec.html#id5[MultiPoint] The following is an example of a list of geojson points: @@ -511,7 +511,7 @@ POST /example/_doc // CONSOLE [float] -[[multilinestring]] +[[geo-multilinestring]] ===== http://www.geojson.org/geojson-spec.html#id6[MultiLineString] The following is an example of a list of geojson linestrings: @@ -544,7 +544,7 @@ POST /example/_doc // CONSOLE [float] -[[multipolygon]] +[[geo-multipolygon]] ===== http://www.geojson.org/geojson-spec.html#id7[MultiPolygon] The following is an example of a list of geojson polygons (second polygon contains a hole): @@ -577,7 +577,7 @@ POST /example/_doc // CONSOLE [float] -[[geometry_collection]] +[[geo-geometry_collection]] ===== http://geojson.org/geojson-spec.html#geometrycollection[Geometry Collection] The following is an example of a collection of geojson geometry objects: diff --git a/docs/reference/mapping/types/shape.asciidoc b/docs/reference/mapping/types/shape.asciidoc new file mode 100644 index 0000000000000..10695be364a50 --- /dev/null +++ b/docs/reference/mapping/types/shape.asciidoc @@ -0,0 +1,465 @@ +[[shape]] +[role="xpack"] +[testenv="basic"] +=== Shape datatype +++++ +Shape +++++ + +The `shape` datatype 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. + +[[shape-mapping-options]] +[float] +==== Mapping Options + +Like the <> field type, the `shape` field mapping maps +http://www.geojson.org[GeoJSON] or http://docs.opengeospatial.org/is/12-063r5/12-063r5.html[Well-Known Text] +(WKT) geometry objects to the shape type. To enable it, users must explicitly map +fields to the shape type. + +[cols="<,<,<",options="header",] +|======================================================================= +|Option |Description| Default + +|`orientation` |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: `right`, `ccw`, `counterclockwise`, +2. Left-hand rule: `left`, `cw`, `clockwise`. The default orientation +(`counterclockwise`) complies with the OGC standard which defines +outer ring vertices in counterclockwise order with inner ring(s) vertices (holes) +in clockwise order. Setting this parameter in the geo_shape mapping explicitly +sets vertex order for the coordinate list of a geo_shape field but can be +overridden in each individual GeoJSON or WKT document. +| `ccw` + +|`ignore_malformed` |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. +| `false` + +|`ignore_z_value` |If `true` (default) three dimension points will be accepted (stored in source) +but only latitude and longitude values will be indexed; the third dimension is ignored. If `false`, +geo-points containing any more than latitude and longitude (two dimensions) values throw an exception +and reject the whole document. +| `true` + +|`coerce` |If `true` unclosed linear rings in polygons will be automatically closed. +| `false` + +|======================================================================= + +[[shape-indexing-approach]] +[float] +==== Indexing approach +Like `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 guarantess 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* + +The following features are not yet supported: + +* `shape` query with `MultiPoint` geometry types - Elasticsearch currently prevents searching + `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. (Note: this could be very costly) + +* `CONTAINS` relation query - `shape` queries with `relation` defined as `contains` are not + yet supported. + +[float] +===== Example + +[source,js] +-------------------------------------------------- +PUT /example +{ + "mappings": { + "properties": { + "geometry": { + "type": "shape" + } + } + } +} +-------------------------------------------------- +// CONSOLE +// TESTSETUP + +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). + +[[shape-input-structure]] +[float] +==== Input Structure + +Shapes can be represented using either the http://www.geojson.org[GeoJSON] +or http://docs.opengeospatial.org/is/12-063r5/12-063r5.html[Well-Known Text] +(WKT) format. The following table provides a mapping of GeoJSON and WKT +to Elasticsearch types: + +[cols="<,<,<,<",options="header",] +|======================================================================= +|GeoJSON Type |WKT Type |Elasticsearch Type |Description + +|`Point` |`POINT` |`point` |A single `x, y` coordinate. +|`LineString` |`LINESTRING` |`linestring` |An arbitrary line given two or more points. +|`Polygon` |`POLYGON` |`polygon` |A _closed_ polygon whose first and last point +must match, thus requiring `n + 1` vertices to create an `n`-sided +polygon and a minimum of `4` vertices. +|`MultiPoint` |`MULTIPOINT` |`multipoint` |An array of unconnected, but likely related +points. +|`MultiLineString` |`MULTILINESTRING` |`multilinestring` |An array of separate linestrings. +|`MultiPolygon` |`MULTIPOLYGON` |`multipolygon` |An array of separate polygons. +|`GeometryCollection` |`GEOMETRYCOLLECTION` |`geometrycollection` | A shape collection similar to the +`multi*` shapes except that multiple types can coexist (e.g., a Point and a LineString). +|`N/A` |`BBOX` |`envelope` |A bounding rectangle, or envelope, specified by +specifying only the top left and bottom right points. +|======================================================================= + +[NOTE] +============================================= +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. +============================================= + +[[point]] +[float] +===== http://geojson.org/geojson-spec.html#id2[Point] + +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. + +[source,js] +-------------------------------------------------- +POST /example/_doc +{ + "location" : { + "type" : "point", + "coordinates" : [-377.03653, 389.897676] + } +} +-------------------------------------------------- +// CONSOLE + +The following is an example of a point in WKT: + +[source,js] +-------------------------------------------------- +POST /example/_doc +{ + "location" : "POINT (-377.03653 389.897676)" +} +-------------------------------------------------- +// CONSOLE + +[float] +[[linestring]] +===== http://geojson.org/geojson-spec.html#id3[LineString] + +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. + +[source,js] +-------------------------------------------------- +POST /example/_doc +{ + "location" : { + "type" : "linestring", + "coordinates" : [[-377.03653, 389.897676], [-377.009051, 389.889939]] + } +} +-------------------------------------------------- +// CONSOLE + +The following is an example of a LineString in WKT: + +[source,js] +-------------------------------------------------- +POST /example/_doc +{ + "location" : "LINESTRING (-377.03653 389.897676, -377.009051 389.889939)" +} +-------------------------------------------------- +// CONSOLE + +[float] +[[polygon]] +===== http://www.geojson.org/geojson-spec.html#id4[Polygon] + +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. + +[source,js] +-------------------------------------------------- +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] ] + ] + } +} +-------------------------------------------------- +// CONSOLE + +The following is an example of a Polygon in WKT: + +[source,js] +-------------------------------------------------- +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))" +} +-------------------------------------------------- +// CONSOLE + +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: + +[source,js] +-------------------------------------------------- +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] ] + ] + } +} +-------------------------------------------------- +// CONSOLE + +The following is an example of a Polygon with a hole in WKT: + +[source,js] +-------------------------------------------------- +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))" +} +-------------------------------------------------- +// CONSOLE + +*IMPORTANT NOTE:* WKT does not enforce a specific order for vertices. +https://tools.ietf.org/html/rfc7946#section-3.1.6[GeoJSON] mandates that the +outer polygon must be counterclockwise and interior shapes must be clockwise, +which agrees with the Open Geospatial Consortium (OGC) +http://www.opengeospatial.org/standards/sfa[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: + +[source,js] +-------------------------------------------------- +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] ] + ] + } +} +-------------------------------------------------- +// CONSOLE + +[float] +[[multipoint]] +===== http://www.geojson.org/geojson-spec.html#id5[MultiPoint] + +The following is an example of a list of geojson points: + +[source,js] +-------------------------------------------------- +POST /example/_doc +{ + "location" : { + "type" : "multipoint", + "coordinates" : [ + [1002.0, 1002.0], [1003.0, 2000.0] + ] + } +} +-------------------------------------------------- +// CONSOLE + +The following is an example of a list of WKT points: + +[source,js] +-------------------------------------------------- +POST /example/_doc +{ + "location" : "MULTIPOINT (1002.0 2000.0, 1003.0 2000.0)" +} +-------------------------------------------------- +// CONSOLE + +[float] +[[multilinestring]] +===== http://www.geojson.org/geojson-spec.html#id6[MultiLineString] + +The following is an example of a list of geojson linestrings: + +[source,js] +-------------------------------------------------- +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] ] + ] + } +} +-------------------------------------------------- +// CONSOLE + +The following is an example of a list of WKT linestrings: + +[source,js] +-------------------------------------------------- +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))" +} +-------------------------------------------------- +// CONSOLE + +[float] +[[multipolygon]] +===== http://www.geojson.org/geojson-spec.html#id7[MultiPolygon] + +The following is an example of a list of geojson polygons (second polygon contains a hole): + +[source,js] +-------------------------------------------------- +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]] ] + ] + } +} +-------------------------------------------------- +// CONSOLE + +The following is an example of a list of WKT polygons (second polygon contains a hole): + +[source,js] +-------------------------------------------------- +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)))" +} +-------------------------------------------------- +// CONSOLE + +[float] +[[geometry_collection]] +===== http://geojson.org/geojson-spec.html#geometrycollection[Geometry Collection] + +The following is an example of a collection of geojson geometry objects: + +[source,js] +-------------------------------------------------- +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] ] + } + ] + } +} +-------------------------------------------------- +// CONSOLE + +The following is an example of a collection of WKT geometry objects: + +[source,js] +-------------------------------------------------- +POST /example/_doc +{ + "location" : "GEOMETRYCOLLECTION (POINT (1000.0 100.0), LINESTRING (1001.0 100.0, 1002.0 100.0))" +} +-------------------------------------------------- +// CONSOLE + +[float] +===== Envelope + +Elasticsearch 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]]`: + +[source,js] +-------------------------------------------------- +POST /example/_doc +{ + "location" : { + "type" : "envelope", + "coordinates" : [ [1000.0, 100.0], [1001.0, 100.0] ] + } +} +-------------------------------------------------- +// CONSOLE + +The following is an example of an envelope using the WKT BBOX format: + +*NOTE:* WKT specification expects the following order: minLon, maxLon, maxLat, minLat. + +[source,js] +-------------------------------------------------- +POST /example/_doc +{ + "location" : "BBOX (1000.0, 1002.0, 2000.0, 1000.0)" +} +-------------------------------------------------- +// CONSOLE + +[float] +==== Sorting and Retrieving index Shapes + +Due 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. \ No newline at end of file diff --git a/server/src/main/java/org/elasticsearch/index/mapper/AbstractGeometryFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/AbstractGeometryFieldMapper.java index e9d71348f4389..baf0b695adf68 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/AbstractGeometryFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/AbstractGeometryFieldMapper.java @@ -95,8 +95,13 @@ public interface Parser { * interface representing a query builder that generates a query from the given shape */ public interface QueryProcessor { + Query process(Geometry shape, String fieldName, ShapeRelation relation, QueryShardContext context); - Query process(Geometry shape, String fieldName, SpatialStrategy strategy, ShapeRelation relation, QueryShardContext context); + @Deprecated + default Query process(Geometry shape, String fieldName, SpatialStrategy strategy, ShapeRelation relation, + QueryShardContext context) { + return process(shape, fieldName, relation, context); + } } public abstract static class Builder @@ -379,7 +384,7 @@ protected void parseCreateField(ParseContext context, List field } @Override - protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { + public void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { builder.field("type", contentType()); AbstractGeometryFieldType ft = (AbstractGeometryFieldType)fieldType(); if (includeDefaults || ft.orientation() != Defaults.ORIENTATION.value()) { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java index dafc024259f07..810ffc1bb22c8 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java @@ -482,7 +482,7 @@ public GeoShapeFieldType fieldType() { } @Override - protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { + public void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); if (includeDefaults diff --git a/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java b/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java index 42271f4b64b41..1eb4a6584c1d3 100644 --- a/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java +++ b/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java @@ -63,6 +63,11 @@ public LegacyGeoShapeQueryProcessor(AbstractGeometryFieldMapper.AbstractGeometry this.ft = ft; } + @Override + public Query process(Geometry shape, String fieldName, ShapeRelation relation, QueryShardContext context) { + throw new UnsupportedOperationException("process method should not be called for PrefixTree based geo_shapes"); + } + @Override public Query process(Geometry shape, String fieldName, SpatialStrategy strategy, ShapeRelation relation, QueryShardContext context) { LegacyGeoShapeFieldMapper.GeoShapeFieldType shapeFieldType = (LegacyGeoShapeFieldMapper.GeoShapeFieldType) ft; diff --git a/server/src/main/java/org/elasticsearch/index/query/VectorGeoShapeQueryProcessor.java b/server/src/main/java/org/elasticsearch/index/query/VectorGeoShapeQueryProcessor.java index 5ca03a7fab34d..9882fe069ebfd 100644 --- a/server/src/main/java/org/elasticsearch/index/query/VectorGeoShapeQueryProcessor.java +++ b/server/src/main/java/org/elasticsearch/index/query/VectorGeoShapeQueryProcessor.java @@ -28,7 +28,6 @@ import org.apache.lucene.search.Query; import org.elasticsearch.common.geo.GeoShapeType; import org.elasticsearch.common.geo.ShapeRelation; -import org.elasticsearch.common.geo.SpatialStrategy; import org.elasticsearch.geo.geometry.Circle; import org.elasticsearch.geo.geometry.Geometry; import org.elasticsearch.geo.geometry.GeometryCollection; @@ -48,7 +47,7 @@ public class VectorGeoShapeQueryProcessor implements AbstractGeometryFieldMapper.QueryProcessor { @Override - public Query process(Geometry shape, String fieldName, SpatialStrategy strategy, ShapeRelation relation, QueryShardContext context) { + public Query process(Geometry shape, String fieldName, ShapeRelation relation, QueryShardContext context) { // CONTAINS queries are not yet supported by VECTOR strategy if (relation == ShapeRelation.CONTAINS) { throw new QueryShardException(context, diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialPlugin.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialPlugin.java index b3f72e12afc2a..39babd68dcd3f 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialPlugin.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialPlugin.java @@ -8,15 +8,21 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.plugins.ActionPlugin; +import org.elasticsearch.plugins.MapperPlugin; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction; import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction; +import org.elasticsearch.xpack.spatial.index.mapper.ShapeFieldMapper; import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; -public class SpatialPlugin extends Plugin implements ActionPlugin { +public class SpatialPlugin extends Plugin implements ActionPlugin, MapperPlugin { public SpatialPlugin(Settings settings) { } @@ -27,4 +33,11 @@ public SpatialPlugin(Settings settings) { new ActionPlugin.ActionHandler<>(XPackUsageFeatureAction.SPATIAL, SpatialUsageTransportAction.class), new ActionPlugin.ActionHandler<>(XPackInfoFeatureAction.SPATIAL, SpatialInfoTransportAction.class)); } + + @Override + public Map getMappers() { + Map mappers = new LinkedHashMap<>(); + mappers.put(ShapeFieldMapper.CONTENT_TYPE, new ShapeFieldMapper.TypeParser()); + return Collections.unmodifiableMap(mappers); + } } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialUsageTransportAction.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialUsageTransportAction.java index a2873a2bcf938..7c00f2c0cb2dd 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialUsageTransportAction.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialUsageTransportAction.java @@ -40,7 +40,8 @@ public SpatialUsageTransportAction(TransportService transportService, ClusterSer @Override protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, ActionListener listener) { - SpatialFeatureSetUsage usage = new SpatialFeatureSetUsage(licenseState.isSpatialAllowed(), true); + SpatialFeatureSetUsage usage = + new SpatialFeatureSetUsage(licenseState.isSpatialAllowed(), true); listener.onResponse(new XPackUsageFeatureResponse(usage)); } } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java new file mode 100644 index 0000000000000..667422b8c00aa --- /dev/null +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java @@ -0,0 +1,134 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.spatial.index.mapper; + +import org.apache.lucene.document.XYShape; +import org.elasticsearch.common.Explicit; +import org.elasticsearch.common.geo.GeometryParser; +import org.elasticsearch.common.geo.builders.ShapeBuilder; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.geo.geometry.Geometry; +import org.elasticsearch.index.mapper.AbstractGeometryFieldMapper; +import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.MapperParsingException; +import org.elasticsearch.xpack.spatial.index.query.ShapeQueryProcessor; + +import java.util.Map; + +/** + * FieldMapper for indexing cartesian {@link XYShape}s. + *

+ * Format supported: + *

+ * "field" : { + * "type" : "polygon", + * "coordinates" : [ + * [ [1050.0, -1000.0], [1051.0, -1000.0], [1051.0, -1001.0], [1050.0, -1001.0], [1050.0, -1000.0] ] + * ] + * } + *

+ * or: + *

+ * "field" : "POLYGON ((1050.0 -1000.0, 1051.0 -1000.0, 1051.0 -1001.0, 1050.0 -1001.0, 1050.0 -1000.0)) + */ +public class ShapeFieldMapper extends AbstractGeometryFieldMapper { + public static final String CONTENT_TYPE = "shape"; + + public static class Defaults extends AbstractGeometryFieldMapper.Defaults { + public static final ShapeFieldType FIELD_TYPE = new ShapeFieldType(); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + public static class Builder extends AbstractGeometryFieldMapper.Builder { + + public Builder(String name) { + super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + builder = this; + } + + @Override + public ShapeFieldMapper build(BuilderContext context) { + setupFieldType(context); + return new ShapeFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), coerce(context), + ignoreZValue(), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); + } + + @Override + public ShapeFieldType fieldType() { + return (ShapeFieldType)fieldType; + } + + @SuppressWarnings("unchecked") + @Override + protected void setupFieldType(BuilderContext context) { + super.setupFieldType(context); + + GeometryParser geometryParser = new GeometryParser(orientation == ShapeBuilder.Orientation.RIGHT, + coerce(context).value(), ignoreZValue().value()); + + fieldType().setGeometryIndexer(new ShapeIndexer(fieldType().name())); + fieldType().setGeometryParser((parser, mapper) -> geometryParser.parse(parser)); + fieldType().setGeometryQueryBuilder(new ShapeQueryProcessor()); + } + } + + public static class TypeParser extends AbstractGeometryFieldMapper.TypeParser { + @Override + protected boolean parseXContentParameters(String name, Map.Entry entry, + Map params) throws MapperParsingException { + return false; + } + + @Override + public Builder newBuilder(String name, Map params) { + return new Builder(name); + } + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + public static final class ShapeFieldType extends AbstractGeometryFieldType { + public ShapeFieldType() { + super(); + } + + public ShapeFieldType(ShapeFieldType ref) { + super(ref); + } + + @Override + public ShapeFieldType clone() { + return new ShapeFieldType(this); + } + + @Override + public String typeName() { + return CONTENT_TYPE; + } + + @Override + protected Indexer geometryIndexer() { + return geometryIndexer; + } + } + + public ShapeFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + Explicit ignoreMalformed, Explicit coerce, + Explicit ignoreZValue, Settings indexSettings, + MultiFields multiFields, CopyTo copyTo) { + super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, ignoreZValue, indexSettings, + multiFields, copyTo); + } + + @Override + protected String contentType() { + return CONTENT_TYPE; + } + + @Override + public ShapeFieldType fieldType() { + return (ShapeFieldType) super.fieldType(); + } +} diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeIndexer.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeIndexer.java new file mode 100644 index 0000000000000..d76ef95d72a36 --- /dev/null +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeIndexer.java @@ -0,0 +1,157 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.spatial.index.mapper; + +import org.apache.lucene.document.XYShape; +import org.apache.lucene.geo.XYLine; +import org.apache.lucene.geo.XYPolygon; +import org.apache.lucene.index.IndexableField; +import org.elasticsearch.geo.geometry.Circle; +import org.elasticsearch.geo.geometry.Geometry; +import org.elasticsearch.geo.geometry.GeometryCollection; +import org.elasticsearch.geo.geometry.GeometryVisitor; +import org.elasticsearch.geo.geometry.LinearRing; +import org.elasticsearch.geo.geometry.MultiLine; +import org.elasticsearch.geo.geometry.MultiPoint; +import org.elasticsearch.geo.geometry.MultiPolygon; +import org.elasticsearch.geo.geometry.Point; +import org.elasticsearch.index.mapper.AbstractGeometryFieldMapper; +import org.elasticsearch.index.mapper.ParseContext; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ShapeIndexer implements AbstractGeometryFieldMapper.Indexer { + private final String name; + + public ShapeIndexer(String name) { + this.name = name; + } + + @Override + public Geometry prepareForIndexing(Geometry geometry) { + return geometry; + } + + @Override + public Class processedClass() { + return Geometry.class; + } + + @Override + public List indexShape(ParseContext context, Geometry shape) { + LuceneGeometryVisitor visitor = new LuceneGeometryVisitor(name); + shape.visit(visitor); + return visitor.fields; + } + + private class LuceneGeometryVisitor implements GeometryVisitor { + private List fields = new ArrayList<>(); + private String name; + + private LuceneGeometryVisitor(String name) { + this.name = name; + } + + @Override + public Void visit(Circle circle) { + throw new IllegalArgumentException("invalid shape type found [Circle] while indexing shape"); + } + + @Override + public Void visit(GeometryCollection collection) { + for (Geometry geometry : collection) { + geometry.visit(this); + } + return null; + } + + @Override + public Void visit(org.elasticsearch.geo.geometry.Line line) { + float[][] vertices = lineToFloatArray(line.getLons(), line.getLats()); + addFields(XYShape.createIndexableFields(name, new XYLine(vertices[0], vertices[1]))); + return null; + } + + @Override + public Void visit(LinearRing ring) { + throw new IllegalArgumentException("invalid shape type found [LinearRing] while indexing shape"); + } + + @Override + public Void visit(MultiLine multiLine) { + for (org.elasticsearch.geo.geometry.Line line : multiLine) { + visit(line); + } + return null; + } + + @Override + public Void visit(MultiPoint multiPoint) { + for(Point point : multiPoint) { + visit(point); + } + return null; + } + + @Override + public Void visit(MultiPolygon multiPolygon) { + for(org.elasticsearch.geo.geometry.Polygon polygon : multiPolygon) { + visit(polygon); + } + return null; + } + + @Override + public Void visit(Point point) { + addFields(XYShape.createIndexableFields(name, (float)point.getLon(), (float)point.getLat())); + return null; + } + + @Override + public Void visit(org.elasticsearch.geo.geometry.Polygon polygon) { + addFields(XYShape.createIndexableFields(name, toLucenePolygon(polygon))); + return null; + } + + @Override + public Void visit(org.elasticsearch.geo.geometry.Rectangle r) { + XYPolygon p = new XYPolygon( + new float[]{(float)r.getMinLon(), (float)r.getMaxLon(), (float)r.getMaxLon(), (float)r.getMinLon(), (float)r.getMinLon()}, + new float[]{(float)r.getMinLat(), (float)r.getMinLat(), (float)r.getMaxLat(), (float)r.getMaxLat(), (float)r.getMinLat()}); + addFields(XYShape.createIndexableFields(name, p)); + return null; + } + + private void addFields(IndexableField[] fields) { + this.fields.addAll(Arrays.asList(fields)); + } + } + + public static XYPolygon toLucenePolygon(org.elasticsearch.geo.geometry.Polygon polygon) { + XYPolygon[] holes = new XYPolygon[polygon.getNumberOfHoles()]; + LinearRing ring; + float[][] vertices; + for(int i = 0; i { + QueryShardContext context; + MappedFieldType fieldType; + String fieldName; + ShapeRelation relation; + + ShapeVisitor(QueryShardContext context, String fieldName, ShapeRelation relation) { + this.context = context; + this.fieldType = context.fieldMapper(fieldName); + this.fieldName = fieldName; + this.relation = relation; + } + + @Override + public Query visit(Circle circle) { + throw new QueryShardException(context, "Field [" + fieldName + "] found and unknown shape Circle"); + } + + @Override + public Query visit(GeometryCollection collection) { + BooleanQuery.Builder bqb = new BooleanQuery.Builder(); + visit(bqb, collection); + return bqb.build(); + } + + private void visit(BooleanQuery.Builder bqb, GeometryCollection collection) { + for (Geometry shape : collection) { + if (shape instanceof MultiPoint) { + // Flatten multipoints + visit(bqb, (GeometryCollection) shape); + } else { + bqb.add(shape.visit(this), BooleanClause.Occur.SHOULD); + } + } + } + + @Override + public Query visit(org.elasticsearch.geo.geometry.Line line) { + return XYShape.newLineQuery(fieldName, relation.getLuceneRelation(), + new XYLine(doubleArrayToFloatArray(line.getLons()), doubleArrayToFloatArray(line.getLats()))); + } + + @Override + public Query visit(LinearRing ring) { + throw new QueryShardException(context, "Field [" + fieldName + "] found and unsupported shape LinearRing"); + } + + @Override + public Query visit(MultiLine multiLine) { + XYLine[] lines = new XYLine[multiLine.size()]; + for (int i=0; i> getPlugins() { + return pluginList(InternalSettingsPlugin.class, SpatialPlugin.class, XPackPlugin.class); + } + + public void testDefaultConfiguration() throws IOException { + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location") + .field("type", "shape") + .endObject().endObject() + .endObject().endObject()); + + DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser() + .parse("type1", new CompressedXContent(mapping)); + Mapper fieldMapper = defaultMapper.mappers().getMapper("location"); + assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); + + ShapeFieldMapper shapeFieldMapper = (ShapeFieldMapper) fieldMapper; + assertThat(shapeFieldMapper.fieldType().orientation(), + equalTo(ShapeFieldMapper.Defaults.ORIENTATION.value())); + } + + /** + * Test that orientation parameter correctly parses + */ + public void testOrientationParsing() throws IOException { + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location") + .field("type", "shape") + .field("orientation", "left") + .endObject().endObject() + .endObject().endObject()); + + DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser() + .parse("type1", new CompressedXContent(mapping)); + Mapper fieldMapper = defaultMapper.mappers().getMapper("location"); + assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); + + ShapeBuilder.Orientation orientation = ((ShapeFieldMapper)fieldMapper).fieldType().orientation(); + assertThat(orientation, equalTo(ShapeBuilder.Orientation.CLOCKWISE)); + assertThat(orientation, equalTo(ShapeBuilder.Orientation.LEFT)); + assertThat(orientation, equalTo(ShapeBuilder.Orientation.CW)); + + // explicit right orientation test + mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location") + .field("type", "shape") + .field("orientation", "right") + .endObject().endObject() + .endObject().endObject()); + + defaultMapper = createIndex("test2").mapperService().documentMapperParser() + .parse("type1", new CompressedXContent(mapping)); + fieldMapper = defaultMapper.mappers().getMapper("location"); + assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); + + orientation = ((ShapeFieldMapper)fieldMapper).fieldType().orientation(); + assertThat(orientation, equalTo(ShapeBuilder.Orientation.COUNTER_CLOCKWISE)); + assertThat(orientation, equalTo(ShapeBuilder.Orientation.RIGHT)); + assertThat(orientation, equalTo(ShapeBuilder.Orientation.CCW)); + } + + /** + * Test that coerce parameter correctly parses + */ + public void testCoerceParsing() throws IOException { + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location") + .field("type", "shape") + .field("coerce", "true") + .endObject().endObject() + .endObject().endObject()); + + DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser() + .parse("type1", new CompressedXContent(mapping)); + Mapper fieldMapper = defaultMapper.mappers().getMapper("location"); + assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); + + boolean coerce = ((ShapeFieldMapper)fieldMapper).coerce().value(); + assertThat(coerce, equalTo(true)); + + // explicit false coerce test + mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location") + .field("type", "shape") + .field("coerce", "false") + .endObject().endObject() + .endObject().endObject()); + + defaultMapper = createIndex("test2").mapperService().documentMapperParser() + .parse("type1", new CompressedXContent(mapping)); + fieldMapper = defaultMapper.mappers().getMapper("location"); + assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); + + coerce = ((ShapeFieldMapper)fieldMapper).coerce().value(); + assertThat(coerce, equalTo(false)); + assertFieldWarnings("tree"); + } + + + /** + * Test that accept_z_value parameter correctly parses + */ + public void testIgnoreZValue() throws IOException { + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location") + .field("type", "shape") + .field(IGNORE_Z_VALUE.getPreferredName(), "true") + .endObject().endObject() + .endObject().endObject()); + + DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser() + .parse("type1", new CompressedXContent(mapping)); + Mapper fieldMapper = defaultMapper.mappers().getMapper("location"); + assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); + + boolean ignoreZValue = ((ShapeFieldMapper)fieldMapper).ignoreZValue().value(); + assertThat(ignoreZValue, equalTo(true)); + + // explicit false accept_z_value test + mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location") + .field("type", "shape") + .field(IGNORE_Z_VALUE.getPreferredName(), "false") + .endObject().endObject() + .endObject().endObject()); + + defaultMapper = createIndex("test2").mapperService().documentMapperParser() + .parse("type1", new CompressedXContent(mapping)); + fieldMapper = defaultMapper.mappers().getMapper("location"); + assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); + + ignoreZValue = ((ShapeFieldMapper)fieldMapper).ignoreZValue().value(); + assertThat(ignoreZValue, equalTo(false)); + } + + /** + * Test that ignore_malformed parameter correctly parses + */ + public void testIgnoreMalformedParsing() throws IOException { + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location") + .field("type", "shape") + .field("ignore_malformed", "true") + .endObject().endObject() + .endObject().endObject()); + + DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser() + .parse("type1", new CompressedXContent(mapping)); + Mapper fieldMapper = defaultMapper.mappers().getMapper("location"); + assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); + + Explicit ignoreMalformed = ((ShapeFieldMapper)fieldMapper).ignoreMalformed(); + assertThat(ignoreMalformed.value(), equalTo(true)); + + // explicit false ignore_malformed test + mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location") + .field("type", "shape") + .field("ignore_malformed", "false") + .endObject().endObject() + .endObject().endObject()); + + defaultMapper = createIndex("test2").mapperService().documentMapperParser() + .parse("type1", new CompressedXContent(mapping)); + fieldMapper = defaultMapper.mappers().getMapper("location"); + assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); + + ignoreMalformed = ((ShapeFieldMapper)fieldMapper).ignoreMalformed(); + assertThat(ignoreMalformed.explicit(), equalTo(true)); + assertThat(ignoreMalformed.value(), equalTo(false)); + } + + + private void assertFieldWarnings(String... fieldNames) { + String[] warnings = new String[fieldNames.length]; + for (int i = 0; i < fieldNames.length; ++i) { + warnings[i] = "Field parameter [" + fieldNames[i] + "] " + + "is deprecated and will be removed in a future version."; + } + } + + public void testShapeMapperMerge() throws Exception { + String stage1Mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") + .startObject("shape").field("type", "shape") + .field("orientation", "ccw") + .endObject().endObject().endObject().endObject()); + MapperService mapperService = createIndex("test").mapperService(); + DocumentMapper docMapper = mapperService.merge("type", new CompressedXContent(stage1Mapping), + MapperService.MergeReason.MAPPING_UPDATE); + String stage2Mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("properties").startObject("shape").field("type", "shape") + .field("orientation", "cw").endObject().endObject().endObject().endObject()); + mapperService.merge("type", new CompressedXContent(stage2Mapping), MapperService.MergeReason.MAPPING_UPDATE); + + // verify nothing changed + Mapper fieldMapper = docMapper.mappers().getMapper("shape"); + assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); + + ShapeFieldMapper ShapeFieldMapper = (ShapeFieldMapper) fieldMapper; + assertThat(ShapeFieldMapper.fieldType().orientation(), equalTo(ShapeBuilder.Orientation.CCW)); + + // change mapping; orientation + stage2Mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("properties").startObject("shape").field("type", "shape") + .field("orientation", "cw").endObject().endObject().endObject().endObject()); + docMapper = mapperService.merge("type", new CompressedXContent(stage2Mapping), MapperService.MergeReason.MAPPING_UPDATE); + + fieldMapper = docMapper.mappers().getMapper("shape"); + assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); + + ShapeFieldMapper shapeFieldMapper = (ShapeFieldMapper) fieldMapper; + assertThat(shapeFieldMapper.fieldType().orientation(), equalTo(ShapeBuilder.Orientation.CW)); + } + + public void testEmptyName() throws Exception { + // after 5.x + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("") + .field("type", "shape") + .endObject().endObject() + .endObject().endObject()); + DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); + + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, + () -> parser.parse("type1", new CompressedXContent(mapping)) + ); + assertThat(e.getMessage(), containsString("name cannot be empty string")); + } + + public void testSerializeDefaults() throws Exception { + DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); + { + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location") + .field("type", "shape") + .endObject().endObject() + .endObject().endObject()); + DocumentMapper defaultMapper = parser.parse("type1", new CompressedXContent(mapping)); + String serialized = toXContentString((ShapeFieldMapper) defaultMapper.mappers().getMapper("location")); + assertTrue(serialized, serialized.contains("\"orientation\":\"" + + AbstractGeometryFieldMapper.Defaults.ORIENTATION.value() + "\"")); + } + } + + public String toXContentString(ShapeFieldMapper mapper, boolean includeDefaults) throws IOException { + XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); + ToXContent.Params params; + if (includeDefaults) { + params = new ToXContent.MapParams(Collections.singletonMap("include_defaults", "true")); + } else { + params = ToXContent.EMPTY_PARAMS; + } + mapper.doXContentBody(builder, includeDefaults, params); + return Strings.toString(builder.endObject()); + } + + public String toXContentString(ShapeFieldMapper mapper) throws IOException { + return toXContentString(mapper, true); + } +}