Geo-point and geo-shape types let you store and search locations and shapes on maps easily.
0
0
Geo-point and geo-shape types in Elasticsearch
Introduction
You want to find places near a specific location, like restaurants near you.
You need to store areas like city boundaries or park shapes.
You want to filter search results by distance or area on a map.
You want to visualize data points or shapes on a map in your app.
Syntax
Elasticsearch
{
"mappings": {
"properties": {
"location": {
"type": "geo_point"
},
"area": {
"type": "geo_shape"
}
}
}
}geo_point stores a single point with latitude and longitude.
geo_shape stores complex shapes like polygons or lines.
Examples
A geo-point stored as a string with latitude and longitude.
Elasticsearch
{
"location": "40.12,-71.34"
}A geo-point stored as an object with separate latitude and longitude.
Elasticsearch
{
"location": {
"lat": 40.12,
"lon": -71.34
}
}A geo-shape storing a polygon with coordinates.
Elasticsearch
{
"area": {
"type": "polygon",
"coordinates": [
[
[-71.34, 40.12],
[-71.35, 40.13],
[-71.36, 40.12],
[-71.34, 40.12]
]
]
}
}Sample Program
This example shows how to define geo-point and geo-shape fields in the mapping and how to index a document with a location point and an area polygon.
Elasticsearch
{
"mappings": {
"properties": {
"location": {
"type": "geo_point"
},
"area": {
"type": "geo_shape"
}
}
}
}
// Example document to index
{
"location": {
"lat": 40.12,
"lon": -71.34
},
"area": {
"type": "polygon",
"coordinates": [
[
[-71.34, 40.12],
[-71.35, 40.13],
[-71.36, 40.12],
[-71.34, 40.12]
]
]
}
}OutputSuccess
Important Notes
Geo-points are best for single locations like a store or event.
Geo-shapes are useful for areas like parks, districts, or routes.
Coordinates in geo-shapes use [longitude, latitude] order, which is different from geo-points.
Summary
Geo-point stores one location with latitude and longitude.
Geo-shape stores complex shapes like polygons or lines.
Use these types to search and filter data by location or area on maps.