From 22526818706e8cfbc8a05662209cc9a46e8dd70b Mon Sep 17 00:00:00 2001 From: Nicholas Knize Date: Mon, 29 Jul 2019 14:56:59 -0500 Subject: [PATCH 1/9] [GEO] New ShapeFieldMapper for indexing cartesian geometries This commit adds a new ShapeFieldMapper to the xpack spatial module for indexing arbitrary cartesian geometries using a new field type called shape. The indexing approach leverages lucene's new XYShape field type which is backed by BKD in the same manner as LatLonShape but without the WGS84 latitude longitude restrictions. The new field mapper builds on and extends the refactoring effort in AbstractGeometryFieldMapper and accepts shapes in either GeoJSON or WKT format (both of which support non geospatial geometries). Tests are provided in the ShapeFieldMapperTest class in the same manner as GeoShapeFieldMapperTests and LegacyGeoShapeFieldMapperTests. Documentation for how to use the new field type and what parameters are accepted. QueryBuilder is provided in a separate commit. --- docs/reference/mapping/types/shape.asciidoc | 464 ++++++++++++++++++ .../mapper/AbstractGeometryFieldMapper.java | 9 +- .../mapper/LegacyGeoShapeFieldMapper.java | 2 +- .../query/LegacyGeoShapeQueryProcessor.java | 5 + .../query/VectorGeoShapeQueryProcessor.java | 3 +- .../xpack/core/XPackSettings.java | 4 + .../xpack/spatial/SpatialPlugin.java | 23 +- .../spatial/SpatialUsageTransportAction.java | 4 +- .../index/mapper/ShapeFieldMapper.java | 269 ++++++++++ .../spatial/index/mapper/ShapeIndexer.java | 22 + .../index/query/ShapeQueryProcessor.java | 151 ++++++ .../index/mapper/ShapeFieldMapperTests.java | 297 +++++++++++ 12 files changed, 1246 insertions(+), 7 deletions(-) create mode 100644 docs/reference/mapping/types/shape.asciidoc create mode 100644 x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java create mode 100644 x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeIndexer.java create mode 100644 x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryProcessor.java create mode 100644 x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapperTests.java diff --git a/docs/reference/mapping/types/shape.asciidoc b/docs/reference/mapping/types/shape.asciidoc new file mode 100644 index 0000000000000..9ddee443549df --- /dev/null +++ b/docs/reference/mapping/types/shape.asciidoc @@ -0,0 +1,464 @@ +[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` + +|======================================================================= + +[[geoshape-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). + +[[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 397ae9fb04465..f0c9fb05bd925 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/AbstractGeometryFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/AbstractGeometryFieldMapper.java @@ -93,8 +93,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 @@ -377,7 +382,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 47adcbeb97237..68f97c71363de 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java @@ -516,7 +516,7 @@ private void doIndexShape(ParseContext context, Shape shape) { } @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..9ffe0dddff489 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) { + return process(shape, fieldName, LegacyGeoShapeFieldMapper.DeprecatedParameters.Defaults.STRATEGY, relation, context); + } + @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 68772d0e4d200..27de33ff85736 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/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java index 618b4c86a9126..6b9fecfada12f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java @@ -121,6 +121,9 @@ private XPackSettings() { /** Setting for enabling or disabling vectors. Defaults to true. */ public static final Setting VECTORS_ENABLED = Setting.boolSetting("xpack.vectors.enabled", true, Setting.Property.NodeScope); + /** Setting for enabling or disabling geo. Defaults to true. */ + public static final Setting SPATIAL_ENABLED = Setting.boolSetting("xpack.spatial.enabled", true, Setting.Property.NodeScope); + /* * SSL settings. These are the settings that are specifically registered for SSL. Many are private as we do not explicitly use them * but instead parse based on a prefix (eg *.ssl.*) @@ -213,6 +216,7 @@ public static List> getAllSettings() { settings.add(DATA_FRAME_ENABLED); settings.add(FLATTENED_ENABLED); settings.add(VECTORS_ENABLED); + settings.add(SPATIAL_ENABLED); return Collections.unmodifiableList(settings); } 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..234b060434657 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,17 +8,28 @@ 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.XPackSettings; 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 { +import static java.util.Collections.emptyMap; + +public class SpatialPlugin extends Plugin implements ActionPlugin, MapperPlugin { + protected final boolean enabled; public SpatialPlugin(Settings settings) { + this.enabled = XPackSettings.SPATIAL_ENABLED.get(settings); } @Override @@ -27,4 +38,14 @@ public SpatialPlugin(Settings settings) { new ActionPlugin.ActionHandler<>(XPackUsageFeatureAction.SPATIAL, SpatialUsageTransportAction.class), new ActionPlugin.ActionHandler<>(XPackInfoFeatureAction.SPATIAL, SpatialInfoTransportAction.class)); } + + @Override + public Map getMappers() { + if (enabled == false) { + return emptyMap(); + } + 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..5b1adc2678608 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 @@ -17,6 +17,7 @@ import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction; import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse; import org.elasticsearch.xpack.core.action.XPackUsageFeatureTransportAction; @@ -40,7 +41,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(), XPackSettings.SPATIAL_ENABLED.get(settings)); 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..4c2bd9d88fa2b --- /dev/null +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java @@ -0,0 +1,269 @@ +/* + * 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.Field; +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.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.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.MappedFieldType; +import org.elasticsearch.index.mapper.MapperParsingException; +import org.elasticsearch.index.mapper.ParseContext; +import org.elasticsearch.xpack.spatial.index.query.ShapeQueryProcessor; + +import java.util.ArrayList; +import java.util.Arrays; +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().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(); + } + + @Override + protected void indexShape(ParseContext context, Geometry shape) { + shape.visit(new LuceneGeometryIndexer(context)); + } + + /** @todo move to {@link ShapeIndexer} after refactor is complete */ + private class LuceneGeometryIndexer implements GeometryVisitor { + private ParseContext context; + + private LuceneGeometryIndexer(ParseContext context) { + this.context = context; + } + + @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[] x = new float[line.length()]; + float[] y = new float[x.length]; + for (int i = 0; i < x.length; ++i) { + x[i] = (float)line.getLon(i); + y[i] = (float)line.getLat(i); + } + indexFields(context, XYShape.createIndexableFields(name(), new XYLine(x, y))); + 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) { + indexFields(context, XYShape.createIndexableFields(name(), (float)point.getLon(), (float)point.getLat())); + return null; + } + + @Override + public Void visit(org.elasticsearch.geo.geometry.Polygon polygon) { + indexFields(context, 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()}); + indexFields(context, XYShape.createIndexableFields(name(), p)); + return null; + } + + private void indexFields(ParseContext context, Field[] fields) { + ArrayList flist = new ArrayList<>(Arrays.asList(fields)); + createFieldNamesField(context, flist); + for (IndexableField f : flist) { + context.doc().add(f); + } + } + } + + public static XYPolygon toLucenePolygon(org.elasticsearch.geo.geometry.Polygon polygon) { + XYPolygon[] holes = new XYPolygon[polygon.getNumberOfHoles()]; + LinearRing ring; + for(int i = 0; i { + + @Override + public Geometry prepareForIndexing(Geometry geometry) { + return geometry; + } + + @Override + public Class processedClass() { + return Geometry.class; + } +} diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryProcessor.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryProcessor.java new file mode 100644 index 0000000000000..62105f94f5f5f --- /dev/null +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryProcessor.java @@ -0,0 +1,151 @@ +/* + * 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.query; + +import org.apache.lucene.document.XYShape; +import org.apache.lucene.geo.XYLine; +import org.apache.lucene.geo.XYPolygon; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.ConstantScoreQuery; +import org.apache.lucene.search.MatchNoDocsQuery; +import org.apache.lucene.search.Query; +import org.elasticsearch.common.geo.GeoShapeType; +import org.elasticsearch.common.geo.ShapeRelation; +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.MappedFieldType; +import org.elasticsearch.index.query.QueryShardContext; +import org.elasticsearch.index.query.QueryShardException; + +import static org.elasticsearch.xpack.spatial.index.mapper.ShapeFieldMapper.toLucenePolygon; + +public class ShapeQueryProcessor implements AbstractGeometryFieldMapper.QueryProcessor { + + @Override + 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, + ShapeRelation.CONTAINS + " query relation not supported for Field [" + fieldName + "]"); + } + if (shape == null) { + return new MatchNoDocsQuery(); + } + // wrap geometry Query as a ConstantScoreQuery + return new ConstantScoreQuery(shape.visit(new ShapeVisitor(context, fieldName, relation))); + } + + private class ShapeVisitor implements GeometryVisitor { + 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); + } +} From b166a5ee4e525f1892b4d36483ebe0088cde5364 Mon Sep 17 00:00:00 2001 From: Nicholas Knize Date: Tue, 30 Jul 2019 09:51:05 -0500 Subject: [PATCH 2/9] update docs reference --- docs/reference/mapping/types.asciidoc | 6 +++++- docs/reference/mapping/types/shape.asciidoc | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) 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/shape.asciidoc b/docs/reference/mapping/types/shape.asciidoc index 9ddee443549df..fc38650cf7bff 100644 --- a/docs/reference/mapping/types/shape.asciidoc +++ b/docs/reference/mapping/types/shape.asciidoc @@ -1,3 +1,4 @@ +[[shape]] [role="xpack"] [testenv="basic"] === Shape datatype From 288b8d06a82d9829321c78b0b1b8314744517d2c Mon Sep 17 00:00:00 2001 From: Nicholas Knize Date: Tue, 30 Jul 2019 09:51:39 -0500 Subject: [PATCH 3/9] revert spatial module to always be enabled --- .../java/org/elasticsearch/xpack/core/XPackSettings.java | 4 ---- .../org/elasticsearch/xpack/spatial/SpatialPlugin.java | 8 -------- 2 files changed, 12 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java index 6b9fecfada12f..618b4c86a9126 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java @@ -121,9 +121,6 @@ private XPackSettings() { /** Setting for enabling or disabling vectors. Defaults to true. */ public static final Setting VECTORS_ENABLED = Setting.boolSetting("xpack.vectors.enabled", true, Setting.Property.NodeScope); - /** Setting for enabling or disabling geo. Defaults to true. */ - public static final Setting SPATIAL_ENABLED = Setting.boolSetting("xpack.spatial.enabled", true, Setting.Property.NodeScope); - /* * SSL settings. These are the settings that are specifically registered for SSL. Many are private as we do not explicitly use them * but instead parse based on a prefix (eg *.ssl.*) @@ -216,7 +213,6 @@ public static List> getAllSettings() { settings.add(DATA_FRAME_ENABLED); settings.add(FLATTENED_ENABLED); settings.add(VECTORS_ENABLED); - settings.add(SPATIAL_ENABLED); return Collections.unmodifiableList(settings); } 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 234b060434657..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 @@ -12,7 +12,6 @@ import org.elasticsearch.plugins.ActionPlugin; import org.elasticsearch.plugins.MapperPlugin; import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction; import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction; import org.elasticsearch.xpack.spatial.index.mapper.ShapeFieldMapper; @@ -23,13 +22,9 @@ import java.util.List; import java.util.Map; -import static java.util.Collections.emptyMap; - public class SpatialPlugin extends Plugin implements ActionPlugin, MapperPlugin { - protected final boolean enabled; public SpatialPlugin(Settings settings) { - this.enabled = XPackSettings.SPATIAL_ENABLED.get(settings); } @Override @@ -41,9 +36,6 @@ public SpatialPlugin(Settings settings) { @Override public Map getMappers() { - if (enabled == false) { - return emptyMap(); - } Map mappers = new LinkedHashMap<>(); mappers.put(ShapeFieldMapper.CONTENT_TYPE, new ShapeFieldMapper.TypeParser()); return Collections.unmodifiableMap(mappers); From 11ea5bf1db8e8e9e3d66a8cdb8a79d2826e02080 Mon Sep 17 00:00:00 2001 From: Nicholas Knize Date: Tue, 30 Jul 2019 10:31:57 -0500 Subject: [PATCH 4/9] fix lone SPATIAL_ENABLED in xpack spatial module --- .../xpack/spatial/SpatialUsageTransportAction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5b1adc2678608..d1a73c84d8e53 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 @@ -42,7 +42,7 @@ public SpatialUsageTransportAction(TransportService transportService, ClusterSer protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, ActionListener listener) { SpatialFeatureSetUsage usage = - new SpatialFeatureSetUsage(licenseState.isSpatialAllowed(), XPackSettings.SPATIAL_ENABLED.get(settings)); + new SpatialFeatureSetUsage(licenseState.isSpatialAllowed(), true); listener.onResponse(new XPackUsageFeatureResponse(usage)); } } From 28172ec2911897611033d5bd18543e257d82b013 Mon Sep 17 00:00:00 2001 From: Nicholas Knize Date: Tue, 30 Jul 2019 10:34:06 -0500 Subject: [PATCH 5/9] deconflict geo_shape and shape id tags in docs --- docs/reference/mapping/types/geo-shape.asciidoc | 14 +++++++------- docs/reference/mapping/types/shape.asciidoc | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/reference/mapping/types/geo-shape.asciidoc b/docs/reference/mapping/types/geo-shape.asciidoc index 0fd370ec4e835..406d50e6c6b74 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]] [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 index fc38650cf7bff..10695be364a50 100644 --- a/docs/reference/mapping/types/shape.asciidoc +++ b/docs/reference/mapping/types/shape.asciidoc @@ -52,7 +52,7 @@ and reject the whole document. |======================================================================= -[[geoshape-indexing-approach]] +[[shape-indexing-approach]] [float] ==== Indexing approach Like `geo_shape`, the `shape` field type is indexed by decomposing geometries into @@ -97,7 +97,7 @@ This mapping definition maps the geometry field to the shape type. The indexer u 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]] +[[shape-input-structure]] [float] ==== Input Structure From 8d78c4e85ceaf9a913b8c74ad44692642c32c865 Mon Sep 17 00:00:00 2001 From: Nicholas Knize Date: Tue, 30 Jul 2019 10:53:29 -0500 Subject: [PATCH 6/9] remove unused import --- .../elasticsearch/xpack/spatial/SpatialUsageTransportAction.java | 1 - 1 file changed, 1 deletion(-) 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 d1a73c84d8e53..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 @@ -17,7 +17,6 @@ import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; -import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction; import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse; import org.elasticsearch.xpack.core.action.XPackUsageFeatureTransportAction; From 2128b094c3b4f6c4f1388df7e9a18fb81f898fe0 Mon Sep 17 00:00:00 2001 From: Nicholas Knize Date: Tue, 30 Jul 2019 11:02:01 -0500 Subject: [PATCH 7/9] fix duplicate geo-point id in docs --- docs/reference/mapping/types/geo-shape.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/mapping/types/geo-shape.asciidoc b/docs/reference/mapping/types/geo-shape.asciidoc index 406d50e6c6b74..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). ============================================= -[[geo-point]] +[[geo-point-type]] [float] ===== http://geojson.org/geojson-spec.html#id2[Point] From 54c479708e167949fd9a69054498f9c4cfad876d Mon Sep 17 00:00:00 2001 From: Nicholas Knize Date: Tue, 30 Jul 2019 10:30:24 -0500 Subject: [PATCH 8/9] merge indexShape changes --- .../index/mapper/ShapeFieldMapper.java | 137 +---------------- .../spatial/index/mapper/ShapeIndexer.java | 140 ++++++++++++++++++ .../index/query/ShapeQueryProcessor.java | 2 +- 3 files changed, 142 insertions(+), 137 deletions(-) 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 index 4c2bd9d88fa2b..667422b8c00aa 100644 --- 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 @@ -5,32 +5,17 @@ */ package org.elasticsearch.xpack.spatial.index.mapper; -import org.apache.lucene.document.Field; 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.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.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.MappedFieldType; import org.elasticsearch.index.mapper.MapperParsingException; -import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.xpack.spatial.index.query.ShapeQueryProcessor; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Map; /** @@ -84,7 +69,7 @@ protected void setupFieldType(BuilderContext context) { GeometryParser geometryParser = new GeometryParser(orientation == ShapeBuilder.Orientation.RIGHT, coerce(context).value(), ignoreZValue().value()); - fieldType().setGeometryIndexer(new ShapeIndexer()); + fieldType().setGeometryIndexer(new ShapeIndexer(fieldType().name())); fieldType().setGeometryParser((parser, mapper) -> geometryParser.parse(parser)); fieldType().setGeometryQueryBuilder(new ShapeQueryProcessor()); } @@ -146,124 +131,4 @@ protected String contentType() { public ShapeFieldType fieldType() { return (ShapeFieldType) super.fieldType(); } - - @Override - protected void indexShape(ParseContext context, Geometry shape) { - shape.visit(new LuceneGeometryIndexer(context)); - } - - /** @todo move to {@link ShapeIndexer} after refactor is complete */ - private class LuceneGeometryIndexer implements GeometryVisitor { - private ParseContext context; - - private LuceneGeometryIndexer(ParseContext context) { - this.context = context; - } - - @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[] x = new float[line.length()]; - float[] y = new float[x.length]; - for (int i = 0; i < x.length; ++i) { - x[i] = (float)line.getLon(i); - y[i] = (float)line.getLat(i); - } - indexFields(context, XYShape.createIndexableFields(name(), new XYLine(x, y))); - 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) { - indexFields(context, XYShape.createIndexableFields(name(), (float)point.getLon(), (float)point.getLat())); - return null; - } - - @Override - public Void visit(org.elasticsearch.geo.geometry.Polygon polygon) { - indexFields(context, 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()}); - indexFields(context, XYShape.createIndexableFields(name(), p)); - return null; - } - - private void indexFields(ParseContext context, Field[] fields) { - ArrayList flist = new ArrayList<>(Arrays.asList(fields)); - createFieldNamesField(context, flist); - for (IndexableField f : flist) { - context.doc().add(f); - } - } - } - - public static XYPolygon toLucenePolygon(org.elasticsearch.geo.geometry.Polygon polygon) { - XYPolygon[] holes = new XYPolygon[polygon.getNumberOfHoles()]; - LinearRing ring; - for(int i = 0; i { + private final String name; + + public ShapeIndexer(String name) { + this.name = name; + } @Override public Geometry prepareForIndexing(Geometry geometry) { @@ -19,4 +41,122 @@ public Geometry prepareForIndexing(Geometry geometry) { 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[] x = new float[line.length()]; + float[] y = new float[x.length]; + for (int i = 0; i < x.length; ++i) { + x[i] = (float)line.getLon(i); + y[i] = (float)line.getLat(i); + } + addFields(XYShape.createIndexableFields(name, new XYLine(x, y))); + 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; + for(int i = 0; i Date: Tue, 30 Jul 2019 15:31:42 -0500 Subject: [PATCH 9/9] feedback from PR review --- .../query/LegacyGeoShapeQueryProcessor.java | 2 +- .../spatial/index/mapper/ShapeIndexer.java | 33 ++++++++----------- 2 files changed, 15 insertions(+), 20 deletions(-) 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 9ffe0dddff489..1eb4a6584c1d3 100644 --- a/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java +++ b/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java @@ -65,7 +65,7 @@ public LegacyGeoShapeQueryProcessor(AbstractGeometryFieldMapper.AbstractGeometry @Override public Query process(Geometry shape, String fieldName, ShapeRelation relation, QueryShardContext context) { - return process(shape, fieldName, LegacyGeoShapeFieldMapper.DeprecatedParameters.Defaults.STRATEGY, relation, context); + throw new UnsupportedOperationException("process method should not be called for PrefixTree based geo_shapes"); } @Override 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 index b1b51158d5726..d76ef95d72a36 100644 --- 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 @@ -72,13 +72,8 @@ public Void visit(GeometryCollection collection) { @Override public Void visit(org.elasticsearch.geo.geometry.Line line) { - float[] x = new float[line.length()]; - float[] y = new float[x.length]; - for (int i = 0; i < x.length; ++i) { - x[i] = (float)line.getLon(i); - y[i] = (float)line.getLat(i); - } - addFields(XYShape.createIndexableFields(name, new XYLine(x, y))); + float[][] vertices = lineToFloatArray(line.getLons(), line.getLats()); + addFields(XYShape.createIndexableFields(name, new XYLine(vertices[0], vertices[1]))); return null; } @@ -140,23 +135,23 @@ private void addFields(IndexableField[] 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