Elasticsearch Geo Shape Query

What it does

The Geo Shape Query enables complex geospatial searches by comparing geometric shapes. It supports various shape types including points, lines, circles, polygons, and multi-polygons. This query is ideal for scenarios where you need to find locations within a specific area or that intersect with a given shape.

Syntax

The basic syntax for a Geo Shape Query is:

{
  "query": {
    "geo_shape": {
      "location": {
        "shape": {
          "type": "polygon",
          "coordinates": [
            [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
          ]
        },
        "relation": "intersects"
      }
    }
  }
}

For more details, refer to the official Elasticsearch Geo Shape Query documentation.

Example Query

Here's an example query that searches for documents with a location that intersects a specified polygon:

GET /my_locations/_search
{
  "query": {
    "geo_shape": {
      "location": {
        "shape": {
          "type": "polygon",
          "coordinates": [
            [
              [-70, 40],
              [-80, 30],
              [-90, 20],
              [-70, 40]
            ]
          ]
        },
        "relation": "intersects"
      }
    }
  }
}

Common Issues

  1. Incorrect shape format: Ensure that the shape coordinates are in the correct format (longitude, latitude).
  2. Performance issues with large shapes: Complex or large shapes can slow down query performance.
  3. Precision errors: Be aware of potential precision issues when working with very small or very large areas.

Best Practices

  1. Index optimization: Use appropriate indexing strategies for geo_shape fields to improve query performance.
  2. Use simpler shapes: When possible, use simpler shapes to improve query speed.
  3. Consider using geo_bounding_box for initial filtering before applying more complex geo_shape queries.
  4. Regularly update and optimize your indices to maintain performance.

Frequently Asked Questions

Q: How does the Geo Shape Query differ from the Geo Point Query?
A: The Geo Shape Query works with complex shapes like polygons, while the Geo Point Query is limited to point locations. Geo Shape Queries allow for more complex spatial relationships like intersects, within, and contains.

Q: Can I use the Geo Shape Query with GeoJSON data?
A: Yes, Elasticsearch supports GeoJSON formatted data in Geo Shape Queries. You can directly use GeoJSON objects in your query shape definitions.

Q: What are the supported relation types in Geo Shape Queries?
A: Elasticsearch supports several relation types including INTERSECTS, DISJOINT, WITHIN, and CONTAINS. These define how the query shape should relate to the indexed shapes.

Q: How can I improve the performance of Geo Shape Queries?
A: To improve performance, use simpler shapes when possible, ensure proper indexing of geo_shape fields, and consider using geo_bounding_box queries for initial filtering before applying more complex geo_shape queries.

Q: Can Geo Shape Queries handle different coordinate reference systems?
A: By default, Elasticsearch uses WGS84 for geo_shape fields. While it's possible to specify different coordinate reference systems, it's generally recommended to stick with WGS84 for compatibility and performance reasons.

Pulse - Elasticsearch Operations Done Right

Stop googling errors and staring at dashboards.

Free Trial

Subscribe to the Pulse Newsletter

Get early access to new Pulse features, insightful blogs & exclusive events , webinars, and workshops.