Elasticsearch Geospatial Essentials for Location-Based Search

Author

Reads 587

An artist's illustration of artificial intelligence (AI). This image visualises the streams of data that large language models produce. It was created by Tim West as part of the Visualisin...
Credit: pexels.com, An artist's illustration of artificial intelligence (AI). This image visualises the streams of data that large language models produce. It was created by Tim West as part of the Visualisin...

Elasticsearch provides a robust geospatial search feature that enables you to store and query spatial data. This allows you to build location-based search applications with ease.

You can store spatial data in Elasticsearch using the geohash mapping type, which converts latitude and longitude coordinates into a compact binary string. This string can then be used for efficient querying.

To enable geospatial search, you need to create a geohash field in your Elasticsearch index. The field type should be set to geohash, and you can specify the precision of the geohash using the precision parameter.

A common use case for geospatial search is to find nearby points of interest, such as restaurants or hotels. By using the geohash field, you can efficiently query for points within a certain distance or bounding box.

A unique perspective: Elastic Search by Field

Elasticsearch Geospatial Basics

Elasticsearch is powered by Elasticsearch, which is designed for high-volume, real-time data ingestion and enrichment.

This means you can quickly process and analyze large amounts of geospatial data.

Related reading: Elasticsearch Spring Data

From above of crop anonymous person driving toy airplane on empty world map drawn on blackboard representing travel concept
Credit: pexels.com, From above of crop anonymous person driving toy airplane on empty world map drawn on blackboard representing travel concept

Spatial queries in Elasticsearch will help you answer location-driven questions, while distance queries let you understand proximity.

For example, you can use spatial queries to find all locations within a certain radius of a specific point.

Elasticsearch has Geo-IP and Geo-Match processors that can augment IPs with latitude and longitude, or you can set up your own reverse-geocoding pipeline.

This is useful for applications that require location-based data, such as mapping services or location-based advertising.

To store big geospatial data efficiently, Elasticsearch uses a distributed index.

This allows you to store large amounts of data without sacrificing performance.

Here are some key benefits of using Elasticsearch for geospatial data:

  • High-volume, real-time data ingestion and enrichment
  • Distributed index for efficient storage of big geospatial data
  • Abundant APIs for interoperability

Geospatial Data Types

Elasticsearch offers two main types of geo data: geo_point and geo_shape. Geo_point represents a pair of latitude and longitude on a map, allowing for simple searches and grouping by location.

To work with geo data, you must manually create the index mapping and explicitly set the field mapping to use geo-queries. Dynamic mapping won't work for geo types.

Credit: youtube.com, Elasticsearch Geospatial: Elastic's Guide to Mapping & Geospatial Search

The geo_point type is the simplest, representing a pair of latitude and longitude on a map. It can be used to search for objects in a specific range represented by a distance.

You can use geo points to group documents by location, including within specific regions or by distance from a specified point. This allows you to sort documents accordingly, from near to further away.

Geo_shape provides the functionality to save and search complex shapes like polygons and rectangles. It's represented as a triangular mesh for high spatial resolution.

Elasticsearch represents a geo_shape as a triangular mesh that allows it to provide a very high spatial resolution.

To search documents that contain shapes other than geopoints, you must use a geo_shape data type.

The geo_shape data type allows for more representation of geospatial data, including points, linestrings, polygons, and more.

Here are some examples of geospatial data types:

  • Point: defined by an one point with latitude and longitude.
  • Linestring: defined by an array of two or more positions.
  • Polygon: defined by a list of a list of points.
  • Multipoint: a list of geojson points.
  • Multilinestring: is an example of a list of geojson linestrings.
  • Multipolygon: list of geojson polygons.
  • Geometrycollection: collection of geojson geometry objects.
  • Envelope: consists of coordinates for upper left and lower right points of the shape to represent a bounding rectangle in the format [[minLon, maxLat], [maxLon, minLat]] .
  • Circle: consists of a center point with a radius.

Geospatial Query Types

Geospatial Query Types offer a range of options for searching geospatial data in Elasticsearch. You can use a geo_bounding_box query to find documents with geo-points that fall into a specified rectangle.

Credit: youtube.com, FOSS4G 2022 | What’s new in geospatial Elasticsearch

There are four main types of geospatial queries: geo_bounding_box, geo_distance, geo_polygon, and geo_shape. Each type serves a specific purpose and is used in different scenarios.

The geo_bounding_box query is used to find documents within a rectangular area, while the geo_distance query is used to find documents within a specified range of a point. The geo_polygon query is used to find documents within a specific polygon, and the geo_shape query is used to find documents whose geospatial shapes intersect, are contained by, or do not intersect with a specified shape.

Here's a quick rundown of the geo_shape query options:

  • INTERSECTS – returns all documents whose geo_shape field intersects the query geometry
  • DISJOINT – retrieves all documents whose geo_shape field has nothing in common with the query geometry
  • WITHIN – gets all documents whose geo_shape field is within the query geometry
  • CONTAINS – returns all documents whose geo_shape field contains the query geometry

Bounding Box Query

A bounding box query is a great way to find documents with geo-points that fall into a specified rectangle. It's a powerful tool for geospatial analysis.

You can use the geo_bounding_box query to fetch all the points within a rectangular area. This query is supported by similar formats as the geo_point data type.

Credit: youtube.com, Point Distance Query with Lucene's GeoPointField Type

To implement a bounding box query, you can create a SearchRequest in your project. This allows you to specify the coordinates of the bounding box.

The bounding box query supports multiple formats for location coordinates, just like the geo_point data type. You can find sample queries for supported formats on the official site.

Here are some key details about the geo_bounding_box query:

  • Supported formats are similar to geo_point data type
  • Sample queries can be found on the official site

By using a bounding box query, you can efficiently retrieve documents with geo-points that fall within a specific area. This can be especially useful for applications that require spatial analysis.

Shape Query

Shape Query is a powerful tool for geospatial analysis. It allows you to query documents with complex shapes like polygons and rectangles.

To use a shape query, you need to use the geo_shape data type, which can represent complex shapes with high spatial resolution. This data type is different from geo_point, which only represents a single point.

You can query documents that contain shapes other than geopoints using a geo_shape query. For example, you can use the INTERSECTS operator to retrieve all documents whose geo_shape field intersects the query geometry.

A unique perspective: Elasticsearch Updating Documents

Credit: youtube.com, Modern Database Concepts - Spatial Data in SQL (GEOMETRY)

The relation field in the query determines the spatial relation operators used at search time. You can choose from a list of operators, including INTERSECTS, DISJOINT, WITHIN, and CONTAINS.

Here are some examples of shape queries:

  • To find all documents that fall within a specific polygon, you can use the WITHIN operator.
  • To find all documents that intersect with a specific polygon, you can use the INTERSECTS operator.
  • To find all documents that are contained within a specific polygon, you can use the CONTAINS operator.
  • To find all documents that have nothing in common with a specific polygon, you can use the DISJOINT operator.

You can also query using different GeoJSON shapes, such as polygons, rectangles, and circles. For example, you can use a rectangle to query documents that fall within a specific bounding box.

To implement a shape query, you can use a SearchRequest in your project. For example, you can create a SearchRequest to query ElasticSearch using a specific shape query.

Here's an example of a shape query using GeoJSON:

```json

{

"query": {

"geo_shape": {

"shape": {

"type": "Polygon",

"coordinates": [

[

[-122.0, 37.0],

[-122.0, 38.0],

[-121.0, 38.0],

[-121.0, 37.0],

[-122.0, 37.0]

]

]

},

"relation": "WITHIN"

}

}

}

```

Spatial Joins

Spatial joins are a powerful tool for combining data from different tables based on their spatial relationships.

A spatial join is used to link two tables together based on their proximity to each other.

For example, a join between a table of customers and a table of nearby restaurants can help identify customers who live near a specific restaurant.

Plane Shadow on Paper Map
Credit: pexels.com, Plane Shadow on Paper Map

In a spatial join, the tables are joined based on the distance between their spatial columns.

The article section on "Spatial Joins in Practice" showed an example where a spatial join was used to find customers who live within a certain distance of a specific store.

This type of join is particularly useful for applications that require proximity-based analysis, such as retail or logistics.

A spatial join can also be used to identify areas of overlap between two tables, such as overlapping territories between different sales regions.

The example in the article section on "Spatial Joins in Practice" demonstrated how a spatial join can be used to identify areas of overlap between two tables.

Geospatial Functions and Operations

Elasticsearch 8.14 introduces four OGC spatial search functions: ST_INTERSECTS, ST_DISJOINT, ST_CONTAINS, and ST_WITHIN. These functions behave similarly to their PostGIS counterparts and are used in the same way.

The ES|QL equivalent of a PostGIS query would be to use the spatial functions in a WHERE clause after a FROM clause. Note that in PostGIS, when using the geometry type, all calculations are done on a planar coordinate system, and so if both geometries have the same SRID, it does not matter what the SRID is.

Credit: youtube.com, ElasticSearch - Geo (Spatial Search)

The geo_shape data type allows for more representation of geospatial data, including points, linestrings, polygons, and more. This data type is useful for visualizing data on a map using Kibana Maps.

The ST_INTERSECTS function returns true if two geometries intersect, and false otherwise. The ST_DISJOINT function returns true if two geometries do not intersect, and false otherwise. The ST_CONTAINS function returns true if one geometry contains another, and false otherwise. The ST_WITHIN function returns true if one geometry is within another, and false otherwise.

Here are the four OGC spatial search functions with their descriptions:

The ES|QL spatial functions can be used in any part of the query where their signature makes sense, including in the EVAL command. The EVAL command allows you to evaluate an expression and return the result.

3.4 Hash

Hash values are concise and great for proximity search, making them a useful alternative to explicit pair values. We can use geo hash to represent our point, which is a more compact way to store and compare locations.

Digital monitor with World map and COVID 19 statistical data of global confirmed cases
Credit: pexels.com, Digital monitor with World map and COVID 19 statistical data of global confirmed cases

Geo hash is not very readable, so it's not ideal for human consumption. For example, we can use an online tool to convert latitude-longitude to geo hash.

Geo hash values are great for proximity search, but they're not as easy to understand as latitude-longitude pairs. This is because they're designed to be compact and efficient, rather than easy to read.

Ogc Functions

Elasticsearch 8.14 introduces four OGC spatial search functions that behave similarly to their PostGIS counterparts.

These functions include ST_INTERSECTS, ST_DISJOINT, ST_CONTAINS, and ST_WITHIN, which return true or false based on the intersection or containment of two geometries.

ST_INTERSECTS returns true if two geometries intersect, and false otherwise. ST_DISJOINT returns true if two geometries do not intersect, and false otherwise, which is the inverse of ST_INTERSECTS.

ST_CONTAINS returns true if one geometry contains another, and false otherwise. ST_WITHIN returns true if one geometry is within another, and false otherwise, which is the inverse of ST_CONTAINS.

Credit: youtube.com, Geospatial Standards for Data Science - Dr Gobe Hobona, OGC

Here are the OGC spatial search functions in a table for easy reference:

These functions can be used in any part of the query where their signature makes sense, including WHERE clauses, ROW commands, and EVAL commands.

Ways to Preserve Shape

To preserve the shape of your geospatial data, you'll want to consider the inner type and coordinates fields. These fields are essential for indexing documents correctly.

The type of geospatial data you're working with will also impact how you preserve the shape. For example, a point is defined by a single point with latitude and longitude, while a polygon is defined by a list of points.

You can represent different types of geospatial data, including points, linestrings, polygons, and more. Here's a breakdown of the different types:

  • Point: defined by a single point with latitude and longitude.
  • Linestring: defined by an array of two or more positions.
  • Polygon: defined by a list of points.
  • Multipoint: a list of geojson points.
  • Multilinestring: a list of geojson linestrings.
  • Multipolygon: a list of geojson polygons.
  • Geometrycollection: a collection of geojson geometry objects.
  • Envelope: a bounding rectangle defined by coordinates.
  • Circle: a shape defined by a center point and radius.

To save geo shape data, you'll need to provide the inner type and coordinates fields. Unfortunately, sorting and retrieving geo-shape fields is currently not possible in Elasticsearch due to their complex structure.

Intriguing read: Elasticsearch Fields

Statistic with Aggregation

Credit: youtube.com, New Course: Geospatial Data Science: Statistics and Machine Learning I

You can create statistics on all restaurants of Paris by using geo distance aggregation.

To do this, you need to inform the location of the area you're interested in, such as the Louvre Museum district.

The maximum distance to search in is 1000 km nearby of the Louvre.

Adding average metrics on a field like "score" will give you an average score of restaurants in that area.

For example, the result is 3.04, which is a random average score.

Visualization and Mapping

You can build geospatial visualizations with ease using the Maps app, which allows you to visualize data from Elasticsearch or upload your own from a CSV or GeoJSON file.

The Maps app offers various analysis options, such as choropleth layers to compare statistics across countries or regions, point-to-point maps to understand origin-destination connections, and dynamic heatmaps, grids, and clusters to instantly see spatial patterns.

Kibana has added support for Spatial ES|QL in the Maps application, enabling you to search for geospatial data in Elasticsearch and visualize the results on a map.

Maps and Visualizations

Credit: youtube.com, An introduction to map visualizations

Maps and Visualizations are a powerful tool for geospatial analysis and action. Kibana's Maps app allows you to build visualizations with ease using data from Elasticsearch or uploaded files.

You can visualize data by uploading your own from a CSV or GeoJSON file. This makes it simple to get started with geospatial visualizations.

Kibana's Maps app offers various analysis options, including choropleth layers to compare statistics across countries or regions. You can also use point-to-point maps to understand origin-destination connections.

With the addition of Spatial ES|QL in the Maps application, you can now use ES|QL to search for geospatial data in Elasticsearch. This allows you to add a layer to the map based on the results of an ES|QL query.

For example, you could add a layer to the map that shows all the airports in the world. Or you could add a layer that shows the polygons from the airport_city_boundaries index.

Elastic Maps enables you to easily share and consume geospatial data via dashboards. This makes it easy to collaborate with others and take action on your geospatial data.

Discover more: Elasticsearch Kibana

Ways to Save

Credit: youtube.com, Mapping 101: Learn how to use maps to visualize your data – Flourish webinar

Saving your data is an essential step in visualization and mapping.

There are different ways to save geo point data, as mentioned in the article, one of which is ready to make some queries to search for our data.

Saving data can be a tedious task, but with the right approach, it can be made easier.

To save geo point data, you can make some queries to search for your data, as we are ready to do now.

Data saving is a crucial step in the visualization and mapping process, and it's essential to do it correctly.

For more insights, see: Elasticsearch Change Mapping

Frequently Asked Questions

Does Elasticsearch support geohash?

Yes, Elasticsearch supports geohash through its Geohash Grid Aggregation feature, which efficiently groups and analyzes geospatial data. This feature allows for powerful geospatial querying and analysis.

Ismael Anderson

Lead Writer

Ismael Anderson is a seasoned writer with a passion for crafting informative and engaging content. With a focus on technical topics, he has established himself as a reliable source for readers seeking in-depth knowledge on complex subjects. His writing portfolio showcases a range of expertise, including articles on cloud computing and storage solutions, such as AWS S3.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.