diff --git a/docs/reference/query-dsl/geo-bounding-box-query.asciidoc b/docs/reference/query-dsl/geo-bounding-box-query.asciidoc index e8db949bbc6..a1b427acf27 100644 --- a/docs/reference/query-dsl/geo-bounding-box-query.asciidoc +++ b/docs/reference/query-dsl/geo-bounding-box-query.asciidoc @@ -180,6 +180,31 @@ GET /_search -------------------------------------------------- // CONSOLE +[float] +===== Bounding Box as Well-Known Text (WKT) + +[source,js] +-------------------------------------------------- +GET /_search +{ + "query": { + "bool" : { + "must" : { + "match_all" : {} + }, + "filter" : { + "geo_bounding_box" : { + "pin.location" : { + "wkt" : "BBOX (-74.1, -71.12, 40.73, 40.01)" + } + } + } + } + } +} +-------------------------------------------------- +// CONSOLE + [float] ===== Geohash diff --git a/server/src/main/java/org/elasticsearch/common/geo/parsers/GeoWKTParser.java b/server/src/main/java/org/elasticsearch/common/geo/parsers/GeoWKTParser.java index 005caed53a7..38643df0179 100644 --- a/server/src/main/java/org/elasticsearch/common/geo/parsers/GeoWKTParser.java +++ b/server/src/main/java/org/elasticsearch/common/geo/parsers/GeoWKTParser.java @@ -63,6 +63,12 @@ public class GeoWKTParser { public static ShapeBuilder parse(XContentParser parser) throws IOException, ElasticsearchParseException { + return parseExpectedType(parser, null); + } + + /** throws an exception if the parsed geometry type does not match the expected shape type */ + public static ShapeBuilder parseExpectedType(XContentParser parser, final GeoShapeType shapeType) + throws IOException, ElasticsearchParseException { FastStringReader reader = new FastStringReader(parser.text()); try { // setup the tokenizer; configured to read words w/o numbers @@ -77,7 +83,7 @@ public class GeoWKTParser { tokenizer.wordChars('.', '.'); tokenizer.whitespaceChars(0, ' '); tokenizer.commentChar('#'); - ShapeBuilder builder = parseGeometry(tokenizer); + ShapeBuilder builder = parseGeometry(tokenizer, shapeType); checkEOF(tokenizer); return builder; } finally { @@ -86,8 +92,14 @@ public class GeoWKTParser { } /** parse geometry from the stream tokenizer */ - private static ShapeBuilder parseGeometry(StreamTokenizer stream) throws IOException, ElasticsearchParseException { + private static ShapeBuilder parseGeometry(StreamTokenizer stream, GeoShapeType shapeType) + throws IOException, ElasticsearchParseException { final GeoShapeType type = GeoShapeType.forName(nextWord(stream)); + if (shapeType != null && shapeType != GeoShapeType.GEOMETRYCOLLECTION) { + if (type.wktName().equals(shapeType.wktName()) == false) { + throw new ElasticsearchParseException("Expected geometry type [{}] but found [{}]", shapeType, type); + } + } switch (type) { case POINT: return parsePoint(stream); @@ -228,9 +240,10 @@ public class GeoWKTParser { if (nextEmptyOrOpen(stream).equals(EMPTY)) { return null; } - GeometryCollectionBuilder builder = new GeometryCollectionBuilder().shape(parseGeometry(stream)); + GeometryCollectionBuilder builder = new GeometryCollectionBuilder().shape( + parseGeometry(stream, GeoShapeType.GEOMETRYCOLLECTION)); while (nextCloserOrComma(stream).equals(COMMA)) { - builder.shape(parseGeometry(stream)); + builder.shape(parseGeometry(stream, null)); } return builder; } diff --git a/server/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java index c0e57cc45af..47dcbaa3514 100644 --- a/server/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java @@ -31,7 +31,10 @@ import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.geo.GeoHashUtils; import org.elasticsearch.common.geo.GeoPoint; +import org.elasticsearch.common.geo.GeoShapeType; import org.elasticsearch.common.geo.GeoUtils; +import org.elasticsearch.common.geo.builders.EnvelopeBuilder; +import org.elasticsearch.common.geo.parsers.GeoWKTParser; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -62,7 +65,6 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder GeoWKTParser.parseExpectedType(parser, GeoShapeType.POLYGON)); + assertThat(e, hasToString(containsString("Expected geometry type [polygon] but found [point]"))); + } } diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java index 133057fb8d0..aeaca328ceb 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java @@ -406,6 +406,50 @@ public class GeoBoundingBoxQueryBuilderTests extends AbstractQueryTestCase 0);