Geo-search in Elasticsearch allows you to perform queries based on geographical data. This is particularly useful for applications that need to handle location-based data, such as finding nearby points of interest, geofencing, and more.
Key Concepts
- Geo-Points: Represents a single point on the earth's surface, defined by latitude and longitude.
- Geo-Shapes: Represents complex shapes like polygons, lines, and circles.
- Geo-Queries: Queries that allow you to search and filter documents based on their geographical location.
Geo-Points
Defining Geo-Points
Geo-points can be defined in your Elasticsearch index mapping. Here’s an example of how to define a geo-point field:
Indexing Geo-Points
Once the mapping is set, you can index documents with geo-point data:
POST /my_index/_doc/1 { "name": "Central Park", "location": { "lat": 40.785091, "lon": -73.968285 } }
Geo-Queries
Geo-Distance Query
A geo-distance query allows you to find documents within a certain distance from a specific point. Here’s an example:
GET /my_index/_search { "query": { "geo_distance": { "distance": "10km", "location": { "lat": 40.785091, "lon": -73.968285 } } } }
Geo-Bounding Box Query
A geo-bounding box query finds documents within a specified rectangle. Here’s an example:
GET /my_index/_search { "query": { "geo_bounding_box": { "location": { "top_left": { "lat": 40.915255, "lon": -74.25909 }, "bottom_right": { "lat": 40.496044, "lon": -73.700272 } } } } }
Geo-Polygon Query
A geo-polygon query finds documents within a specified polygon. Here’s an example:
GET /my_index/_search { "query": { "geo_polygon": { "location": { "points": [ { "lat": 40.73, "lon": -74.1 }, { "lat": 40.01, "lon": -71.12 }, { "lat": 40.70, "lon": -74.00 } ] } } } }
Geo-Shapes
Defining Geo-Shapes
Geo-shapes can be defined in your Elasticsearch index mapping. Here’s an example of how to define a geo-shape field:
Indexing Geo-Shapes
Once the mapping is set, you can index documents with geo-shape data:
POST /my_index/_doc/1 { "name": "Central Park", "location": { "type": "polygon", "coordinates": [ [ [-73.981, 40.768], [-73.958, 40.768], [-73.958, 40.785], [-73.981, 40.785], [-73.981, 40.768] ] ] } }
Practical Exercises
Exercise 1: Indexing and Querying Geo-Points
- Define a geo-point field in your index mapping.
- Index a few documents with geo-point data.
- Perform a geo-distance query to find documents within a 5km radius of a specific point.
Solution
-
Define a geo-point field:
PUT /places { "mappings": { "properties": { "location": { "type": "geo_point" } } } }
-
Index documents:
POST /places/_doc/1 { "name": "Place A", "location": { "lat": 40.730610, "lon": -73.935242 } } POST /places/_doc/2 { "name": "Place B", "location": { "lat": 40.650002, "lon": -73.949997 } }
-
Perform a geo-distance query:
GET /places/_search { "query": { "geo_distance": { "distance": "5km", "location": { "lat": 40.730610, "lon": -73.935242 } } } }
Exercise 2: Indexing and Querying Geo-Shapes
- Define a geo-shape field in your index mapping.
- Index a document with a polygon geo-shape.
- Perform a geo-shape query to find documents within a specified polygon.
Solution
-
Define a geo-shape field:
PUT /regions { "mappings": { "properties": { "location": { "type": "geo_shape" } } } }
-
Index a document:
POST /regions/_doc/1 { "name": "Region A", "location": { "type": "polygon", "coordinates": [ [ [-73.981, 40.768], [-73.958, 40.768], [-73.958, 40.785], [-73.981, 40.785], [-73.981, 40.768] ] ] } }
-
Perform a geo-shape query:
GET /regions/_search { "query": { "geo_shape": { "location": { "shape": { "type": "polygon", "coordinates": [ [ [-73.981, 40.768], [-73.958, 40.768], [-73.958, 40.785], [-73.981, 40.785], [-73.981, 40.768] ] ] }, "relation": "within" } } } }
Conclusion
In this section, you learned about geo-search capabilities in Elasticsearch, including how to define and index geo-points and geo-shapes, and how to perform various geo-queries. These skills are essential for building applications that require location-based search functionality. In the next module, we will explore custom plugins and how to extend Elasticsearch's capabilities further.
Elasticsearch Course
Module 1: Introduction to Elasticsearch
- What is Elasticsearch?
- Installing Elasticsearch
- Basic Concepts: Nodes, Clusters, and Indices
- Elasticsearch Architecture
Module 2: Getting Started with Elasticsearch
Module 3: Advanced Search Techniques
Module 4: Data Modeling and Index Management
Module 5: Performance and Scaling
Module 6: Security and Access Control
- Securing Elasticsearch
- User Authentication and Authorization
- Role-Based Access Control
- Auditing and Compliance
Module 7: Integrations and Ecosystem
- Elasticsearch with Logstash
- Elasticsearch with Kibana
- Elasticsearch with Beats
- Elasticsearch with Other Tools