Half the queries in the world have a where in them: restaurants near me, ships inside this corridor, incidents in this district. Elasticsearch treats location as just another field type, geo_point, latitude and longitude, and stores it in the same BKD tree that indexes every number. This essay is about what that buys: queries drawn as shapes, and aggregations drawn as maps.
A geographic query is a shape, and a document matches if its point falls inside. Drag the query around the harbor and switch shapes; the colored cells are how the index sees the problem:
Zoom a map application out and it doesn't draw ten million pins; it draws a heat map. That is a geotile aggregation: bucket every point by the map tile it falls in, count per bucket, and let the zoom level decide how fine the tiles are:
geohex_grid, tiles the world in H3 hexagons instead, which suffer less distortion near the poles and look better on dashboards besides.Under both figures sits one idea: location is a number pair, indexed in the BKD tree for queries and stored as doc values for aggregations and sorting, and everything else, aggregations, ranking by distance, filters during a vector search, composes with it for free. There are richer shapes too: geo_shape indexes polygons themselves, coastlines, delivery zones, no-fly corridors, broken into triangles and indexed in the same kind of tree, so the query "which zones contain this point" is answered the same way.