Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find documents containing polygons by point?

I have documents in ES that have a "boundaries" field. This is an example of the contents of the field: https://gist.github.com/litzinger/a95342dedc0081c8db8d

Given a lon/lat point, I need to be able to query this index to find which document(s) that point falls within. I'm having trouble constructing this query and can't seem to find a clear cut example of how to do this anywhere.

This is an example of one of my queries in which the coordinates array is the lon/lat point. I already have a working query that will use a polygon to find all documents that have a lon/lat point, but I can't seem to get it to work the other way around.

{
  "query": {
    "filtered": {
      "filter": {
        "geo_shape": {
          "boundaries": {
            "relation": "intersects",
            "shape": {
              "coordinates": [
                [
                  [-96.960876,32.795025]
                ]
              ],
              "type": "point"
            }
          }
        }
      },
      "query": {
        "match_all": {}
      }
    }
  }
}
like image 511
Brian Litzinger Avatar asked Mar 18 '23 17:03

Brian Litzinger


1 Answers

Figured it out. First, my mapping was wrong. The field was called "boundary" not "boundaries". Oops. Second, the coordinates value was wrong. Search query should be:

{
  "query": {
    "filtered": {
      "filter": {
        "geo_shape": {
          "boundaries": {
            "relation": "intersects",
            "shape": {
              "coordinates": [
                -96.960876,
                32.795025
              ],
              "type": "point"
            }
          }
        }
      },
      "query": {
        "match_all": {}
      }
    }
  }
}
like image 87
Brian Litzinger Avatar answered Mar 21 '23 07:03

Brian Litzinger