Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Types  ›  Lesson

Geo Points — Searching by Place

Geo Points 12 minA geo_point must be mapped explicitly, and while objects and strings list latitude first, arrays and WKT list longitude first.

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.

Checking a listing from MumbaiStep 1 of 4
  1. The data

    A listing arrives with "shop_location": [18.93, 72.83].

Drills

Answer fast — trust the rule.

quick drill

A new field arrives as {"lat": 18.52, "lon": 73.86} with no mapping. What does dynamic mapping create?

quick drill

In the array form [73.86, 18.52], which number is the latitude?

quick drill

Which query finds listings within 5 km of a point?

quick drill

Which of these is a correctly ordered string geo point for latitude 18.52, longitude 73.86?

Remember

  • Map it as geo_point first; 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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFind the nearest copies

A shopper at latitude 18.53, longitude 73.85 wants in-stock copies within 10 km. Explain how you would map the location field, write one example value, and which query does the search.