MultiPolygon Query
A GeoJSON MultiPolygon Query against any GeoJSON type.
Couchbase Server 7.1.2
QueryShape for a MultiPolygon Query
A GeoJSON query via a GeoShape of MultiPolygon to find GeoJSON types in a Search index using the 3 relations intersects, contains, and within.
MultiPolygon Intersects
Query
An intersect
query for multipolygon returns all the matched documents with shapes that overlap with the area of any of the polygons within the query.
A multipolygon intersection
query sample is given below.
{
"query": {
"field": "<<fieldName>>",
"geometry": {
"shape": {
"type": "MultiPolygon",
"coordinates": [
[
[[-114.027099609375, 42.00848901572399],
[-114.04907226562499, 36.99377838872517],
[-109.05029296875, 36.99377838872517],
[-109.05029296875, 40.98819156349393],
[-111.060791015625, 40.98819156349393],
[-111.02783203125, 42.00848901572399],
[-114.027099609375, 42.00848901572399]]
],
[
[[-109.05029296875,37.00255267215955],
[-102.041015625,37.00255267215955],
[-102.041015625,40.9964840143779],
[-109.05029296875,40.9964840143779],
[-109.05029296875,37.00255267215955]]
]
]
},
"relation": "intersects"
}
}
}
The intersection rules are similar to that of the Polygon Query shape described earlier. The rules will be applied to every indexed GeoJSON Polygon shape in the MultiPolygon array. If any of the query polygons intersect, then it will be a matching document.
MultiPolygon Contains
Query
A contains
query for multipolygon returns all the matched documents with shape(s) that collectively contain the area of every polygon within the query.
A multipolygon contains
query sample is given below.
{
"query": {
"field": "<<fieldName>>",
"geometry": {
"shape": {
"type": "Polygon",
"coordinates": [
[
[[-114.027099609375, 42.00848901572399],
[-114.04907226562499, 36.99377838872517],
[-109.05029296875, 36.99377838872517],
[-109.05029296875, 40.98819156349393],
[-111.060791015625, 40.98819156349393],
[-111.02783203125, 42.00848901572399],
[-114.027099609375, 42.00848901572399]]
],
[
[[-109.05029296875,37.00255267215955],
[-102.041015625,37.00255267215955],
[-102.041015625,40.9964840143779],
[-109.05029296875,40.9964840143779],
[-109.05029296875,37.00255267215955]]
]
]
},
"relation": "contains"
}
}
}
The containment rules are similar to that of the Polygon Query shape described earlier. The rules will be applied to every indexed GeoJSON Polygon shape in the MultiPolygon array. If every query polygon is contained within any of the indexed shapes in the document, then it will be considered as a matching document.
MultiPolygon WithIn
Query
The Within query is not supported by line geometries.
A within
query for multipolygon returns all the matched documents with shape(s) that are residing within the area of any of the polygons within the query.
A multipolygon contains
query sample is given below.
{
"query": {
"field": "<<fieldName>>",
"geometry": {
"shape": {
"type": "Polygon",
"coordinates": [
[
[[-114.027099609375, 42.00848901572399],
[-114.04907226562499, 36.99377838872517],
[-109.05029296875, 36.99377838872517],
[-109.05029296875, 40.98819156349393],
[-111.060791015625, 40.98819156349393],
[-111.02783203125, 42.00848901572399],
[-114.027099609375, 42.00848901572399]]
],
[
[[-109.05029296875,37.00255267215955],
[-102.041015625,37.00255267215955],
[-102.041015625,40.9964840143779],
[-109.05029296875,40.9964840143779],
[-109.05029296875,37.00255267215955]]
]
]
},
"relation": "within"
}
}
}
WithIn rules for the MultiPolygon Query with other indexed GeoJSON shapes in the document set are given below.
The within rules are similar to that of the Polygon Query shape described earlier. The rules will be applied to every indexed GeoJSON Polygon shape in the MultiPolygon array. If all the polygons in the query collectively contain/cover all of the shapes in the documents, then it will be considered as a matching document.
Example MultiPolygon Query (against Points)
It is assumed that you your cluster has 1) a modified travel-sample with GeoJSON data and 2) a Search index as per Creating a GeoJSON Index via the REST API prior to running this example. |
Matches when the multipolygon in the query contains the point in the document including points on the edge or coinciding with the vertices of the multipolygon.
The MultiPolygon contains a two polygons one for Utah and one for Colorado. The results are specified to be sorted on name
. Note type hotel and landmark have a name field and type airport has an airportname field all these values are analyzed as a keyword (exposed as name
).
curl -s -XPOST -H "Content-Type: application/json" \
-u ${CB_USERNAME}:${CB_PASSWORD} http://${CB_HOSTNAME}:8094/api/index/test_geojson/query \
-d '{
"query": {
"field": "geojson",
"geometry": {
"shape": {
"type": "MultiPolygon",
"coordinates": [
[
[[-114.027099609375, 42.00848901572399],
[-114.04907226562499, 36.99377838872517],
[-109.05029296875, 36.99377838872517],
[-109.05029296875, 40.98819156349393],
[-111.060791015625, 40.98819156349393],
[-111.02783203125, 42.00848901572399],
[-114.027099609375, 42.00848901572399]]
],
[
[[-109.05029296875,37.00255267215955],
[-102.041015625,37.00255267215955],
[-102.041015625,40.9964840143779],
[-109.05029296875,40.9964840143779],
[-109.05029296875,37.00255267215955]]
]
]
},
"relation": "within"
}
},
"size": 5,
"from": 0,
"sort": ["name"]
}' | jq .
The output of five (5) hits (from a total of 45 matching docs) is as follows
{
"status": {
"total": 1,
"failed": 0,
"successful": 1
},
"request": {
"query": {
"geometry": {
"shape": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-114.027099609375,
42.00848901572399
],
[
-114.04907226562499,
36.99377838872517
],
[
-109.05029296875,
36.99377838872517
],
[
-109.05029296875,
40.98819156349393
],
[
-111.060791015625,
40.98819156349393
],
[
-111.02783203125,
42.00848901572399
],
[
-114.027099609375,
42.00848901572399
]
]
],
[
[
[
-109.05029296875,
37.00255267215955
],
[
-102.041015625,
37.00255267215955
],
[
-102.041015625,
40.9964840143779
],
[
-109.05029296875,
40.9964840143779
],
[
-109.05029296875,
37.00255267215955
]
]
]
]
},
"relation": "within"
},
"field": "geojson"
},
"size": 5,
"from": 0,
"highlight": null,
"fields": null,
"facets": null,
"explain": false,
"sort": [
"name"
],
"includeLocations": false,
"search_after": null,
"search_before": null
},
"hits": [
{
"index": "test_geojson_3397081757afba65_4c1c5584",
"id": "airport_7001",
"score": 0.15727687392401135,
"sort": [
"Aspen Pitkin County Sardy Field"
]
},
{
"index": "test_geojson_3397081757afba65_4c1c5584",
"id": "airport_8854",
"score": 0.07715884020494193,
"sort": [
"Boulder Municipal"
]
},
{
"index": "test_geojson_3397081757afba65_4c1c5584",
"id": "airport_6999",
"score": 0.0741364322553217,
"sort": [
"Brigham City"
]
},
{
"index": "test_geojson_3397081757afba65_4c1c5584",
"id": "airport_7857",
"score": 0.15503416574594084,
"sort": [
"Bryce Canyon"
]
},
{
"index": "test_geojson_3397081757afba65_4c1c5584",
"id": "airport_3567",
"score": 0.07715884020494193,
"sort": [
"Buckley Afb"
]
}
],
"total_hits": 45,
"max_score": 0.28539049531242594,
"took": 10460443,
"facets": null
}
Example MultiPolygon Query (against Circles)
It is assumed that you your cluster has 1) a modified travel-sample with GeoJSON data and 2) a Search index as per Creating a GeoJSON Index via the REST API prior to running this example. |
The MultiPolygon contains a two polygons one for Utah and one for Colorado. Intersects when the query multipolygon intersects the circular region in the document.
The results are specified to be sorted on name
. Note type hotel and landmark have a name field and type airport has an airportname field all these values are analyzed as a keyword (exposed as name
).
curl -s -XPOST -H "Content-Type: application/json" \
-u ${CB_USERNAME}:${CB_PASSWORD} http://${CB_HOSTNAME}:8094/api/index/test_geojson/query \
-d '{
"query": {
"field": "geoarea",
"geometry": {
"shape": {
"type": "MultiPolygon",
"coordinates": [
[
[[-114.027099609375, 42.00848901572399],
[-114.04907226562499, 36.99377838872517],
[-109.05029296875, 36.99377838872517],
[-109.05029296875, 40.98819156349393],
[-111.060791015625, 40.98819156349393],
[-111.02783203125, 42.00848901572399],
[-114.027099609375, 42.00848901572399]]
],
[
[[-109.05029296875,37.00255267215955],
[-102.041015625,37.00255267215955],
[-102.041015625,40.9964840143779],
[-109.05029296875,40.9964840143779],
[-109.05029296875,37.00255267215955]]
]
]
},
"relation": "intersects"
}
},
"size": 5,
"from": 0,
"sort": ["name"]
}' | jq .
The output of five (5) hits (from a total of 49 matching docs) is as follows
{
"status": {
"total": 1,
"failed": 0,
"successful": 1
},
"request": {
"query": {
"geometry": {
"shape": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-114.027099609375,
42.00848901572399
],
[
-114.04907226562499,
36.99377838872517
],
[
-109.05029296875,
36.99377838872517
],
[
-109.05029296875,
40.98819156349393
],
[
-111.060791015625,
40.98819156349393
],
[
-111.02783203125,
42.00848901572399
],
[
-114.027099609375,
42.00848901572399
]
]
],
[
[
[
-109.05029296875,
37.00255267215955
],
[
-102.041015625,
37.00255267215955
],
[
-102.041015625,
40.9964840143779
],
[
-109.05029296875,
40.9964840143779
],
[
-109.05029296875,
37.00255267215955
]
]
]
]
},
"relation": "intersects"
},
"field": "geoarea"
},
"size": 5,
"from": 0,
"highlight": null,
"fields": null,
"facets": null,
"explain": false,
"sort": [
"name"
],
"includeLocations": false,
"search_after": null,
"search_before": null
},
"hits": [
{
"index": "test_geojson_3397081757afba65_4c1c5584",
"id": "airport_7001",
"score": 0.10519759431791387,
"sort": [
"Aspen Pitkin County Sardy Field"
]
},
{
"index": "test_geojson_3397081757afba65_4c1c5584",
"id": "airport_8854",
"score": 0.050596784242215975,
"sort": [
"Boulder Municipal"
]
},
{
"index": "test_geojson_3397081757afba65_4c1c5584",
"id": "airport_6999",
"score": 0.04283511574155623,
"sort": [
"Brigham City"
]
},
{
"index": "test_geojson_3397081757afba65_4c1c5584",
"id": "airport_7857",
"score": 0.23115574489506296,
"sort": [
"Bryce Canyon"
]
},
{
"index": "test_geojson_3397081757afba65_4c1c5584",
"id": "airport_3567",
"score": 0.047931898270349875,
"sort": [
"Buckley Afb"
]
}
],
"total_hits": 49,
"max_score": 0.412412891553119,
"took": 11706695,
"facets": null
}