Geo-point and geo-shape types let you store and search locations and shapes on maps easily.
Geo-point and geo-shape types in 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.
{
"location": "40.12,-71.34"
}{
"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]
]
]
}
}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.
{
"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]
]
]
}
}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.
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.
Practice
What is the main difference between geo_point and geo_shape types in Elasticsearch?
Solution
Step 1: Understand
geo_pointtypegeo_pointstores a single geographic location using latitude and longitude coordinates.Step 2: Understand
geo_shapetypegeo_shapestores complex geographic shapes such as polygons, lines, or multi-points.Final Answer:
geo_pointstores a single location;geo_shapestores complex shapes like polygons. -> Option AQuick Check:
Single point vs complex shape = C [OK]
- Confusing points with polygons
- Thinking geo_point stores multiple points
- Mixing data types with geo types
Which of the following is the correct way to define a geo_point field in an Elasticsearch mapping?
{
"properties": {
"location": {
"type": "?"
}
}
}Solution
Step 1: Recall Elasticsearch geo types
Elasticsearch uses "geo_point" as the type name for single geographic points.Step 2: Match the correct type in mapping
The mapping must specify "type": "geo_point" to store latitude and longitude.Final Answer:
"geo_point" -> Option BQuick Check:
Mapping type for point = "geo_point" [OK]
- Using incorrect type names like geo_shape for points
- Typos in type string
- Confusing field name with type
Given this Elasticsearch document indexed with a geo_point field location:
{ "location": { "lat": 40.7128, "lon": -74.0060 } }Which query will correctly find documents within 10km of this point?
Solution
Step 1: Identify correct query type for geo_point
Forgeo_point,geo_distancequery finds documents within a distance from a point.Step 2: Check query syntax and parameters
{ "geo_distance": { "distance": "10km", "location": { "lat": 40.7128, "lon": -74.0060 } } } usesgeo_distancewith correct distance and point coordinates.Final Answer:
{ "geo_distance": { "distance": "10km", "location": { "lat": 40.7128, "lon": -74.0060 } } } -> Option DQuick Check:
geo_distance query = { "geo_distance": { "distance": "10km", "location": { "lat": 40.7128, "lon": -74.0060 } } } [OK]
- Using geo_shape query on geo_point field
- Wrong coordinate order in geo_shape
- Using term or range queries for geo data
What is wrong with this Elasticsearch mapping for a geo_shape field named area?
{
"properties": {
"area": {
"type": "geo_point"
}
}
}The data to be stored is a polygon shape.
Solution
Step 1: Check field type for polygon data
Polygons requiregeo_shapetype, notgeo_point.Step 2: Identify mapping error
Mapping usesgeo_pointwhich only supports single points, so it cannot store polygons.Final Answer:
The type should begeo_shapeto store polygons. -> Option AQuick Check:
Polygon needs geo_shape type = D [OK]
- Using geo_point for polygon shapes
- Assuming field name affects type
- Adding coordinates in mapping instead of data
You want to store a city boundary as a polygon and find all points inside it. Which Elasticsearch setup is correct?
{
"mappings": {
"properties": {
"city_boundary": { "type": "?" },
"user_location": { "type": "?" }
}
}
}Choose the correct types for city_boundary and user_location.
Solution
Step 1: Determine type for city boundary polygon
City boundary is a polygon, so it requiresgeo_shapetype.Step 2: Determine type for user location point
User location is a single point, so it requiresgeo_pointtype.Step 3: Match types to fields
Assigngeo_shapetocity_boundaryandgeo_pointtouser_location.Final Answer:
"geo_shape" for city_boundary and "geo_point" for user_location -> Option CQuick Check:
Polygon = geo_shape, Point = geo_point [OK]
- Swapping types between polygon and point
- Using geo_point for polygons
- Using geo_shape for single points unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
