dylankbuckley
dylankbuckley

Reputation: 1647

Elasticsearch query if geo_polygon is inside a multi-polygon

I am checking to see if a geo point lies within a polygon using elastic. I am able to get it to work for simply "Polygon", however "MultiPolygon" doesn't work.

This works (Polygon):

{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_polygon" : {
                    "geo" : {
                        "points" : [
                            [-131.602021, 55.117982],
                            [-131.569159, 55.28229],
                            [-131.355558, 55.183705],
                            [-131.38842, 55.01392],
                            [-131.645836, 55.035827],
                            [-131.602021, 55.117982]
                        ]
                    }
                }
            }
        }
    }
}

This does not (Multipolygon):

{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_polygon" : {
                    "geo" : {
                        "points" : [
                            [
                                    [-131.602021, 55.117982],
                                    [-131.569159, 55.28229],
                                    [-131.355558, 55.183705],
                                    [-131.38842, 55.01392],
                                    [-131.645836, 55.035827],
                                    [-131.602021, 55.117982]
                            ],
                            [
                                    [-131.832052, 55.42469],
                                    [-131.645836, 55.304197],
                                    [-131.749898, 55.128935],
                                    [-131.832052, 55.189182],
                                    [-131.832052, 55.42469]
                            ]
                         ]
                    }
                }
            }
        }
    }
}

My understanding is that I may need to do some sort of boolean query on each of the individual polygons - however, any guidance would be great.

Upvotes: 2

Views: 1524

Answers (2)

Joe - Check out my books
Joe - Check out my books

Reputation: 16905

@Nate is right -- multipolygons are not supported within geo_polygon queries but there's a recently-active PR to enable geo_shape querying on geo_point types -- which would perfectly suit your use case.


In the meantime, you'll have to resort to splitting your multipolygons and using bool-should:

{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "bool": {
          "should": [
            {
              "geo_polygon": {
                "geo": {
                  "points": [
                          [-131.602021, 55.117982],
                          [-131.569159, 55.28229],
                          [-131.355558, 55.183705],
                          [-131.38842, 55.01392],
                          [-131.645836, 55.035827],
                          [-131.602021, 55.117982]
                   ]
                }
              }
            },
            {
              "geo_polygon": {
                "geo": {
                  "points": [
                          [-131.832052, 55.42469],
                          [-131.645836, 55.304197],
                          [-131.749898, 55.128935],
                          [-131.832052, 55.189182],
                          [-131.832052, 55.42469]
                  ]
                }
              }
            }
          ]
        }
      }
    }
  }
}

Upvotes: 3

ARR
ARR

Reputation: 2308

the above answer didnt work for me I tried the following which did the trick:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "geo_polygon": {
                        "_name": "location.geo_coordinates",
                        "location.geo_coordinates": {
                          "points": []
                        }
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "geo_polygon": {
                        "_name": "location.geo_coordinates",
                        "location.geo_coordinates": {
                          "points": []
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ],
    }
  }
}

The minimum_should_match does the trick. And to not affect other should queries I wrap it into separate bool.

Upvotes: 0

Related Questions