Skip to content
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

Add geo polygon query type #613

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions clientlib/src/main/proto/yelp/nrtsearch/search.proto
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,22 @@ message GeoPointQuery {
google.type.LatLng point = 2; // point used to query whether the polygon contains it.
}

// Polygon defined by a list of geo points
message Polygon {
// Points defining the polygon. Coordinates must be in clockwise order, except for holes. Holes must be in counter-clockwise order. The shape is assumed to be closed, with the first point automatically also used as the last point. The polygon must not be self-crossing, otherwise may result in unexpected behavior. Polygons cannot cross the 180th meridian. Instead, use two polygons: one on each side.
repeated google.type.LatLng points = 1;
// Specify holes in the polygon. Hole polygons cannot themselves contain holes.
repeated Polygon holes = 2;
}

// A query that matches documents with geo points within polygons
message GeoPolygonQuery {
// Field in the document to query
string field = 1;
// Geo polygons to search for containing points
repeated Polygon polygons = 2;
}

// A query that matches documents which contain a value for a field.
message ExistsQuery {
string field = 1; // Field in the document to query
Expand Down Expand Up @@ -363,6 +379,7 @@ enum QueryType {
MATCH_PHRASE_PREFIX = 18;
PREFIX = 19;
CONSTANT_SCORE_QUERY = 20;
GEO_POLYGON = 21;
}

// Defines a full query consisting of a QueryNode which may be one of several types.
Expand Down Expand Up @@ -392,6 +409,7 @@ message Query {
MatchPhrasePrefixQuery matchPhrasePrefixQuery = 21;
PrefixQuery prefixQuery = 22;
ConstantScoreQuery constantScoreQuery = 23;
GeoPolygonQuery geoPolygonQuery = 24;
}
}

Expand Down
15 changes: 15 additions & 0 deletions docs/queries/geo_polygon.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Geo-Polygon Query
==========================

A query that matches documents with geo point within a defined set of polygons.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aprudhomme should we add the restrictions mentioned in the notes in https://lucene.apache.org/core/8_4_0/core/org/apache/lucene/geo/Polygon.html ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The important ones are in the Polygon comments, but I can add them here too.

I tried to do some digging, and it looks like some of the requirements have to do with conforming to the GeoJson spec. https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.6 Oddly, the winding order in the spec looks like the opposite of the class notes. The spec also says it is ok to ignore the wrong winding order.

I also see that the lucene class detects the winding order in the constructor, so it really might not be vital to functionality. I think I will try writing some tests that use the opposite order, and relax the requirements if they work.


Proto definition:

.. code-block::

message GeoPolygonQuery {
// Field in the document to query
string field = 1;
// Geo polygons to search for containing points
repeated Polygon polygons = 2;
}
43 changes: 42 additions & 1 deletion grpc-gateway/luceneserver.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3475,6 +3475,23 @@
},
"description": "A query that matches documents with polygon that contains the geo point."
},
"luceneserverGeoPolygonQuery": {
"type": "object",
"properties": {
"field": {
"type": "string",
"title": "Field in the document to query"
},
"polygons": {
"type": "array",
"items": {
"$ref": "#/definitions/luceneserverPolygon"
},
"title": "Geo polygons to search for containing points"
}
},
"title": "A query that matches documents with geo points within polygons"
},
"luceneserverGeoRadiusQuery": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -4243,6 +4260,26 @@
},
"title": "Point representation"
},
"luceneserverPolygon": {
"type": "object",
"properties": {
"points": {
"type": "array",
"items": {
"$ref": "#/definitions/typeLatLng"
},
"description": "Points defining the polygon. Coordinates must be in clockwise order, except for holes. Holes must be in counter-clockwise order. The shape is assumed to be closed, with the first point automatically also used as the last point. The polygon must not be self-crossing, otherwise may result in unexpected behavior. Polygons cannot cross the 180th meridian. Instead, use two polygons: one on each side."
},
"holes": {
"type": "array",
"items": {
"$ref": "#/definitions/luceneserverPolygon"
},
"description": "Specify holes in the polygon. Hole polygons cannot themselves contain holes."
}
},
"title": "Polygon defined by a list of geo points"
},
"luceneserverPrefixQuery": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -4356,6 +4393,9 @@
},
"constantScoreQuery": {
"$ref": "#/definitions/luceneserverConstantScoreQuery"
},
"geoPolygonQuery": {
"$ref": "#/definitions/luceneserverGeoPolygonQuery"
}
},
"description": "Defines a full query consisting of a QueryNode which may be one of several types."
Expand Down Expand Up @@ -4414,7 +4454,8 @@
"MULTI_FUNCTION_SCORE_QUERY",
"MATCH_PHRASE_PREFIX",
"PREFIX",
"CONSTANT_SCORE_QUERY"
"CONSTANT_SCORE_QUERY",
"GEO_POLYGON"
],
"default": "NONE",
"description": "Defines different types of QueryNodes."
Expand Down
Loading
Loading