Search code examples
elasticsearchgeojsonelasticsearch-geo-shape

What is the difference between CONTAINS and WITHIN on Elasticsearch GeoShape queries?


I try to determine if a polygon (indexed in document) has common point with another shape like a circle.

The Elasticsearch documentation provides 4 types of relation:

  • INTERSECT ;
  • DISJOINCT ;
  • CONTAINS ;
  • WITHIN.

I try to find more information especially from the GeoJSON side without success.

I think the first type could answer my problem but I can't find the difference between the two lasts. The documentation is very short.

Thanks.


Solution

  • WITHIN will return all documents whose geo_shape field is within the geometric shape specified in the query.

    => In this case, you will specify a geometric shape A in your query and ES will return all documents whose geo_shape field B is located entirely WITHIN that shape A: A contains B

    CONTAINS will return all documents whose geo_shape field contains the geometric shape specified in the query.

    => In this case, ES will return all documents whose geo_shape field B CONTAINS the geometric shape A specified in your query: B contains A