Geoshape query and spatial relations

I'd like to return documents using the geoshape query, but I'm having trouble deciding which spatial relation to use. The document has a location as a geopoint. I will be searching by providing a polygon. Should I use the default intersects or should I use within? What are the differences in regards to searching geopoints (if any)? Which is more performant?

This is my mapping

{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point",
        "index": true
      }
    }
  }
}

Here is a sample query

{
  "query": {
    "bool": {
      "filter": [
        {
          "geo_shape": {
            "location": {
              "shape": {
                "type": "polygon",
                "coordinates": [
                  [
                    [
                      -121.79854,
                      37.29158
                    ],
                    [
                      -121.79562,
                      37.29178
                    ],
                    [
                      -121.79592,
                      37.28953
                    ],
                    [
                      -121.79847,
                      37.28984
                    ],
                    [
                      -121.79854,
                      37.29158
                    ]
                  ]
                ]
              },
              "relation": "intersects"
            }
          }
        }
      ]
    }
  }
}

Hello @JbalancioLP ,

If your data contains only single valued points, then there is no difference between using within or intersects. Performance and document matches should be the same for both relationships.

On the other hand, if you have multivalued points then the behaviour is different. The within relation only matches documents where all points are within the geometry, the intersects relationship matches documents where at least one point is within the relationship. The performance of intersects in this case is much better than within.

Thanks for the clear explanation @Ignacio_Vera! Our data will only contain single values. location is a geopoint of lat, lon.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.