# Geoshape query and spatial relations

**URL:** <https://discuss.elastic.co/t/geoshape-query-and-spatial-relations/330470>\
**Category:** Elasticsearch\
**Created:** [April 20, 2023, 6:50pm UTC](https://discuss.elastic.co/t/geoshape-query-and-spatial-relations/330470 "2023-04-20T18:50:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JbalancioLP](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jbalanciolp/32/117992_2.png) [@JbalancioLP](https://discuss.elastic.co/u/JbalancioLP)\
**Post date:** [April 20, 2023, 6:50pm UTC](https://discuss.elastic.co/t/geoshape-query-and-spatial-relations/330470/1 "2023-04-20T18:50:17Z")

</div>

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

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

```

Here is a sample query

```auto
{
  "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"
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [April 20, 2023, 7:15pm UTC](https://discuss.elastic.co/t/geoshape-query-and-spatial-relations/330470/2 "2023-04-20T19:15:21Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![JbalancioLP](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jbalanciolp/32/117992_2.png) [@JbalancioLP](https://discuss.elastic.co/u/JbalancioLP)\
**Post date:** [April 20, 2023, 7:35pm UTC](https://discuss.elastic.co/t/geoshape-query-and-spatial-relations/330470/3 "2023-04-20T19:35:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 18, 2023, 7:35pm UTC](https://discuss.elastic.co/t/geoshape-query-and-spatial-relations/330470/4 "2023-05-18T19:35:52Z")

</div>

Closed on May 18, 2023, 7:35pm UTC

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