Skip to content

Support for Geohash as bounding box in geo_bounding_box #30470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion docs/reference/query-dsl/geo-bounding-box-query.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ GET /_search
// CONSOLE

[float]
===== Geohash
===== Geohash as corners

[source,js]
--------------------------------------------------
Expand All @@ -231,6 +231,31 @@ GET /_search
--------------------------------------------------
// CONSOLE

[float]
===== Geohash as a bounding box

[source,js]
--------------------------------------------------
GET /_search
{
"query": {
"bool" : {
"must" : {
"match_all" : {}
},
"filter" : {
"geo_bounding_box" : {
"pin.location" : {
"geohash" : "dr5r"
}
}
}
}
}
}
--------------------------------------------------
// CONSOLE

[float]
==== Vertices

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder<GeoBounding
private static final ParseField BOTTOM_LEFT_FIELD = new ParseField("bottom_left");
private static final ParseField IGNORE_UNMAPPED_FIELD = new ParseField("ignore_unmapped");
private static final ParseField WKT_FIELD = new ParseField("wkt");
private static final ParseField GEOHASH_FIELD = new ParseField("geohash");


/** Name of field holding geo coordinates to compute the bounding box on.*/
Expand Down Expand Up @@ -473,6 +474,7 @@ public static Rectangle parseBoundingBox(XContentParser parser) throws IOExcepti

String currentFieldName;
GeoPoint sparse = new GeoPoint();
Rectangle rectangle = null;
EnvelopeBuilder envelope = null;

while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
Expand All @@ -481,6 +483,13 @@ public static Rectangle parseBoundingBox(XContentParser parser) throws IOExcepti
token = parser.nextToken();
if (WKT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
envelope = (EnvelopeBuilder)(GeoWKTParser.parseExpectedType(parser, GeoShapeType.ENVELOPE));
} else if (GEOHASH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
if (parser.currentToken() == XContentParser.Token.VALUE_STRING) {
rectangle = GeoHashUtils.bbox(parser.textOrNull());
} else {
throw new ElasticsearchParseException("failed to parse bounding box. unsupported geohash value in field [{}]",
currentFieldName);
}
} else if (TOP_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
top = parser.doubleValue();
} else if (BOTTOM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
Expand Down Expand Up @@ -515,13 +524,24 @@ public static Rectangle parseBoundingBox(XContentParser parser) throws IOExcepti
}
}
if (envelope != null) {
if ((Double.isNaN(top) || Double.isNaN(bottom) || Double.isNaN(left) || Double.isNaN(right)) == false) {
if (rectangle != null) {
throw new ElasticsearchParseException("failed to parse bounding box. Conflicting definition found "
+ "using well-known text and geohash");
}
if ((Double.isNaN(top) && Double.isNaN(bottom) && Double.isNaN(left) && Double.isNaN(right)) == false) {
throw new ElasticsearchParseException("failed to parse bounding box. Conflicting definition found "
+ "using well-known text and explicit corners.");
}
org.locationtech.spatial4j.shape.Rectangle r = envelope.build();
return new Rectangle(r.getMinY(), r.getMaxY(), r.getMinX(), r.getMaxX());
}
if (rectangle != null) {
if ((Double.isNaN(top) && Double.isNaN(bottom) && Double.isNaN(left) && Double.isNaN(right)) == false) {
throw new ElasticsearchParseException("failed to parse bounding box. Conflicting definition found "
+ "using geohash and explicit corners.");
}
return rectangle;
}
return new Rectangle(bottom, top, left, right);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.index.mapper.MappedFieldType;
Expand Down Expand Up @@ -450,6 +451,80 @@ public void testFromWKT() throws IOException {
assertEquals(expectedJson, GeoExecType.MEMORY, parsed.type());
}

public void testFromGeohash() throws IOException {
String json =
"{\n" +
" \"geo_bounding_box\" : {\n" +
" \"pin.location\" : {\n" +
" \"geohash\" : \"dr\"\n" +
" },\n" +
" \"validation_method\" : \"STRICT\",\n" +
" \"type\" : \"MEMORY\",\n" +
" \"ignore_unmapped\" : false,\n" +
" \"boost\" : 1.0\n" +
" }\n" +
"}";

String expectedJson =
"{\n" +
" \"geo_bounding_box\" : {\n" +
" \"pin.location\" : {\n" +
" \"top_left\" : [ -78.75, 45.0 ],\n" +
" \"bottom_right\" : [ -67.5, 39.375 ]\n" +
" },\n" +
" \"validation_method\" : \"STRICT\",\n" +
" \"type\" : \"MEMORY\",\n" +
" \"ignore_unmapped\" : false,\n" +
" \"boost\" : 1.0\n" +
" }\n" +
"}";
GeoBoundingBoxQueryBuilder parsed = (GeoBoundingBoxQueryBuilder) parseQuery(json);
checkGeneratedJson(expectedJson, parsed);
assertEquals(json, "pin.location", parsed.fieldName());
assertEquals(json, -78.75, parsed.topLeft().getLon(), 0.0001);
assertEquals(json, 45.0, parsed.topLeft().getLat(), 0.0001);
assertEquals(json, -67.5, parsed.bottomRight().getLon(), 0.0001);
assertEquals(json, 39.375, parsed.bottomRight().getLat(), 0.0001);
assertEquals(json, 1.0, parsed.boost(), 0.0001);
assertEquals(json, GeoExecType.MEMORY, parsed.type());
}

public void testMalformedGeohashes() {
String jsonGeohashAndWkt =
"{\n" +
" \"geo_bounding_box\" : {\n" +
" \"pin.location\" : {\n" +
" \"geohash\" : \"dr\"," +
" \"wkt\" : \"BBOX (-74.1, -71.12, 40.73, 40.01)\"\n" +
" },\n" +
" \"validation_method\" : \"STRICT\",\n" +
" \"type\" : \"MEMORY\",\n" +
" \"ignore_unmapped\" : false,\n" +
" \"boost\" : 1.0\n" +
" }\n" +
"}";

ElasticsearchParseException e1 = expectThrows(ElasticsearchParseException.class, () -> parseQuery(jsonGeohashAndWkt));
assertThat(e1.getMessage(), containsString("Conflicting definition found using well-known text and geohash"));

String jsonGeohashAndTopLeft=
"{\n" +
" \"geo_bounding_box\" : {\n" +
" \"pin.location\" : {\n" +
" \"geohash\" : \"dr\"," +
" \"top_left\" : [ -78.75, 45.0 ]\n" +
" },\n" +
" \"validation_method\" : \"STRICT\",\n" +
" \"type\" : \"MEMORY\",\n" +
" \"ignore_unmapped\" : false,\n" +
" \"boost\" : 1.0\n" +
" }\n" +
"}";

ElasticsearchParseException e2 = expectThrows(ElasticsearchParseException.class, () -> parseQuery(jsonGeohashAndTopLeft));
assertThat(e2.getMessage(), containsString("Conflicting definition found using geohash and explicit corners"));
}

@Override
public void testMustRewrite() throws IOException {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
Expand Down