[SPATIAL] Backport new ShapeFieldMapper and ShapeQueryBuilder to 7x (#45363)
* Introduce Spatial Plugin (#44389) Introduce a skeleton Spatial plugin that holds new licensed features coming to Geo/Spatial land! * [GEO] Refactor DeprecatedParameters in AbstractGeometryFieldMapper (#44923) Refactor DeprecatedParameters specific to legacy geo_shape out of AbstractGeometryFieldMapper.TypeParser#parse. * [SPATIAL] New ShapeFieldMapper for indexing cartesian geometries (#44980) Add 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 is included. The QueryBuilder for searching indexed shapes is provided in a separate commit. * [SPATIAL] New ShapeQueryBuilder for querying indexed cartesian geometry (#45108) Add a new ShapeQueryBuilder to the xpack spatial module for querying arbitrary Cartesian geometries indexed using the new shape field type. The query builder extends AbstractGeometryQueryBuilder and leverages the ShapeQueryProcessor added in the previous field mapper commit. Tests are provided in ShapeQueryTests in the same manner as GeoShapeQueryTests and docs are updated to explain how the query works.
This commit is contained in:
parent
deab736aad
commit
647a8308c3
|
@ -54,6 +54,8 @@ string:: <<text,`text`>> and <<keyword,`keyword`>>
|
|||
|
||||
<<flattened>>:: Allows an entire JSON object to be indexed as a single field.
|
||||
|
||||
<<shape>>:: `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[]
|
||||
include::types/token-count.asciidoc[]
|
||||
|
||||
include::types/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:
|
||||
|
|
|
@ -0,0 +1,468 @@
|
|||
[[shape]]
|
||||
[role="xpack"]
|
||||
[testenv="basic"]
|
||||
=== Shape datatype
|
||||
++++
|
||||
<titleabbrev>Shape</titleabbrev>
|
||||
++++
|
||||
|
||||
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.
|
||||
|
||||
You can query documents using this type using
|
||||
<<query-dsl-shape-query,shape Query>>.
|
||||
|
||||
[[shape-mapping-options]]
|
||||
[float]
|
||||
==== Mapping Options
|
||||
|
||||
Like the <<geo-shape, geo_shape>> 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.
|
|
@ -35,6 +35,8 @@ include::query-dsl/full-text-queries.asciidoc[]
|
|||
|
||||
include::query-dsl/geo-queries.asciidoc[]
|
||||
|
||||
include::query-dsl/shape-queries.asciidoc[]
|
||||
|
||||
include::query-dsl/joining-queries.asciidoc[]
|
||||
|
||||
include::query-dsl/match-all-query.asciidoc[]
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
[[shape-queries]]
|
||||
[role="xpack"]
|
||||
[testenv="basic"]
|
||||
== Shape queries
|
||||
|
||||
Like <<geo-shape,`geo_shape`>> Elasticsearch supports the ability to index
|
||||
arbitrary two dimension (non Geospatial) geometries making it possible to
|
||||
map out virtual worlds, sporting venues, theme parks, and CAD diagrams. The
|
||||
<<shape,`shape`>> field type supports points, lines, polygons, multi-polygons,
|
||||
envelope, etc.
|
||||
|
||||
The queries in this group are:
|
||||
|
||||
<<query-dsl-shape-query,`shape`>> query::
|
||||
Finds documents with shapes that either intersect, are within, or do not
|
||||
intersect a specified shape.
|
||||
|
||||
include::shape-query.asciidoc[]
|
|
@ -0,0 +1,149 @@
|
|||
[[query-dsl-shape-query]]
|
||||
[role="xpack"]
|
||||
[testenv="basic"]
|
||||
=== Shape query
|
||||
++++
|
||||
<titleabbrev>Shape</titleabbrev>
|
||||
++++
|
||||
|
||||
Queries documents that contain fields indexed using the `shape` type.
|
||||
|
||||
Requires the <<shape,`shape` Mapping>>.
|
||||
|
||||
The query supports two ways of defining the target shape, either by
|
||||
providing a whole shape definition, or by referencing the name, or id, of a shape
|
||||
pre-indexed in another index. Both formats are defined below with
|
||||
examples.
|
||||
|
||||
==== Inline Shape Definition
|
||||
|
||||
Similar to the `geo_shape` query, the `shape` query uses
|
||||
http://www.geojson.org[GeoJSON] or
|
||||
https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry[Well Known Text]
|
||||
(WKT) to represent shapes.
|
||||
|
||||
Given the following index:
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
PUT /example
|
||||
{
|
||||
"mappings": {
|
||||
"properties": {
|
||||
"geometry": {
|
||||
"type": "shape"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
POST /example/_doc?refresh
|
||||
{
|
||||
"name": "Lucky Landing",
|
||||
"location": {
|
||||
"type": "point",
|
||||
"coordinates": [1355.400544, 5255.530286]
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
// TESTSETUP
|
||||
|
||||
The following query will find the point using the Elasticsearch's
|
||||
`envelope` GeoJSON extension:
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
GET /example/_search
|
||||
{
|
||||
"query":{
|
||||
"shape": {
|
||||
"geometry": {
|
||||
"shape": {
|
||||
"type": "envelope",
|
||||
"coordinates" : [[1355.0, 5355.0], [1400.0, 5200.0]]
|
||||
},
|
||||
"relation": "within"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
|
||||
==== Pre-Indexed Shape
|
||||
|
||||
The Query also supports using a shape which has already been indexed in
|
||||
another index. This is particularly useful for when
|
||||
you have a pre-defined list of shapes which are useful to your
|
||||
application and you want to reference this using a logical name (for
|
||||
example 'New Zealand') rather than having to provide their coordinates
|
||||
each time. In this situation it is only necessary to provide:
|
||||
|
||||
* `id` - The ID of the document that containing the pre-indexed shape.
|
||||
* `index` - Name of the index where the pre-indexed shape is. Defaults
|
||||
to 'shapes'.
|
||||
* `path` - The field specified as path containing the pre-indexed shape.
|
||||
Defaults to 'shape'.
|
||||
* `routing` - The routing of the shape document if required.
|
||||
|
||||
The following is an example of using the Filter with a pre-indexed
|
||||
shape:
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
PUT /shapes
|
||||
{
|
||||
"mappings": {
|
||||
"properties": {
|
||||
"geometry": {
|
||||
"type": "shape"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PUT /shapes/_doc/footprint
|
||||
{
|
||||
"geometry": {
|
||||
"type": "envelope",
|
||||
"coordinates" : [[1355.0, 5355.0], [1400.0, 5200.0]]
|
||||
}
|
||||
}
|
||||
|
||||
GET /example/_search
|
||||
{
|
||||
"query": {
|
||||
"shape": {
|
||||
"geometry": {
|
||||
"indexed_shape": {
|
||||
"index": "shapes",
|
||||
"id": "footprint",
|
||||
"path": "geometry"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
|
||||
==== Spatial Relations
|
||||
|
||||
The following is a complete list of spatial relation operators available:
|
||||
|
||||
* `INTERSECTS` - (default) Return all documents whose `geo_shape` field
|
||||
intersects the query geometry.
|
||||
* `DISJOINT` - Return all documents whose `geo_shape` field
|
||||
has nothing in common with the query geometry.
|
||||
* `WITHIN` - Return all documents whose `geo_shape` field
|
||||
is within the query geometry.
|
||||
|
||||
[float]
|
||||
==== Ignore Unmapped
|
||||
|
||||
When set to `true` the `ignore_unmapped` option will ignore an unmapped field
|
||||
and will not match any documents for this query. This can be useful when
|
||||
querying multiple indexes which might have different mappings. When set to
|
||||
`false` (the default value) the query will throw an exception if the field
|
||||
is not mapped.
|
|
@ -111,6 +111,10 @@ Example response:
|
|||
"available" : true,
|
||||
"enabled" : false
|
||||
},
|
||||
"spatial" : {
|
||||
"available" : true,
|
||||
"enabled" : true
|
||||
},
|
||||
"sql" : {
|
||||
"available" : true,
|
||||
"enabled" : true
|
||||
|
|
|
@ -43,6 +43,7 @@ import org.elasticsearch.index.query.QueryShardException;
|
|||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -94,8 +95,13 @@ public abstract class AbstractGeometryFieldMapper<Parsed, Processed> extends Fie
|
|||
* 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<T extends Builder, Y extends AbstractGeometryFieldMapper>
|
||||
|
@ -193,61 +199,73 @@ public abstract class AbstractGeometryFieldMapper<Parsed, Processed> extends Fie
|
|||
}
|
||||
}
|
||||
|
||||
protected static final String DEPRECATED_PARAMETERS_KEY = "deprecated_parameters";
|
||||
|
||||
public static class TypeParser implements Mapper.TypeParser {
|
||||
protected boolean parseXContentParameters(String name, Map.Entry<String, Object> entry, Map<String, Object> params)
|
||||
throws MapperParsingException {
|
||||
if (DeprecatedParameters.parse(name, entry.getKey(), entry.getValue(),
|
||||
(DeprecatedParameters)params.get(DEPRECATED_PARAMETERS_KEY))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected Builder newBuilder(String name, Map<String, Object> params) {
|
||||
if (params.containsKey(DEPRECATED_PARAMETERS_KEY)) {
|
||||
return new LegacyGeoShapeFieldMapper.Builder(name, (DeprecatedParameters)params.get(DEPRECATED_PARAMETERS_KEY));
|
||||
}
|
||||
return new GeoShapeFieldMapper.Builder(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
|
||||
Boolean coerce = null;
|
||||
Boolean ignoreZ = null;
|
||||
Boolean ignoreMalformed = null;
|
||||
Orientation orientation = null;
|
||||
DeprecatedParameters deprecatedParameters = new DeprecatedParameters();
|
||||
boolean parsedDeprecatedParams = false;
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
boolean parsedDeprecatedParameters = false;
|
||||
params.put(DEPRECATED_PARAMETERS_KEY, new DeprecatedParameters());
|
||||
for (Iterator<Map.Entry<String, Object>> iterator = node.entrySet().iterator(); iterator.hasNext();) {
|
||||
Map.Entry<String, Object> entry = iterator.next();
|
||||
String fieldName = entry.getKey();
|
||||
Object fieldNode = entry.getValue();
|
||||
if (DeprecatedParameters.parse(name, fieldName, fieldNode, deprecatedParameters)) {
|
||||
parsedDeprecatedParams = true;
|
||||
if (parseXContentParameters(name, entry, params)) {
|
||||
parsedDeprecatedParameters = true;
|
||||
iterator.remove();
|
||||
} else if (Names.ORIENTATION.match(fieldName, LoggingDeprecationHandler.INSTANCE)) {
|
||||
orientation = ShapeBuilder.Orientation.fromString(fieldNode.toString());
|
||||
params.put(Names.ORIENTATION.getPreferredName(), ShapeBuilder.Orientation.fromString(fieldNode.toString()));
|
||||
iterator.remove();
|
||||
} else if (IGNORE_MALFORMED.equals(fieldName)) {
|
||||
ignoreMalformed = XContentMapValues.nodeBooleanValue(fieldNode, name + ".ignore_malformed");
|
||||
params.put(IGNORE_MALFORMED, XContentMapValues.nodeBooleanValue(fieldNode, name + ".ignore_malformed"));
|
||||
iterator.remove();
|
||||
} else if (Names.COERCE.match(fieldName, LoggingDeprecationHandler.INSTANCE)) {
|
||||
coerce = XContentMapValues.nodeBooleanValue(fieldNode, name + "." + Names.COERCE.getPreferredName());
|
||||
params.put(Names.COERCE.getPreferredName(),
|
||||
XContentMapValues.nodeBooleanValue(fieldNode, name + "." + Names.COERCE.getPreferredName()));
|
||||
iterator.remove();
|
||||
} else if (GeoPointFieldMapper.Names.IGNORE_Z_VALUE.getPreferredName().equals(fieldName)) {
|
||||
ignoreZ = XContentMapValues.nodeBooleanValue(fieldNode,
|
||||
name + "." + GeoPointFieldMapper.Names.IGNORE_Z_VALUE.getPreferredName());
|
||||
params.put(GeoPointFieldMapper.Names.IGNORE_Z_VALUE.getPreferredName(),
|
||||
XContentMapValues.nodeBooleanValue(fieldNode,
|
||||
name + "." + GeoPointFieldMapper.Names.IGNORE_Z_VALUE.getPreferredName()));
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
final Builder builder;
|
||||
if (parsedDeprecatedParams || parserContext.indexVersionCreated().before(Version.V_6_6_0)) {
|
||||
// Legacy index-based shape
|
||||
builder = new LegacyGeoShapeFieldMapper.Builder(name, deprecatedParameters);
|
||||
} else {
|
||||
// BKD-based shape
|
||||
builder = new GeoShapeFieldMapper.Builder(name);
|
||||
if (parserContext.indexVersionCreated().onOrAfter(Version.V_6_6_0) && parsedDeprecatedParameters == false) {
|
||||
params.remove(DEPRECATED_PARAMETERS_KEY);
|
||||
}
|
||||
Builder builder = newBuilder(name, params);
|
||||
|
||||
if (params.containsKey(Names.COERCE.getPreferredName())) {
|
||||
builder.coerce((Boolean)params.get(Names.COERCE.getPreferredName()));
|
||||
}
|
||||
|
||||
if (coerce != null) {
|
||||
builder.coerce(coerce);
|
||||
if (params.containsKey(GeoPointFieldMapper.Names.IGNORE_Z_VALUE.getPreferredName())) {
|
||||
builder.ignoreZValue((Boolean)params.get(GeoPointFieldMapper.Names.IGNORE_Z_VALUE.getPreferredName()));
|
||||
}
|
||||
|
||||
if (ignoreZ != null) {
|
||||
builder.ignoreZValue(ignoreZ);
|
||||
if (params.containsKey(IGNORE_MALFORMED)) {
|
||||
builder.ignoreMalformed((Boolean)params.get(IGNORE_MALFORMED));
|
||||
}
|
||||
|
||||
if (ignoreMalformed != null) {
|
||||
builder.ignoreMalformed(ignoreMalformed);
|
||||
}
|
||||
|
||||
if (orientation != null) {
|
||||
builder.orientation(orientation);
|
||||
if (params.containsKey(Names.ORIENTATION.getPreferredName())) {
|
||||
builder.orientation((Orientation)params.get(Names.ORIENTATION.getPreferredName()));
|
||||
}
|
||||
|
||||
return builder;
|
||||
|
@ -302,7 +320,8 @@ public abstract class AbstractGeometryFieldMapper<Parsed, Processed> extends Fie
|
|||
|
||||
@Override
|
||||
public Query termQuery(Object value, QueryShardContext context) {
|
||||
throw new QueryShardException(context, "Geo fields do not support exact searching, use dedicated geo queries instead");
|
||||
throw new QueryShardException(context,
|
||||
"Geometry fields do not support exact searching, use dedicated geometry queries instead");
|
||||
}
|
||||
|
||||
public void setGeometryIndexer(Indexer<Parsed, Processed> geometryIndexer) {
|
||||
|
@ -361,10 +380,11 @@ public abstract class AbstractGeometryFieldMapper<Parsed, Processed> extends Fie
|
|||
|
||||
@Override
|
||||
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
|
||||
throw new UnsupportedOperationException("Parsing is implemented in parse(), this method should NEVER be called");
|
||||
}
|
||||
|
||||
@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()) {
|
||||
|
|
|
@ -484,7 +484,7 @@ public class LegacyGeoShapeFieldMapper extends AbstractGeometryFieldMapper<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
|
||||
|
|
|
@ -553,7 +553,7 @@ public abstract class AbstractGeometryQueryBuilder<QB extends AbstractGeometryQu
|
|||
}
|
||||
|
||||
/** local class that encapsulates xcontent parsed shape parameters */
|
||||
protected abstract static class ParsedShapeQueryParams {
|
||||
protected abstract static class ParsedGeometryQueryParams {
|
||||
public String fieldName;
|
||||
public ShapeRelation relation;
|
||||
public ShapeBuilder shape;
|
||||
|
@ -571,7 +571,7 @@ public abstract class AbstractGeometryQueryBuilder<QB extends AbstractGeometryQu
|
|||
protected abstract boolean parseXContentField(XContentParser parser) throws IOException;
|
||||
}
|
||||
|
||||
public static ParsedShapeQueryParams parsedParamsFromXContent(XContentParser parser, ParsedShapeQueryParams params)
|
||||
public static ParsedGeometryQueryParams parsedParamsFromXContent(XContentParser parser, ParsedGeometryQueryParams params)
|
||||
throws IOException {
|
||||
String fieldName = null;
|
||||
XContentParser.Token token;
|
||||
|
|
|
@ -237,7 +237,7 @@ public class GeoShapeQueryBuilder extends AbstractGeometryQueryBuilder<GeoShapeQ
|
|||
return builder;
|
||||
}
|
||||
|
||||
private static class ParsedGeoShapeQueryParams extends ParsedShapeQueryParams {
|
||||
private static class ParsedGeoShapeQueryParams extends ParsedGeometryQueryParams {
|
||||
SpatialStrategy strategy;
|
||||
|
||||
@Override
|
||||
|
|
|
@ -63,6 +63,11 @@ public class LegacyGeoShapeQueryProcessor implements AbstractGeometryFieldMapper
|
|||
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;
|
||||
|
|
|
@ -28,7 +28,6 @@ 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.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 @@ import static org.elasticsearch.index.mapper.GeoShapeIndexer.toLucenePolygon;
|
|||
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,
|
||||
|
|
|
@ -61,13 +61,15 @@ public class GeometryTestUtils {
|
|||
}
|
||||
|
||||
public static Line randomLine(boolean hasAlts) {
|
||||
int size = ESTestCase.randomIntBetween(2, 10);
|
||||
// we use nextPolygon because it guarantees no duplicate points
|
||||
org.apache.lucene.geo.Polygon lucenePolygon = GeoTestUtil.nextPolygon();
|
||||
int size = lucenePolygon.numPoints() - 1;
|
||||
double[] lats = new double[size];
|
||||
double[] lons = new double[size];
|
||||
double[] alts = hasAlts ? new double[size] : null;
|
||||
for (int i = 0; i < size; i++) {
|
||||
lats[i] = randomLat();
|
||||
lons[i] = randomLon();
|
||||
lats[i] = lucenePolygon.getPolyLat(i);
|
||||
lons[i] = lucenePolygon.getPolyLon(i);
|
||||
if (hasAlts) {
|
||||
alts[i] = randomAlt();
|
||||
}
|
||||
|
@ -96,11 +98,12 @@ public class GeometryTestUtils {
|
|||
org.apache.lucene.geo.Polygon[] luceneHoles = lucenePolygon.getHoles();
|
||||
List<LinearRing> holes = new ArrayList<>();
|
||||
for (int i = 0; i < lucenePolygon.numHoles(); i++) {
|
||||
holes.add(linearRing(luceneHoles[i], hasAlt));
|
||||
org.apache.lucene.geo.Polygon poly = luceneHoles[i];
|
||||
holes.add(linearRing(poly.getPolyLats(), poly.getPolyLons(), hasAlt));
|
||||
}
|
||||
return new Polygon(linearRing(lucenePolygon, hasAlt), holes);
|
||||
return new Polygon(linearRing(lucenePolygon.getPolyLats(), lucenePolygon.getPolyLons(), hasAlt), holes);
|
||||
}
|
||||
return new Polygon(linearRing(lucenePolygon, hasAlt));
|
||||
return new Polygon(linearRing(lucenePolygon.getPolyLats(), lucenePolygon.getPolyLons(), hasAlt));
|
||||
}
|
||||
|
||||
|
||||
|
@ -113,12 +116,11 @@ public class GeometryTestUtils {
|
|||
return alts;
|
||||
}
|
||||
|
||||
private static LinearRing linearRing(org.apache.lucene.geo.Polygon polygon, boolean generateAlts) {
|
||||
public static LinearRing linearRing(double[] lats, double[] lons, boolean generateAlts) {
|
||||
if (generateAlts) {
|
||||
return new LinearRing(polygon.getPolyLats(), polygon.getPolyLons(), randomAltRing(polygon.numPoints()));
|
||||
} else {
|
||||
return new LinearRing(polygon.getPolyLats(), polygon.getPolyLons());
|
||||
return new LinearRing(lats, lons, randomAltRing(lats.length));
|
||||
}
|
||||
return new LinearRing(lats, lons);
|
||||
}
|
||||
|
||||
public static Rectangle randomRectangle() {
|
||||
|
@ -170,7 +172,7 @@ public class GeometryTestUtils {
|
|||
return randomGeometry(0, hasAlt);
|
||||
}
|
||||
|
||||
private static Geometry randomGeometry(int level, boolean hasAlt) {
|
||||
protected static Geometry randomGeometry(int level, boolean hasAlt) {
|
||||
@SuppressWarnings("unchecked") Function<Boolean, Geometry> geometry = ESTestCase.randomFrom(
|
||||
GeometryTestUtils::randomCircle,
|
||||
GeometryTestUtils::randomLine,
|
||||
|
|
|
@ -728,6 +728,22 @@ public class XPackLicenseState {
|
|||
return licensed && localStatus.active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if Spatial features should be enabled.
|
||||
* <p>
|
||||
* Spatial features are available in for all license types except
|
||||
* {@link OperationMode#MISSING}
|
||||
*
|
||||
* @return {@code true} as long as the license is valid. Otherwise
|
||||
* {@code false}.
|
||||
*/
|
||||
public boolean isSpatialAllowed() {
|
||||
// status is volatile
|
||||
Status localStatus = status;
|
||||
// Should work on all active licenses
|
||||
return localStatus.active;
|
||||
}
|
||||
|
||||
public synchronized boolean isTrialLicense() {
|
||||
return status.mode == OperationMode.TRIAL;
|
||||
}
|
||||
|
|
|
@ -209,6 +209,7 @@ import org.elasticsearch.xpack.core.slm.action.DeleteSnapshotLifecycleAction;
|
|||
import org.elasticsearch.xpack.core.slm.action.ExecuteSnapshotLifecycleAction;
|
||||
import org.elasticsearch.xpack.core.slm.action.GetSnapshotLifecycleAction;
|
||||
import org.elasticsearch.xpack.core.slm.action.PutSnapshotLifecycleAction;
|
||||
import org.elasticsearch.xpack.core.spatial.SpatialFeatureSetUsage;
|
||||
import org.elasticsearch.xpack.core.sql.SqlFeatureSetUsage;
|
||||
import org.elasticsearch.xpack.core.ssl.SSLService;
|
||||
import org.elasticsearch.xpack.core.ssl.action.GetCertificateInfoAction;
|
||||
|
@ -541,7 +542,9 @@ public class XPackClientPlugin extends Plugin implements ActionPlugin, NetworkPl
|
|||
// Voting Only Node
|
||||
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.VOTING_ONLY, VotingOnlyNodeFeatureSetUsage::new),
|
||||
// Frozen indices
|
||||
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.FROZEN_INDICES, FrozenIndicesFeatureSetUsage::new)
|
||||
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.FROZEN_INDICES, FrozenIndicesFeatureSetUsage::new),
|
||||
// Spatial
|
||||
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.SPATIAL, SpatialFeatureSetUsage::new)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -45,6 +45,8 @@ public final class XPackField {
|
|||
public static final String VOTING_ONLY = "voting_only";
|
||||
/** Name constant for the frozen index feature. */
|
||||
public static final String FROZEN_INDICES = "frozen_indices";
|
||||
/** Name constant for spatial features. */
|
||||
public static final String SPATIAL = "spatial";
|
||||
|
||||
private XPackField() {}
|
||||
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* 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.core.spatial;
|
||||
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.xpack.core.XPackFeatureSet;
|
||||
import org.elasticsearch.xpack.core.XPackField;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
public class SpatialFeatureSetUsage extends XPackFeatureSet.Usage {
|
||||
|
||||
public SpatialFeatureSetUsage(boolean available, boolean enabled) {
|
||||
super(XPackField.SPATIAL, available, enabled);
|
||||
}
|
||||
|
||||
public SpatialFeatureSetUsage(StreamInput input) throws IOException {
|
||||
super(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
super.writeTo(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(available, enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
SpatialFeatureSetUsage other = (SpatialFeatureSetUsage) obj;
|
||||
return Objects.equals(available, other.available) &&
|
||||
Objects.equals(enabled, other.enabled);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* 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.core.spatial;
|
||||
|
||||
import org.elasticsearch.common.io.stream.Writeable;
|
||||
import org.elasticsearch.test.AbstractWireSerializingTestCase;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SpatialFeatureSetUsageTests extends AbstractWireSerializingTestCase<SpatialFeatureSetUsage> {
|
||||
|
||||
@Override
|
||||
protected SpatialFeatureSetUsage createTestInstance() {
|
||||
boolean available = randomBoolean();
|
||||
boolean enabled = randomBoolean();
|
||||
return new SpatialFeatureSetUsage(available, enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SpatialFeatureSetUsage mutateInstance(SpatialFeatureSetUsage instance) throws IOException {
|
||||
boolean available = instance.available();
|
||||
boolean enabled = instance.enabled();
|
||||
switch (between(0, 1)) {
|
||||
case 0:
|
||||
available = available == false;
|
||||
break;
|
||||
case 1:
|
||||
enabled = enabled == false;
|
||||
break;
|
||||
default:
|
||||
throw new AssertionError("Illegal randomisation branch");
|
||||
}
|
||||
return new SpatialFeatureSetUsage(available, enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Writeable.Reader<SpatialFeatureSetUsage> instanceReader() {
|
||||
return SpatialFeatureSetUsage::new;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
evaluationDependsOn(xpackModule('core'))
|
||||
|
||||
apply plugin: 'elasticsearch.esplugin'
|
||||
|
||||
esplugin {
|
||||
name 'spatial'
|
||||
description 'A plugin for Basic Spatial features'
|
||||
classname 'org.elasticsearch.xpack.spatial.SpatialPlugin'
|
||||
extendedPlugins = ['x-pack-core']
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly project(path: xpackModule('core'), configuration: 'default')
|
||||
testCompile project(path: xpackModule('core'), configuration: 'testArtifacts')
|
||||
if (isEclipse) {
|
||||
testCompile project(path: xpackModule('core-tests'), configuration: 'testArtifacts')
|
||||
}
|
||||
}
|
||||
|
||||
licenseHeaders {
|
||||
// This class was sourced from apache lucene's sandbox module tests
|
||||
excludes << 'org/apache/lucene/geo/XShapeTestUtil.java'
|
||||
}
|
||||
|
||||
// xpack modules are installed in real clusters as the meta plugin, so
|
||||
// installing them as individual plugins for integ tests doesn't make sense,
|
||||
// so we disable integ tests
|
||||
integTest.enabled = false
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.license.XPackLicenseState;
|
||||
import org.elasticsearch.xpack.core.XPackFeatureSet;
|
||||
import org.elasticsearch.xpack.core.XPackField;
|
||||
import org.elasticsearch.xpack.core.spatial.SpatialFeatureSetUsage;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class SpatialFeatureSet implements XPackFeatureSet {
|
||||
private final XPackLicenseState licenseState;
|
||||
|
||||
@Inject
|
||||
public SpatialFeatureSet(Settings settings, @Nullable XPackLicenseState licenseState) {
|
||||
this.licenseState = licenseState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return XPackField.SPATIAL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean available() {
|
||||
return licenseState != null && licenseState.isSpatialAllowed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean enabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> nativeCodeInfo() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void usage(ActionListener<XPackFeatureSet.Usage> listener) {
|
||||
listener.onResponse(new SpatialFeatureSetUsage(available(), enabled()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
import org.elasticsearch.common.inject.Module;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.mapper.Mapper;
|
||||
import org.elasticsearch.plugins.MapperPlugin;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.xpack.core.XPackPlugin;
|
||||
import org.elasticsearch.plugins.SearchPlugin;
|
||||
import org.elasticsearch.xpack.spatial.index.mapper.ShapeFieldMapper;
|
||||
import org.elasticsearch.xpack.spatial.index.query.ShapeQueryBuilder;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
|
||||
public class SpatialPlugin extends Plugin implements MapperPlugin, SearchPlugin {
|
||||
|
||||
public SpatialPlugin(Settings settings) {
|
||||
}
|
||||
|
||||
public Collection<Module> createGuiceModules() {
|
||||
return Collections.singletonList(b -> {
|
||||
XPackPlugin.bindFeatureSet(b, SpatialFeatureSet.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Mapper.TypeParser> getMappers() {
|
||||
Map<String, Mapper.TypeParser> mappers = new LinkedHashMap<>();
|
||||
mappers.put(ShapeFieldMapper.CONTENT_TYPE, new ShapeFieldMapper.TypeParser());
|
||||
return Collections.unmodifiableMap(mappers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<QuerySpec<?>> getQueries() {
|
||||
return singletonList(new QuerySpec<>(ShapeQueryBuilder.NAME, ShapeQueryBuilder::new, ShapeQueryBuilder::fromXContent));
|
||||
}
|
||||
}
|
|
@ -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.
|
||||
* <p>
|
||||
* Format supported:
|
||||
* <p>
|
||||
* "field" : {
|
||||
* "type" : "polygon",
|
||||
* "coordinates" : [
|
||||
* [ [1050.0, -1000.0], [1051.0, -1000.0], [1051.0, -1001.0], [1050.0, -1001.0], [1050.0, -1000.0] ]
|
||||
* ]
|
||||
* }
|
||||
* <p>
|
||||
* or:
|
||||
* <p>
|
||||
* "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<Geometry, Geometry> {
|
||||
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<AbstractGeometryFieldMapper.Builder, ShapeFieldMapper> {
|
||||
|
||||
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<String, Object> entry,
|
||||
Map<String, Object> params) throws MapperParsingException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder newBuilder(String name, Map<String, Object> 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<Geometry, Geometry> geometryIndexer() {
|
||||
return geometryIndexer;
|
||||
}
|
||||
}
|
||||
|
||||
public ShapeFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType,
|
||||
Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce,
|
||||
Explicit<Boolean> 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();
|
||||
}
|
||||
}
|
|
@ -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<Geometry, Geometry> {
|
||||
private final String name;
|
||||
|
||||
public ShapeIndexer(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Geometry prepareForIndexing(Geometry geometry) {
|
||||
return geometry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Geometry> processedClass() {
|
||||
return Geometry.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IndexableField> indexShape(ParseContext context, Geometry shape) {
|
||||
LuceneGeometryVisitor visitor = new LuceneGeometryVisitor(name);
|
||||
shape.visit(visitor);
|
||||
return visitor.fields;
|
||||
}
|
||||
|
||||
private class LuceneGeometryVisitor implements GeometryVisitor<Void, RuntimeException> {
|
||||
private List<IndexableField> 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<holes.length; i++) {
|
||||
ring = polygon.getHole(i);
|
||||
vertices = lineToFloatArray(ring.getLons(), ring.getLats());
|
||||
holes[i] = new XYPolygon(vertices[0], vertices[1]);
|
||||
}
|
||||
ring = polygon.getPolygon();
|
||||
vertices = lineToFloatArray(ring.getLons(), ring.getLats());
|
||||
return new XYPolygon(vertices[0], vertices[1], holes);
|
||||
}
|
||||
|
||||
private static float[][] lineToFloatArray(double[] x, double[] y) {
|
||||
float[][] result = new float[2][x.length];
|
||||
for (int i = 0; i < x.length; ++i) {
|
||||
result[0][i] = (float)x[i];
|
||||
result[1][i] = (float)y[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,213 @@
|
|||
/*
|
||||
* 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.logging.log4j.LogManager;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.geo.builders.ShapeBuilder;
|
||||
import org.elasticsearch.common.geo.parsers.ShapeParser;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.logging.DeprecationLogger;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.geo.geometry.Geometry;
|
||||
import org.elasticsearch.index.mapper.AbstractGeometryFieldMapper;
|
||||
import org.elasticsearch.index.mapper.MappedFieldType;
|
||||
import org.elasticsearch.index.query.AbstractGeometryQueryBuilder;
|
||||
import org.elasticsearch.index.query.GeoShapeQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryRewriteContext;
|
||||
import org.elasticsearch.index.query.QueryShardContext;
|
||||
import org.elasticsearch.index.query.QueryShardException;
|
||||
import org.elasticsearch.xpack.spatial.index.mapper.ShapeFieldMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Derived {@link AbstractGeometryQueryBuilder} that builds a {@code x, y} Shape Query
|
||||
*
|
||||
* GeoJson and WKT shape definitions are supported
|
||||
*/
|
||||
public class ShapeQueryBuilder extends AbstractGeometryQueryBuilder<ShapeQueryBuilder> {
|
||||
public static final String NAME = "shape";
|
||||
|
||||
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(
|
||||
LogManager.getLogger(GeoShapeQueryBuilder.class));
|
||||
|
||||
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Types are deprecated in [geo_shape] queries. " +
|
||||
"The type should no longer be specified in the [indexed_shape] section.";
|
||||
|
||||
/**
|
||||
* Creates a new GeoShapeQueryBuilder whose Query will be against the given
|
||||
* field name using the given Shape
|
||||
*
|
||||
* @param fieldName
|
||||
* Name of the field that will be queried
|
||||
* @param shape
|
||||
* Shape used in the Query
|
||||
* @deprecated use {@link #ShapeQueryBuilder(String, Geometry)} instead
|
||||
*/
|
||||
@Deprecated
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
protected ShapeQueryBuilder(String fieldName, ShapeBuilder shape) {
|
||||
super(fieldName, shape);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new GeoShapeQueryBuilder whose Query will be against the given
|
||||
* field name using the given Shape
|
||||
*
|
||||
* @param fieldName
|
||||
* Name of the field that will be queried
|
||||
* @param shape
|
||||
* Shape used in the Query
|
||||
*/
|
||||
public ShapeQueryBuilder(String fieldName, Geometry shape) {
|
||||
super(fieldName, shape);
|
||||
}
|
||||
|
||||
protected ShapeQueryBuilder(String fieldName, Supplier<Geometry> shapeSupplier, String indexedShapeId,
|
||||
@Nullable String indexedShapeType) {
|
||||
super(fieldName, shapeSupplier, indexedShapeId, indexedShapeType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new GeoShapeQueryBuilder whose Query will be against the given
|
||||
* field name and will use the Shape found with the given ID
|
||||
*
|
||||
* @param fieldName
|
||||
* Name of the field that will be filtered
|
||||
* @param indexedShapeId
|
||||
* ID of the indexed Shape that will be used in the Query
|
||||
*/
|
||||
public ShapeQueryBuilder(String fieldName, String indexedShapeId) {
|
||||
super(fieldName, indexedShapeId);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
protected ShapeQueryBuilder(String fieldName, String indexedShapeId, String indexedShapeType) {
|
||||
super(fieldName, (Geometry) null, indexedShapeId, indexedShapeType);
|
||||
}
|
||||
|
||||
public ShapeQueryBuilder(StreamInput in) throws IOException {
|
||||
super(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doWriteTo(StreamOutput out) throws IOException {
|
||||
super.doWriteTo(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ShapeQueryBuilder newShapeQueryBuilder(String fieldName, Geometry shape) {
|
||||
return new ShapeQueryBuilder(fieldName, shape);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ShapeQueryBuilder newShapeQueryBuilder(String fieldName, Supplier<Geometry> shapeSupplier, String indexedShapeId,
|
||||
String indexedShapeType) {
|
||||
return new ShapeQueryBuilder(fieldName, shapeSupplier, indexedShapeId, indexedShapeType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String queryFieldType() {
|
||||
return ShapeFieldMapper.CONTENT_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
protected List validContentTypes() {
|
||||
return Arrays.asList(ShapeFieldMapper.CONTENT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
public Query buildShapeQuery(QueryShardContext context, MappedFieldType fieldType) {
|
||||
if (fieldType.typeName().equals(ShapeFieldMapper.CONTENT_TYPE) == false) {
|
||||
throw new QueryShardException(context,
|
||||
"Field [" + fieldName + "] is not of type [" + queryFieldType() + "] but of type [" + fieldType.typeName() + "]");
|
||||
}
|
||||
|
||||
final AbstractGeometryFieldMapper.AbstractGeometryFieldType ft = (AbstractGeometryFieldMapper.AbstractGeometryFieldType) fieldType;
|
||||
return ft.geometryQueryBuilder().process(shape, ft.name(), relation, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doShapeQueryXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
// noop
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ShapeQueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
|
||||
return (ShapeQueryBuilder)super.doRewrite(queryRewriteContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean doEquals(ShapeQueryBuilder other) {
|
||||
return super.doEquals((AbstractGeometryQueryBuilder)other);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int doHashCode() {
|
||||
return Objects.hash(super.doHashCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getWriteableName() {
|
||||
return NAME;
|
||||
}
|
||||
|
||||
private static class ParsedShapeQueryParams extends ParsedGeometryQueryParams {
|
||||
@Override
|
||||
protected boolean parseXContentField(XContentParser parser) throws IOException {
|
||||
if (SHAPE_FIELD.match(parser.currentName(), parser.getDeprecationHandler())) {
|
||||
this.shape = ShapeParser.parse(parser);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static ShapeQueryBuilder fromXContent(XContentParser parser) throws IOException {
|
||||
ParsedShapeQueryParams pgsqb = (ParsedShapeQueryParams)AbstractGeometryQueryBuilder.parsedParamsFromXContent(parser,
|
||||
new ParsedShapeQueryParams());
|
||||
|
||||
ShapeQueryBuilder builder;
|
||||
if (pgsqb.type != null) {
|
||||
deprecationLogger.deprecatedAndMaybeLog(
|
||||
"geo_share_query_with_types", TYPES_DEPRECATION_MESSAGE);
|
||||
}
|
||||
|
||||
if (pgsqb.shape != null) {
|
||||
builder = new ShapeQueryBuilder(pgsqb.fieldName, pgsqb.shape);
|
||||
} else {
|
||||
builder = new ShapeQueryBuilder(pgsqb.fieldName, pgsqb.id, pgsqb.type);
|
||||
}
|
||||
if (pgsqb.index != null) {
|
||||
builder.indexedShapeIndex(pgsqb.index);
|
||||
}
|
||||
if (pgsqb.shapePath != null) {
|
||||
builder.indexedShapePath(pgsqb.shapePath);
|
||||
}
|
||||
if (pgsqb.shapeRouting != null) {
|
||||
builder.indexedShapeRouting(pgsqb.shapeRouting);
|
||||
}
|
||||
if (pgsqb.relation != null) {
|
||||
builder.relation(pgsqb.relation);
|
||||
}
|
||||
if (pgsqb.queryName != null) {
|
||||
builder.queryName(pgsqb.queryName);
|
||||
}
|
||||
builder.boost(pgsqb.boost);
|
||||
builder.ignoreUnmapped(pgsqb.ignoreUnmapped);
|
||||
return builder;
|
||||
}
|
||||
}
|
|
@ -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.ShapeIndexer.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<Query, RuntimeException> {
|
||||
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<multiLine.size(); i++) {
|
||||
lines[i] = new XYLine(doubleArrayToFloatArray(multiLine.get(i).getLons()),
|
||||
doubleArrayToFloatArray(multiLine.get(i).getLats()));
|
||||
}
|
||||
return XYShape.newLineQuery(fieldName, relation.getLuceneRelation(), lines);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query visit(MultiPoint multiPoint) {
|
||||
throw new QueryShardException(context, "Field [" + fieldName + "] does not support " + GeoShapeType.MULTIPOINT +
|
||||
" queries");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query visit(MultiPolygon multiPolygon) {
|
||||
XYPolygon[] polygons = new XYPolygon[multiPolygon.size()];
|
||||
for (int i=0; i<multiPolygon.size(); i++) {
|
||||
polygons[i] = toLucenePolygon(multiPolygon.get(i));
|
||||
}
|
||||
return visitMultiPolygon(polygons);
|
||||
}
|
||||
|
||||
private Query visitMultiPolygon(XYPolygon... polygons) {
|
||||
return XYShape.newPolygonQuery(fieldName, relation.getLuceneRelation(), polygons);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query visit(Point point) {
|
||||
return XYShape.newBoxQuery(fieldName, relation.getLuceneRelation(),
|
||||
(float)point.getLon(), (float)point.getLon(), (float)point.getLat(), (float)point.getLat());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query visit(org.elasticsearch.geo.geometry.Polygon polygon) {
|
||||
return XYShape.newPolygonQuery(fieldName, relation.getLuceneRelation(), toLucenePolygon(polygon));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query visit(org.elasticsearch.geo.geometry.Rectangle r) {
|
||||
return XYShape.newBoxQuery(fieldName, relation.getLuceneRelation(),
|
||||
(float)r.getMinLon(), (float)r.getMaxLon(), (float)r.getMinLat(), (float)r.getMaxLat());
|
||||
}
|
||||
}
|
||||
|
||||
private static float[] doubleArrayToFloatArray(double[] array) {
|
||||
float[] result = new float[array.length];
|
||||
for (int i = 0; i < array.length; ++i) {
|
||||
result[i] = (float) array[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,208 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.geo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
import com.carrotsearch.randomizedtesting.RandomizedContext;
|
||||
import com.carrotsearch.randomizedtesting.generators.BiasedNumbers;
|
||||
import org.apache.lucene.util.SloppyMath;
|
||||
import org.apache.lucene.util.TestUtil;
|
||||
|
||||
/** generates random cartesian geometry; heavy reuse of {@link GeoTestUtil} */
|
||||
public class XShapeTestUtil {
|
||||
|
||||
/** returns next pseudorandom polygon */
|
||||
public static XYPolygon nextPolygon() {
|
||||
if (random().nextBoolean()) {
|
||||
return surpriseMePolygon();
|
||||
} else if (random().nextInt(10) == 1) {
|
||||
// this poly is slow to create ... only do it 10% of the time:
|
||||
while (true) {
|
||||
int gons = TestUtil.nextInt(random(), 4, 500);
|
||||
// So the poly can cover at most 50% of the earth's surface:
|
||||
double radius = random().nextDouble() * 0.5 * Float.MAX_VALUE + 1.0;
|
||||
try {
|
||||
return createRegularPolygon(nextDouble(), nextDouble(), radius, gons);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
// we tried to cross dateline or pole ... try again
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
XYRectangle box = nextBoxInternal();
|
||||
if (random().nextBoolean()) {
|
||||
// box
|
||||
return boxPolygon(box);
|
||||
} else {
|
||||
// triangle
|
||||
return trianglePolygon(box);
|
||||
}
|
||||
}
|
||||
|
||||
private static XYPolygon trianglePolygon(XYRectangle box) {
|
||||
final float[] polyX = new float[4];
|
||||
final float[] polyY = new float[4];
|
||||
polyX[0] = (float)box.minX;
|
||||
polyY[0] = (float)box.minY;
|
||||
polyX[1] = (float)box.minX;
|
||||
polyY[1] = (float)box.minY;
|
||||
polyX[2] = (float)box.minX;
|
||||
polyY[2] = (float)box.minY;
|
||||
polyX[3] = (float)box.minX;
|
||||
polyY[3] = (float)box.minY;
|
||||
return new XYPolygon(polyX, polyY);
|
||||
}
|
||||
|
||||
public static XYRectangle nextBox() {
|
||||
return nextBoxInternal();
|
||||
}
|
||||
|
||||
private static XYRectangle nextBoxInternal() {
|
||||
// prevent lines instead of boxes
|
||||
double x0 = nextDouble();
|
||||
double x1 = nextDouble();
|
||||
while (x0 == x1) {
|
||||
x1 = nextDouble();
|
||||
}
|
||||
// prevent lines instead of boxes
|
||||
double y0 = nextDouble();
|
||||
double y1 = nextDouble();
|
||||
while (y0 == y1) {
|
||||
y1 = nextDouble();
|
||||
}
|
||||
|
||||
if (x1 < x0) {
|
||||
double x = x0;
|
||||
x0 = x1;
|
||||
x1 = x;
|
||||
}
|
||||
|
||||
if (y1 < y0) {
|
||||
double y = y0;
|
||||
y0 = y1;
|
||||
y1 = y;
|
||||
}
|
||||
|
||||
return new XYRectangle(x0, x1, y0, y1);
|
||||
}
|
||||
|
||||
private static XYPolygon boxPolygon(XYRectangle box) {
|
||||
final float[] polyX = new float[5];
|
||||
final float[] polyY = new float[5];
|
||||
polyX[0] = (float)box.minX;
|
||||
polyY[0] = (float)box.minY;
|
||||
polyX[1] = (float)box.minX;
|
||||
polyY[1] = (float)box.minY;
|
||||
polyX[2] = (float)box.minX;
|
||||
polyY[2] = (float)box.minY;
|
||||
polyX[3] = (float)box.minX;
|
||||
polyY[3] = (float)box.minY;
|
||||
polyX[4] = (float)box.minX;
|
||||
polyY[4] = (float)box.minY;
|
||||
return new XYPolygon(polyX, polyY);
|
||||
}
|
||||
|
||||
private static XYPolygon surpriseMePolygon() {
|
||||
// repeat until we get a poly that doesn't cross dateline:
|
||||
while (true) {
|
||||
//System.out.println("\nPOLY ITER");
|
||||
double centerX = nextDouble();
|
||||
double centerY = nextDouble();
|
||||
double radius = 0.1 + 20 * random().nextDouble();
|
||||
double radiusDelta = random().nextDouble();
|
||||
|
||||
ArrayList<Float> xList = new ArrayList<>();
|
||||
ArrayList<Float> yList = new ArrayList<>();
|
||||
double angle = 0.0;
|
||||
while (true) {
|
||||
angle += random().nextDouble()*40.0;
|
||||
//System.out.println(" angle " + angle);
|
||||
if (angle > 360) {
|
||||
break;
|
||||
}
|
||||
double len = radius * (1.0 - radiusDelta + radiusDelta * random().nextDouble());
|
||||
double maxX = StrictMath.min(StrictMath.abs(Float.MAX_VALUE - centerX), StrictMath.abs(-Float.MAX_VALUE - centerX));
|
||||
double maxY = StrictMath.min(StrictMath.abs(Float.MAX_VALUE - centerY), StrictMath.abs(-Float.MAX_VALUE - centerY));
|
||||
|
||||
len = StrictMath.min(len, StrictMath.min(maxX, maxY));
|
||||
|
||||
//System.out.println(" len=" + len);
|
||||
float x = (float)(centerX + len * Math.cos(SloppyMath.toRadians(angle)));
|
||||
float y = (float)(centerY + len * Math.sin(SloppyMath.toRadians(angle)));
|
||||
|
||||
xList.add(x);
|
||||
yList.add(y);
|
||||
|
||||
//System.out.println(" lat=" + lats.get(lats.size()-1) + " lon=" + lons.get(lons.size()-1));
|
||||
}
|
||||
|
||||
// close it
|
||||
xList.add(xList.get(0));
|
||||
yList.add(yList.get(0));
|
||||
|
||||
float[] xArray = new float[xList.size()];
|
||||
float[] yArray = new float[yList.size()];
|
||||
for(int i=0;i<xList.size();i++) {
|
||||
xArray[i] = xList.get(i);
|
||||
yArray[i] = yList.get(i);
|
||||
}
|
||||
return new XYPolygon(xArray, yArray);
|
||||
}
|
||||
}
|
||||
|
||||
/** Makes an n-gon, centered at the provided x/y, and each vertex approximately
|
||||
* distanceMeters away from the center.
|
||||
*
|
||||
* Do not invoke me across the dateline or a pole!! */
|
||||
public static XYPolygon createRegularPolygon(double centerX, double centerY, double radius, int gons) {
|
||||
|
||||
double maxX = StrictMath.min(StrictMath.abs(Float.MAX_VALUE - centerX), StrictMath.abs(-Float.MAX_VALUE - centerX));
|
||||
double maxY = StrictMath.min(StrictMath.abs(Float.MAX_VALUE - centerY), StrictMath.abs(-Float.MAX_VALUE - centerY));
|
||||
|
||||
radius = StrictMath.min(radius, StrictMath.min(maxX, maxY));
|
||||
|
||||
float[][] result = new float[2][];
|
||||
result[0] = new float[gons+1];
|
||||
result[1] = new float[gons+1];
|
||||
//System.out.println("make gon=" + gons);
|
||||
for(int i=0;i<gons;i++) {
|
||||
double angle = 360.0-i*(360.0/gons);
|
||||
//System.out.println(" angle " + angle);
|
||||
double x = Math.cos(StrictMath.toRadians(angle));
|
||||
double y = Math.sin(StrictMath.toRadians(angle));
|
||||
result[0][i] = (float)(centerY + y * radius);
|
||||
result[1][i] = (float)(centerX + x * radius);
|
||||
}
|
||||
|
||||
// close poly
|
||||
result[0][gons] = result[0][0];
|
||||
result[1][gons] = result[1][0];
|
||||
|
||||
return new XYPolygon(result[0], result[1]);
|
||||
}
|
||||
|
||||
public static double nextDouble() {
|
||||
return BiasedNumbers.randomDoubleBetween(random(), -Float.MAX_VALUE, Float.MAX_VALUE);
|
||||
}
|
||||
|
||||
/** Keep it simple, we don't need to take arbitrary Random for geo tests */
|
||||
private static Random random() {
|
||||
return RandomizedContext.current().getRandom();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
import org.elasticsearch.action.support.PlainActionFuture;
|
||||
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.license.XPackLicenseState;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.xpack.core.XPackFeatureSet;
|
||||
import org.elasticsearch.xpack.core.spatial.SpatialFeatureSetUsage;
|
||||
import org.junit.Before;
|
||||
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class SpatialFeatureSetTests extends ESTestCase {
|
||||
private XPackLicenseState licenseState;
|
||||
|
||||
@Before
|
||||
public void init() throws Exception {
|
||||
licenseState = mock(XPackLicenseState.class);
|
||||
}
|
||||
|
||||
public void testAvailable() throws Exception {
|
||||
SpatialFeatureSet featureSet = new SpatialFeatureSet(Settings.EMPTY, licenseState);
|
||||
boolean available = randomBoolean();
|
||||
when(licenseState.isSpatialAllowed()).thenReturn(available);
|
||||
assertThat(featureSet.available(), is(available));
|
||||
PlainActionFuture<XPackFeatureSet.Usage> future = new PlainActionFuture<>();
|
||||
featureSet.usage(future);
|
||||
XPackFeatureSet.Usage usage = future.get();
|
||||
assertThat(usage.available(), is(available));
|
||||
|
||||
BytesStreamOutput out = new BytesStreamOutput();
|
||||
usage.writeTo(out);
|
||||
XPackFeatureSet.Usage serializedUsage = new SpatialFeatureSetUsage(out.bytes().streamInput());
|
||||
assertThat(serializedUsage.available(), is(available));
|
||||
}
|
||||
|
||||
public void testEnabled() throws Exception {
|
||||
boolean enabled = true;
|
||||
Settings.Builder settings = Settings.builder();
|
||||
if (randomBoolean()) {
|
||||
settings.put("xpack.spatial.enabled", enabled);
|
||||
}
|
||||
SpatialFeatureSet featureSet = new SpatialFeatureSet(settings.build(), licenseState);
|
||||
assertThat(featureSet.enabled(), is(enabled));
|
||||
PlainActionFuture<XPackFeatureSet.Usage> future = new PlainActionFuture<>();
|
||||
featureSet.usage(future);
|
||||
XPackFeatureSet.Usage usage = future.get();
|
||||
assertThat(usage.enabled(), is(enabled));
|
||||
|
||||
BytesStreamOutput out = new BytesStreamOutput();
|
||||
usage.writeTo(out);
|
||||
XPackFeatureSet.Usage serializedUsage = new SpatialFeatureSetUsage(out.bytes().streamInput());
|
||||
assertThat(serializedUsage.enabled(), is(enabled));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,297 @@
|
|||
/*
|
||||
* 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.elasticsearch.common.Explicit;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.compress.CompressedXContent;
|
||||
import org.elasticsearch.common.geo.builders.ShapeBuilder;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.index.mapper.AbstractGeometryFieldMapper;
|
||||
import org.elasticsearch.index.mapper.DocumentMapper;
|
||||
import org.elasticsearch.index.mapper.DocumentMapperParser;
|
||||
import org.elasticsearch.index.mapper.Mapper;
|
||||
import org.elasticsearch.index.mapper.MapperService;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.test.ESSingleNodeTestCase;
|
||||
import org.elasticsearch.test.InternalSettingsPlugin;
|
||||
import org.elasticsearch.xpack.core.XPackPlugin;
|
||||
import org.elasticsearch.xpack.spatial.SpatialPlugin;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.elasticsearch.index.mapper.GeoPointFieldMapper.Names.IGNORE_Z_VALUE;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
|
||||
/** testing for {@link org.elasticsearch.xpack.spatial.index.mapper.ShapeFieldMapper} */
|
||||
public class ShapeFieldMapperTests extends ESSingleNodeTestCase {
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> 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<Boolean> 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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,291 @@
|
|||
/*
|
||||
* 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.search.BooleanQuery;
|
||||
import org.apache.lucene.search.ConstantScoreQuery;
|
||||
import org.apache.lucene.search.MatchNoDocsQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
|
||||
import org.elasticsearch.action.get.GetRequest;
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.bytes.BytesArray;
|
||||
import org.elasticsearch.common.compress.CompressedXContent;
|
||||
import org.elasticsearch.common.geo.GeoJson;
|
||||
import org.elasticsearch.common.geo.ShapeRelation;
|
||||
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
||||
import org.elasticsearch.common.xcontent.ToXContentObject;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.geo.geometry.Geometry;
|
||||
import org.elasticsearch.geo.geometry.ShapeType;
|
||||
import org.elasticsearch.index.get.GetResult;
|
||||
import org.elasticsearch.index.mapper.MapperService;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryShardException;
|
||||
import org.elasticsearch.index.query.Rewriteable;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.search.internal.SearchContext;
|
||||
import org.elasticsearch.test.AbstractQueryTestCase;
|
||||
import org.elasticsearch.xpack.spatial.SpatialPlugin;
|
||||
import org.elasticsearch.xpack.spatial.util.ShapeTestUtils;
|
||||
import org.junit.After;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.anyOf;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
public class ShapeQueryBuilderTests extends AbstractQueryTestCase<ShapeQueryBuilder> {
|
||||
|
||||
protected static final String SHAPE_FIELD_NAME = "mapped_shape";
|
||||
|
||||
private static String docType = "_doc";
|
||||
|
||||
protected static String indexedShapeId;
|
||||
protected static String indexedShapeType;
|
||||
protected static String indexedShapePath;
|
||||
protected static String indexedShapeIndex;
|
||||
protected static String indexedShapeRouting;
|
||||
protected static Geometry indexedShapeToReturn;
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> getPlugins() {
|
||||
return Collections.singleton(SpatialPlugin.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
|
||||
mapperService.merge(docType, new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef(docType,
|
||||
fieldName(), "type=shape"))), MapperService.MergeReason.MAPPING_UPDATE);
|
||||
}
|
||||
|
||||
protected String fieldName() {
|
||||
return SHAPE_FIELD_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ShapeQueryBuilder doCreateTestQueryBuilder() {
|
||||
return doCreateTestQueryBuilder(randomBoolean());
|
||||
}
|
||||
|
||||
protected ShapeQueryBuilder doCreateTestQueryBuilder(boolean indexedShape) {
|
||||
Geometry shape;
|
||||
// multipoint queries not (yet) supported
|
||||
do {
|
||||
shape = ShapeTestUtils.randomGeometry(false);
|
||||
} while (shape.type() == ShapeType.MULTIPOINT || shape.type() == ShapeType.GEOMETRYCOLLECTION);
|
||||
|
||||
ShapeQueryBuilder builder;
|
||||
clearShapeFields();
|
||||
if (indexedShape == false) {
|
||||
builder = new ShapeQueryBuilder(fieldName(), shape);
|
||||
} else {
|
||||
indexedShapeToReturn = shape;
|
||||
indexedShapeId = randomAlphaOfLengthBetween(3, 20);
|
||||
indexedShapeType = randomBoolean() ? randomAlphaOfLengthBetween(3, 20) : null;
|
||||
builder = new ShapeQueryBuilder(fieldName(), indexedShapeId, indexedShapeType);
|
||||
if (randomBoolean()) {
|
||||
indexedShapeIndex = randomAlphaOfLengthBetween(3, 20);
|
||||
builder.indexedShapeIndex(indexedShapeIndex);
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
indexedShapePath = randomAlphaOfLengthBetween(3, 20);
|
||||
builder.indexedShapePath(indexedShapePath);
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
indexedShapeRouting = randomAlphaOfLengthBetween(3, 20);
|
||||
builder.indexedShapeRouting(indexedShapeRouting);
|
||||
}
|
||||
}
|
||||
|
||||
if (shape.type() == ShapeType.LINESTRING || shape.type() == ShapeType.MULTILINESTRING) {
|
||||
builder.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS));
|
||||
} else {
|
||||
// XYShape does not support CONTAINS:
|
||||
builder.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS, ShapeRelation.WITHIN));
|
||||
}
|
||||
|
||||
if (randomBoolean()) {
|
||||
builder.ignoreUnmapped(randomBoolean());
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
@After
|
||||
public void clearShapeFields() {
|
||||
indexedShapeToReturn = null;
|
||||
indexedShapeId = null;
|
||||
indexedShapeType = null;
|
||||
indexedShapePath = null;
|
||||
indexedShapeIndex = null;
|
||||
indexedShapeRouting = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doAssertLuceneQuery(ShapeQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
|
||||
// Logic for doToQuery is complex and is hard to test here. Need to rely
|
||||
// on Integration tests to determine if created query is correct
|
||||
// TODO improve ShapeQueryBuilder.doToQuery() method to make it
|
||||
// easier to test here
|
||||
assertThat(query, anyOf(instanceOf(BooleanQuery.class), instanceOf(ConstantScoreQuery.class)));
|
||||
}
|
||||
|
||||
public void testNoFieldName() {
|
||||
Geometry shape = ShapeTestUtils.randomGeometry(false);
|
||||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new ShapeQueryBuilder(null, shape));
|
||||
assertEquals("fieldName is required", e.getMessage());
|
||||
}
|
||||
|
||||
public void testNoShape() {
|
||||
expectThrows(IllegalArgumentException.class, () -> new ShapeQueryBuilder(fieldName(), (Geometry) null));
|
||||
}
|
||||
|
||||
public void testNoIndexedShape() {
|
||||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
|
||||
() -> new ShapeQueryBuilder(fieldName(), null, "type"));
|
||||
assertEquals("either shape or indexedShapeId is required", e.getMessage());
|
||||
}
|
||||
|
||||
public void testNoRelation() {
|
||||
Geometry shape = ShapeTestUtils.randomGeometry(false);
|
||||
ShapeQueryBuilder builder = new ShapeQueryBuilder(fieldName(), shape);
|
||||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> builder.relation(null));
|
||||
assertEquals("No Shape Relation defined", e.getMessage());
|
||||
}
|
||||
|
||||
public void testFromJson() throws IOException {
|
||||
String json =
|
||||
"{\n" +
|
||||
" \"shape\" : {\n" +
|
||||
" \"geometry\" : {\n" +
|
||||
" \"shape\" : {\n" +
|
||||
" \"type\" : \"envelope\",\n" +
|
||||
" \"coordinates\" : [ [ 1300.0, 5300.0 ], [ 1400.0, 5200.0 ] ]\n" +
|
||||
" },\n" +
|
||||
" \"relation\" : \"intersects\"\n" +
|
||||
" },\n" +
|
||||
" \"ignore_unmapped\" : false,\n" +
|
||||
" \"boost\" : 42.0\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
ShapeQueryBuilder parsed = (ShapeQueryBuilder) parseQuery(json);
|
||||
checkGeneratedJson(json, parsed);
|
||||
assertEquals(json, 42.0, parsed.boost(), 0.0001);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testMustRewrite() {
|
||||
ShapeQueryBuilder query = doCreateTestQueryBuilder(true);
|
||||
|
||||
UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> query.toQuery(createShardContext()));
|
||||
assertEquals("query must be rewritten first", e.getMessage());
|
||||
QueryBuilder rewrite = rewriteAndFetch(query, createShardContext());
|
||||
ShapeQueryBuilder geoShapeQueryBuilder = new ShapeQueryBuilder(fieldName(), indexedShapeToReturn);
|
||||
geoShapeQueryBuilder.relation(query.relation());
|
||||
assertEquals(geoShapeQueryBuilder, rewrite);
|
||||
}
|
||||
|
||||
public void testMultipleRewrite() {
|
||||
ShapeQueryBuilder shape = doCreateTestQueryBuilder(true);
|
||||
QueryBuilder builder = new BoolQueryBuilder()
|
||||
.should(shape)
|
||||
.should(shape);
|
||||
|
||||
builder = rewriteAndFetch(builder, createShardContext());
|
||||
|
||||
ShapeQueryBuilder expectedShape = new ShapeQueryBuilder(fieldName(), indexedShapeToReturn);
|
||||
expectedShape.relation(shape.relation());
|
||||
QueryBuilder expected = new BoolQueryBuilder()
|
||||
.should(expectedShape)
|
||||
.should(expectedShape);
|
||||
assertEquals(expected, builder);
|
||||
}
|
||||
|
||||
public void testIgnoreUnmapped() throws IOException {
|
||||
Geometry shape = ShapeTestUtils.randomGeometry(false);
|
||||
final ShapeQueryBuilder queryBuilder = new ShapeQueryBuilder("unmapped", shape);
|
||||
queryBuilder.ignoreUnmapped(true);
|
||||
Query query = queryBuilder.toQuery(createShardContext());
|
||||
assertThat(query, notNullValue());
|
||||
assertThat(query, instanceOf(MatchNoDocsQuery.class));
|
||||
|
||||
final ShapeQueryBuilder failingQueryBuilder = new ShapeQueryBuilder("unmapped", shape);
|
||||
failingQueryBuilder.ignoreUnmapped(false);
|
||||
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext()));
|
||||
assertThat(e.getMessage(), containsString("failed to find shape field [unmapped]"));
|
||||
}
|
||||
|
||||
public void testWrongFieldType() {
|
||||
Geometry shape = ShapeTestUtils.randomGeometry(false);
|
||||
final ShapeQueryBuilder queryBuilder = new ShapeQueryBuilder(STRING_FIELD_NAME, shape);
|
||||
QueryShardException e = expectThrows(QueryShardException.class, () -> queryBuilder.toQuery(createShardContext()));
|
||||
assertThat(e.getMessage(), containsString("Field [mapped_string] is not of type [shape] but of type [text]"));
|
||||
}
|
||||
|
||||
public void testSerializationFailsUnlessFetched() throws IOException {
|
||||
QueryBuilder builder = doCreateTestQueryBuilder(true);
|
||||
QueryBuilder queryBuilder = Rewriteable.rewrite(builder, createShardContext());
|
||||
IllegalStateException ise = expectThrows(IllegalStateException.class, () -> queryBuilder.writeTo(new BytesStreamOutput(10)));
|
||||
assertEquals(ise.getMessage(), "supplier must be null, can't serialize suppliers, missing a rewriteAndFetch?");
|
||||
builder = rewriteAndFetch(builder, createShardContext());
|
||||
builder.writeTo(new BytesStreamOutput(10));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected QueryBuilder parseQuery(XContentParser parser) throws IOException {
|
||||
QueryBuilder query = super.parseQuery(parser);
|
||||
assertThat(query, instanceOf(ShapeQueryBuilder.class));
|
||||
|
||||
ShapeQueryBuilder shapeQuery = (ShapeQueryBuilder) query;
|
||||
if (shapeQuery.indexedShapeType() != null) {
|
||||
assertWarnings(ShapeQueryBuilder.TYPES_DEPRECATION_MESSAGE);
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GetResponse executeGet(GetRequest getRequest) {
|
||||
String indexedType = indexedShapeType != null ? indexedShapeType : MapperService.SINGLE_MAPPING_NAME;
|
||||
|
||||
assertThat(indexedShapeToReturn, notNullValue());
|
||||
assertThat(indexedShapeId, notNullValue());
|
||||
assertThat(getRequest.id(), equalTo(indexedShapeId));
|
||||
assertThat(getRequest.type(), equalTo(indexedType));
|
||||
assertThat(getRequest.routing(), equalTo(indexedShapeRouting));
|
||||
String expectedShapeIndex = indexedShapeIndex == null ? ShapeQueryBuilder.DEFAULT_SHAPE_INDEX_NAME : indexedShapeIndex;
|
||||
assertThat(getRequest.index(), equalTo(expectedShapeIndex));
|
||||
String expectedShapePath = indexedShapePath == null ? ShapeQueryBuilder.DEFAULT_SHAPE_FIELD_NAME : indexedShapePath;
|
||||
|
||||
String json;
|
||||
try {
|
||||
XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
|
||||
builder.startObject();
|
||||
builder.field(expectedShapePath, new ToXContentObject() {
|
||||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
return GeoJson.toXContent(indexedShapeToReturn, builder, null);
|
||||
}
|
||||
});
|
||||
builder.field(randomAlphaOfLengthBetween(10, 20), "something");
|
||||
builder.endObject();
|
||||
json = Strings.toString(builder);
|
||||
} catch (IOException ex) {
|
||||
throw new ElasticsearchException("boom", ex);
|
||||
}
|
||||
return new GetResponse(new GetResult(indexedShapeIndex, indexedType, indexedShapeId, 0, 1, 0, true, new BytesArray(json),
|
||||
null, null));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,236 @@
|
|||
/*
|
||||
* 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.search;
|
||||
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.geo.GeoJson;
|
||||
import org.elasticsearch.common.geo.ShapeRelation;
|
||||
import org.elasticsearch.common.geo.builders.EnvelopeBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.geo.geometry.Geometry;
|
||||
import org.elasticsearch.geo.geometry.ShapeType;
|
||||
import org.elasticsearch.index.query.ExistsQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.test.ESSingleNodeTestCase;
|
||||
import org.elasticsearch.xpack.core.XPackPlugin;
|
||||
import org.elasticsearch.xpack.spatial.SpatialPlugin;
|
||||
import org.elasticsearch.xpack.spatial.index.query.ShapeQueryBuilder;
|
||||
import org.elasticsearch.xpack.spatial.util.ShapeTestUtils;
|
||||
import org.locationtech.jts.geom.Coordinate;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
public class ShapeQueryTests extends ESSingleNodeTestCase {
|
||||
|
||||
private static String INDEX = "test";
|
||||
private static String IGNORE_MALFORMED_INDEX = INDEX + "_ignore_malformed";
|
||||
private static String FIELD_TYPE = "geometry";
|
||||
private static String FIELD = "shape";
|
||||
private static Geometry queryGeometry = null;
|
||||
|
||||
private int numDocs;
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
// create test index
|
||||
assertAcked(client().admin().indices().prepareCreate(INDEX)
|
||||
.addMapping(FIELD_TYPE, FIELD, "type=shape", "alias", "type=alias,path=" + FIELD).get());
|
||||
// create index that ignores malformed geometry
|
||||
assertAcked(client().admin().indices().prepareCreate(IGNORE_MALFORMED_INDEX)
|
||||
.addMapping(FIELD_TYPE, FIELD, "type=shape,ignore_malformed=true", "_source", "enabled=false").get());
|
||||
ensureGreen();
|
||||
|
||||
// index random shapes
|
||||
numDocs = randomIntBetween(25, 50);
|
||||
Geometry geometry;
|
||||
for (int i = 0; i < numDocs; ++i) {
|
||||
geometry = ShapeTestUtils.randomGeometry(false);
|
||||
if (geometry.type() == ShapeType.CIRCLE) continue;
|
||||
if (queryGeometry == null && geometry.type() != ShapeType.MULTIPOINT) {
|
||||
queryGeometry = geometry;
|
||||
}
|
||||
XContentBuilder geoJson = GeoJson.toXContent(geometry, XContentFactory.jsonBuilder()
|
||||
.startObject().field(FIELD), null).endObject();
|
||||
|
||||
try {
|
||||
client().prepareIndex(INDEX, FIELD_TYPE).setSource(geoJson).setRefreshPolicy(IMMEDIATE).get();
|
||||
client().prepareIndex(IGNORE_MALFORMED_INDEX, FIELD_TYPE).setRefreshPolicy(IMMEDIATE).setSource(geoJson).get();
|
||||
} catch (Exception e) {
|
||||
// sometimes GeoTestUtil will create invalid geometry; catch and continue:
|
||||
--i;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testIndexedShapeReferenceSourceDisabled() throws Exception {
|
||||
EnvelopeBuilder shape = new EnvelopeBuilder(new Coordinate(-45, 45), new Coordinate(45, -45));
|
||||
|
||||
client().prepareIndex(IGNORE_MALFORMED_INDEX, FIELD_TYPE, "Big_Rectangle").setSource(jsonBuilder().startObject()
|
||||
.field(FIELD, shape).endObject()).setRefreshPolicy(IMMEDIATE).get();
|
||||
|
||||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> client().prepareSearch(IGNORE_MALFORMED_INDEX)
|
||||
.setQuery(new ShapeQueryBuilder(FIELD, "Big_Rectangle").indexedShapeIndex(IGNORE_MALFORMED_INDEX)).get());
|
||||
assertThat(e.getMessage(), containsString("source disabled"));
|
||||
}
|
||||
|
||||
public void testShapeFetchingPath() throws Exception {
|
||||
String indexName = "shapes_index";
|
||||
String searchIndex = "search_index";
|
||||
createIndex(indexName);
|
||||
client().admin().indices().prepareCreate(searchIndex).addMapping("type", "location", "type=shape").get();
|
||||
|
||||
String location = "\"location\" : {\"type\":\"polygon\", \"coordinates\":[[[-10,-10],[10,-10],[10,10],[-10,10],[-10,-10]]]}";
|
||||
|
||||
client().prepareIndex(indexName, "type", "1")
|
||||
.setSource(
|
||||
String.format(
|
||||
Locale.ROOT, "{ %s, \"1\" : { %s, \"2\" : { %s, \"3\" : { %s } }} }", location, location, location, location
|
||||
), XContentType.JSON)
|
||||
.setRefreshPolicy(IMMEDIATE).get();
|
||||
client().prepareIndex(searchIndex, "type", "1")
|
||||
.setSource(jsonBuilder().startObject().startObject("location")
|
||||
.field("type", "polygon")
|
||||
.startArray("coordinates").startArray()
|
||||
.startArray().value(-20).value(-20).endArray()
|
||||
.startArray().value(20).value(-20).endArray()
|
||||
.startArray().value(20).value(20).endArray()
|
||||
.startArray().value(-20).value(20).endArray()
|
||||
.startArray().value(-20).value(-20).endArray()
|
||||
.endArray().endArray()
|
||||
.endObject().endObject()).setRefreshPolicy(IMMEDIATE).get();
|
||||
|
||||
ShapeQueryBuilder filter = new ShapeQueryBuilder("location", "1").relation(ShapeRelation.INTERSECTS)
|
||||
.indexedShapeIndex(indexName)
|
||||
.indexedShapePath("location");
|
||||
SearchResponse result = client().prepareSearch(searchIndex).setQuery(QueryBuilders.matchAllQuery())
|
||||
.setPostFilter(filter).get();
|
||||
assertSearchResponse(result);
|
||||
assertHitCount(result, 1);
|
||||
filter = new ShapeQueryBuilder("location", "1").relation(ShapeRelation.INTERSECTS)
|
||||
.indexedShapeIndex(indexName)
|
||||
.indexedShapePath("1.location");
|
||||
result = client().prepareSearch(searchIndex).setQuery(QueryBuilders.matchAllQuery())
|
||||
.setPostFilter(filter).get();
|
||||
assertSearchResponse(result);
|
||||
assertHitCount(result, 1);
|
||||
filter = new ShapeQueryBuilder("location", "1").relation(ShapeRelation.INTERSECTS)
|
||||
.indexedShapeIndex(indexName)
|
||||
.indexedShapePath("1.2.location");
|
||||
result = client().prepareSearch(searchIndex).setQuery(QueryBuilders.matchAllQuery())
|
||||
.setPostFilter(filter).get();
|
||||
assertSearchResponse(result);
|
||||
assertHitCount(result, 1);
|
||||
filter = new ShapeQueryBuilder("location", "1").relation(ShapeRelation.INTERSECTS)
|
||||
.indexedShapeIndex(indexName)
|
||||
.indexedShapePath("1.2.3.location");
|
||||
result = client().prepareSearch(searchIndex).setQuery(QueryBuilders.matchAllQuery())
|
||||
.setPostFilter(filter).get();
|
||||
assertSearchResponse(result);
|
||||
assertHitCount(result, 1);
|
||||
|
||||
// now test the query variant
|
||||
ShapeQueryBuilder query = new ShapeQueryBuilder("location", "1")
|
||||
.indexedShapeIndex(indexName)
|
||||
.indexedShapePath("location");
|
||||
result = client().prepareSearch(searchIndex).setQuery(query).get();
|
||||
assertSearchResponse(result);
|
||||
assertHitCount(result, 1);
|
||||
query = new ShapeQueryBuilder("location", "1")
|
||||
.indexedShapeIndex(indexName)
|
||||
.indexedShapePath("1.location");
|
||||
result = client().prepareSearch(searchIndex).setQuery(query).get();
|
||||
assertSearchResponse(result);
|
||||
assertHitCount(result, 1);
|
||||
query = new ShapeQueryBuilder("location", "1")
|
||||
.indexedShapeIndex(indexName)
|
||||
.indexedShapePath("1.2.location");
|
||||
result = client().prepareSearch(searchIndex).setQuery(query).get();
|
||||
assertSearchResponse(result);
|
||||
assertHitCount(result, 1);
|
||||
query = new ShapeQueryBuilder("location", "1")
|
||||
.indexedShapeIndex(indexName)
|
||||
.indexedShapePath("1.2.3.location");
|
||||
result = client().prepareSearch(searchIndex).setQuery(query).get();
|
||||
assertSearchResponse(result);
|
||||
assertHitCount(result, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> getPlugins() {
|
||||
return pluginList(SpatialPlugin.class, XPackPlugin.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that ignore_malformed on GeoShapeFieldMapper does not fail the entire document
|
||||
*/
|
||||
public void testIgnoreMalformed() {
|
||||
assertHitCount(client().prepareSearch(IGNORE_MALFORMED_INDEX).setQuery(matchAllQuery()).get(), numDocs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the indexed shape routing can be provided if it is required
|
||||
*/
|
||||
public void testIndexShapeRouting() {
|
||||
String source = "{\n" +
|
||||
" \"shape\" : {\n" +
|
||||
" \"type\" : \"bbox\",\n" +
|
||||
" \"coordinates\" : [[" + -Float.MAX_VALUE + "," + Float.MAX_VALUE + "], [" + Float.MAX_VALUE + ", " + -Float.MAX_VALUE
|
||||
+ "]]\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
|
||||
client().prepareIndex(INDEX, FIELD_TYPE, "0").setSource(source, XContentType.JSON).setRouting("ABC").get();
|
||||
client().admin().indices().prepareRefresh(INDEX).get();
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch(INDEX).setQuery(
|
||||
new ShapeQueryBuilder(FIELD, "0").indexedShapeIndex(INDEX).indexedShapeRouting("ABC")
|
||||
).get();
|
||||
|
||||
assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long)numDocs+1));
|
||||
}
|
||||
|
||||
public void testNullShape() {
|
||||
// index a null shape
|
||||
client().prepareIndex(INDEX, FIELD_TYPE, "aNullshape").setSource("{\"" + FIELD + "\": null}", XContentType.JSON)
|
||||
.setRefreshPolicy(IMMEDIATE).get();
|
||||
client().prepareIndex(IGNORE_MALFORMED_INDEX, FIELD_TYPE, "aNullshape").setSource("{\"" + FIELD + "\": null}",
|
||||
XContentType.JSON).setRefreshPolicy(IMMEDIATE).get();
|
||||
GetResponse result = client().prepareGet(INDEX, FIELD_TYPE, "aNullshape").get();
|
||||
assertThat(result.getField(FIELD), nullValue());
|
||||
}
|
||||
|
||||
public void testExistsQuery() {
|
||||
ExistsQueryBuilder eqb = QueryBuilders.existsQuery(FIELD);
|
||||
SearchResponse result = client().prepareSearch(INDEX).setQuery(eqb).get();
|
||||
assertSearchResponse(result);
|
||||
assertHitCount(result, numDocs);
|
||||
}
|
||||
|
||||
public void testFieldAlias() {
|
||||
SearchResponse response = client().prepareSearch(INDEX)
|
||||
.setQuery(new ShapeQueryBuilder("alias", queryGeometry).relation(ShapeRelation.INTERSECTS))
|
||||
.get();
|
||||
assertTrue(response.getHits().getTotalHits().value > 0);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,144 @@
|
|||
/*
|
||||
* 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.util;
|
||||
|
||||
import org.apache.lucene.geo.XShapeTestUtil;
|
||||
import org.apache.lucene.geo.XYPolygon;
|
||||
import org.elasticsearch.geo.GeometryTestUtils;
|
||||
import org.elasticsearch.geo.geometry.Geometry;
|
||||
import org.elasticsearch.geo.geometry.GeometryCollection;
|
||||
import org.elasticsearch.geo.geometry.Line;
|
||||
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.geo.geometry.Polygon;
|
||||
import org.elasticsearch.geo.geometry.Rectangle;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.elasticsearch.geo.GeometryTestUtils.linearRing;
|
||||
import static org.elasticsearch.geo.GeometryTestUtils.randomAlt;
|
||||
|
||||
/** generates random cartesian shapes */
|
||||
public class ShapeTestUtils {
|
||||
|
||||
public static double randomValue() {
|
||||
return XShapeTestUtil.nextDouble();
|
||||
}
|
||||
|
||||
public static Point randomPoint() {
|
||||
return randomPoint(ESTestCase.randomBoolean());
|
||||
}
|
||||
|
||||
public static Point randomPoint(boolean hasAlt) {
|
||||
if (hasAlt) {
|
||||
return new Point(randomValue(), randomValue(), randomAlt());
|
||||
}
|
||||
return new Point(randomValue(), randomValue());
|
||||
}
|
||||
|
||||
public static Line randomLine(boolean hasAlts) {
|
||||
// we use nextPolygon because it guarantees no duplicate points
|
||||
XYPolygon lucenePolygon = XShapeTestUtil.nextPolygon();
|
||||
int size = lucenePolygon.numPoints() - 1;
|
||||
double[] x = new double[size];
|
||||
double[] y = new double[size];
|
||||
double[] alts = hasAlts ? new double[size] : null;
|
||||
for (int i = 0; i < size; i++) {
|
||||
x[i] = lucenePolygon.getPolyX(i);
|
||||
y[i] = lucenePolygon.getPolyY(i);
|
||||
if (hasAlts) {
|
||||
alts[i] = randomAlt();
|
||||
}
|
||||
}
|
||||
if (hasAlts) {
|
||||
return new Line(x, y, alts);
|
||||
}
|
||||
return new Line(x, y);
|
||||
}
|
||||
|
||||
public static Polygon randomPolygon(boolean hasAlt) {
|
||||
XYPolygon lucenePolygon = XShapeTestUtil.nextPolygon();
|
||||
if (lucenePolygon.numHoles() > 0) {
|
||||
XYPolygon[] luceneHoles = lucenePolygon.getHoles();
|
||||
List<LinearRing> holes = new ArrayList<>();
|
||||
for (int i = 0; i < lucenePolygon.numHoles(); i++) {
|
||||
XYPolygon poly = luceneHoles[i];
|
||||
holes.add(linearRing(poly.getPolyY(), poly.getPolyX(), hasAlt));
|
||||
}
|
||||
return new Polygon(linearRing(lucenePolygon.getPolyY(), lucenePolygon.getPolyX(), hasAlt), holes);
|
||||
}
|
||||
return new Polygon(linearRing(lucenePolygon.getPolyY(), lucenePolygon.getPolyX(), hasAlt));
|
||||
}
|
||||
|
||||
public static Rectangle randomRectangle() {
|
||||
org.apache.lucene.geo.XYRectangle rectangle = XShapeTestUtil.nextBox();
|
||||
return new Rectangle(rectangle.minY, rectangle.maxY, rectangle.minX, rectangle.maxX);
|
||||
}
|
||||
|
||||
public static MultiPoint randomMultiPoint(boolean hasAlt) {
|
||||
int size = ESTestCase.randomIntBetween(3, 10);
|
||||
List<Point> points = new ArrayList<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
points.add(randomPoint(hasAlt));
|
||||
}
|
||||
return new MultiPoint(points);
|
||||
}
|
||||
|
||||
public static MultiLine randomMultiLine(boolean hasAlt) {
|
||||
int size = ESTestCase.randomIntBetween(3, 10);
|
||||
List<Line> lines = new ArrayList<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
lines.add(randomLine(hasAlt));
|
||||
}
|
||||
return new MultiLine(lines);
|
||||
}
|
||||
|
||||
public static MultiPolygon randomMultiPolygon(boolean hasAlt) {
|
||||
int size = ESTestCase.randomIntBetween(3, 10);
|
||||
List<Polygon> polygons = new ArrayList<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
polygons.add(randomPolygon(hasAlt));
|
||||
}
|
||||
return new MultiPolygon(polygons);
|
||||
}
|
||||
|
||||
public static GeometryCollection<Geometry> randomGeometryCollection(boolean hasAlt) {
|
||||
return randomGeometryCollection(0, hasAlt);
|
||||
}
|
||||
|
||||
private static GeometryCollection<Geometry> randomGeometryCollection(int level, boolean hasAlt) {
|
||||
int size = ESTestCase.randomIntBetween(1, 10);
|
||||
List<Geometry> shapes = new ArrayList<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
shapes.add(randomGeometry(level, hasAlt));
|
||||
}
|
||||
return new GeometryCollection<>(shapes);
|
||||
}
|
||||
|
||||
public static Geometry randomGeometry(boolean hasAlt) {
|
||||
return randomGeometry(0, hasAlt);
|
||||
}
|
||||
|
||||
protected static Geometry randomGeometry(int level, boolean hasAlt) {
|
||||
@SuppressWarnings("unchecked") Function<Boolean, Geometry> geometry = ESTestCase.randomFrom(
|
||||
ShapeTestUtils::randomLine,
|
||||
ShapeTestUtils::randomPoint,
|
||||
ShapeTestUtils::randomPolygon,
|
||||
ShapeTestUtils::randomMultiLine,
|
||||
ShapeTestUtils::randomMultiPoint,
|
||||
ShapeTestUtils::randomMultiPolygon,
|
||||
hasAlt ? ShapeTestUtils::randomPoint : (b) -> randomRectangle(),
|
||||
level < 3 ? (b) -> randomGeometryCollection(level + 1, b) : GeometryTestUtils::randomPoint // don't build too deep
|
||||
);
|
||||
return geometry.apply(hasAlt);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue