Geo Points — Searching by Place
The bookshop opens branches, and each book listing now records which branch has it on the shelf. A shopper in Pune asks, "which in-stock copies are within 10 km of me, nearest first?" That needs a field that understands latitude and longitude: geo_point.
The rule
Map it as geo_point first; in arrays, longitude comes first.
A geo_point holds one location on Earth. Dynamic mapping will not detect one — {"lat": 18.52, "lon": 73.86} would just become an object with two float fields — so you declare it:
PUT /books/_mapping
{ "properties": { "shop_location": { "type": "geo_point" } } }It accepts several spellings of the same point:
| Format | Example (a Pune branch) |
|---|---|
| Object | { "lat": 18.52, "lon": 73.86 } |
| String | "18.52,73.86" — latitude, then longitude |
| Array | [73.86, 18.52] — longitude, then latitude |
| WKT | "POINT (73.86 18.52)" — longitude, then latitude |
The array and WKT forms follow the GeoJSON convention of x before y, which is longitude before latitude. The object form is the safest because it names each part.
With the field mapped, you can filter by distance:
GET /books/_search
{
"query": {
"geo_distance": {
"distance": "10km",
"shop_location": { "lat": 18.53, "lon": 73.85 }
}
}
}Other common tools: geo_bounding_box (everything inside a rectangle on the map), sorting by _geo_distance to put the nearest first, and aggregations that bucket results into distance rings or map tiles.
The shortcut
Whenever you see a pair of coordinates, say the labels out loud: "lat, lon" or "lon, lat"? Named keys and strings are lat-first; arrays and WKT are lon-first. A swapped point usually lands somewhere absurd — a sign you flipped it.
The data
A listing arrives with
"shop_location": [18.93, 72.83].
Drills
Answer fast — trust the rule.
A new field arrives as {"lat": 18.52, "lon": 73.86} with no mapping. What does dynamic mapping create?
In the array form [73.86, 18.52], which number is the latitude?
Which query finds listings within 5 km of a point?
Which of these is a correctly ordered string geo point for latitude 18.52, longitude 73.86?
Remember
- Map it as
geo_pointfirst; it is never guessed. - Objects and strings are lat-first; arrays and WKT are lon-first.
- A point in an absurd place usually means swapped coordinates.
