OpenSearch: how do I find polygons within a given bounding box?

233 Views Asked by At

I've attempted:

GET thing/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "location": {
            "top_left": {
              "lat": 47.7328,
              "lon": -122.448
            },
            "bottom_right": {
              "lat": 47.468,
              "lon": -122.0924
            }
          }
        }
      }
    }
  }
}

...but get the following error:

"failed_shards" : [
  {
    "shard" : 0,
    "index" : "geofence",
    "node" : "rLGHWGhqRTqDxDlhjA3hhA",
    "reason" : {
      "type" : "query_shard_exception",
      "reason" : "failed to find geo_point field [location]",
      "index" : "geofence",
      "index_uuid" : "sBbXbANQROeQ15IzmBRf3g"
    }
  }
]

...as such I added:

PUT /thing/_mapping
{
  "mappings": {
    "properties": {
      "point": {
        "type": "geo_point"
      }
    }
  }
}

...which returns the following error:

"type" : "mapper_parsing_exception",
"reason" : "Root mapping definition has unsupported parameters:  [mappings : {properties={point={type=geo_point}}}]"

This query works:

GET geofence/_search
{
  "query": {
    "geo_shape": {
      "geometry": {
        "shape": {
          "type": "envelope",
          "coordinates": [ [ -90.0, 90.0], [ 90.0, -90.0] ]
        },
        "relation": "WITHIN"
      }
    }
  }
}

...but this isn't a bounding box and fails for anything outside of -90.0 such as -122.0

given the following geometry:

      "geometry" : {
        "type" : "Polygon",
        "coordinates" : [
          [
            [
              -122.661806,
              45.596338
            ],
            [
              -122.661324,
              45.597164
            ],
            [
              -122.661865,
              45.597326
            ],
            [
              -122.662461,
              45.597532
            ],
            [
              -122.662868,
              45.597652
            ],
            [
              -122.663405,
              45.597825
            ],
            [
              -122.664381,
              45.598088
            ],
            [
              -122.66421,
              45.598846
            ],
            [
              -122.663995,
              45.599551
            ],
            [
              -122.663974,
              45.599672
            ],
            [
              -122.664135,
              45.599844
            ],
            [
              -122.663898,
              45.600662
            ],
            [
              -122.664574,
              45.60088
            ],
            [
              -122.665272,
              45.601008
            ],
            [
              -122.665808,
              45.601128
            ],
            [
              -122.666162,
              45.599799
            ],
            [
              -122.665304,
              45.599582
            ],
            [
              -122.665411,
              45.599046
            ],
            [
              -122.66599,
              45.597269
            ],
            [
              -122.66378,
              45.596884
            ],
            [
              -122.661806,
              45.596338
            ]
          ]
        ]
      },

What is the correct way to query for a list of polygons within a bounding box?

2

There are 2 best solutions below

0
studiobrain On BEST ANSWER
  GET thing/_search
  {
    "size": 1000,
    "query": {
      "bool": {
        "filter": [
          {
            "terms":{
              ...
            }
          }, {
            "geo_shape": {
              "geometry": {
                "shape": {
                  "type": "Polygon",
                  "coordinates" : [[
                    [-180, 75],
                    [-50, 75],
                    [-50, 0],
                    [-180, 0],
                    [-180, 75]
                  ]]
                },
              "relation": "within"
              }
            }
          }
        ]
      }
    }
  }

This gives an actual coordinate based lookup vs the bbox (or whatever an envelope actually is)...

The above coordinates draw a box around N. America.

2
Val On

The error states

failed to find geo_point field [location]

So you need to add a location field of type geo_point in your mapping this way:

PUT /thing/_mapping
{
    "properties": {
      "location": {
        "type": "geo_point"
      }
  }
}

And then your first query will work provided you make sure to index data into the new location field. Your first query, however, will just find "points" within the given bounding box.

If you want to find polygons within a bounding box, you need to use the geo_shape query on a geo_shape field