System design Level 2 This level · 9 of 48
Medium search elasticsearch

How do you write range and geo filters so they stay cheap?

The 30 second answer

Ranges on numbers and dates and geo filters on geo_point fields are yes-or-no conditions, so they go in filter context, where they skip scoring and can be cached. I filter by a radius or bounding box first and only then sort by distance, because a distance sort computes a value for every match. For dates I round now, and for prices I query the precomputed per-market field.

Could you say this in an interview?

From the basics. Numeric, date and geo point fields aren't stored as words in the inverted index. Lucene indexes them in a structure built for ranges, so "price between 50 and 100" or "within 10 km of this point" is a fast index lookup, as long as the query asks the index for it rather than computing something per document.

Typical shop uses. Price sliders and "new in the last 30 days" (range), and "available in a store near me" or "delivery area" (geo).

Badly written. Range in must (scored, not cacheable), distance computed by script, and a distance sort over every product in the country:

{ "query": { "bool": { "must": [
    { "range": { "price": { "gte": 5000, "lte": 10000 } } },
    { "script": { "script": "doc['store_location'].arcDistance(52.52, 13.40) < 10000" } } ] } },
  "sort": [ { "_geo_distance": { "store_location": { "lat": 52.52, "lon": 13.40 } } } ] }

Better. Filters for both, then the distance sort on the small remaining set:

GET store_stock/_search
{ "size": 20,
  "query": { "bool": { "filter": [
    { "range": { "price": { "gte": 5000, "lte": 10000 } } },
    { "range": { "updated_at": { "gte": "now-1d/d" } } },
    { "geo_distance": { "distance": "10km", "store_location": { "lat": 52.52, "lon": 13.40 } } } ] } },
  "sort": [ { "_geo_distance": { "store_location": { "lat": 52.52, "lon": 13.40 },
                                 "order": "asc", "unit": "km" } } ] }

geo_bounding_box is an alternative when the UI already has a map viewport.

Trade-offs.

  • Range filters on now miss the cache unless rounded; the slow-query page covers date rounding.
  • A distance sort is per-document work. With a radius filter first it's bounded; without one, it's a full-catalog computation.
  • Price ranges as facets (buckets) are better precomputed into a price_range keyword at index time if the buckets are fixed.

Alternate interview scenarios

"Show products in stock at stores within 20 km, sorted by relevance, not distance."

Keep the geo filter, keep _score sorting, and use distance only as a small function_score decay boost if product wants nearer stores first among equals.

"The price filter must apply to the shopper's market price."

Query the market's own price field (or a per-market index) rather than converting currencies in a script.

Sources (4)
  1. Elasticsearch docs: Range queryelastic.co
  2. Elasticsearch docs: Geo-distance queryelastic.co
  3. Elasticsearch docs: Geo-bounding box queryelastic.co
  4. Elasticsearch docs: Sort search results (geo distance sorting)elastic.co
Previous
Next