Skip to content

Search engine

The one idea: an index sorted by importance

Section titled “The one idea: an index sorted by importance”

Every place gets a static importance score:

importance = feature weight + population score
Feature Weight
Country 1600
State (ADM1) 1450
Capital 1300
Airport 1250
State capital 1200
Island 1150
District capital 1100
Other town or village 1000
Other admin area, mountain, park 900
Buildings, hotels 850
Rivers, roads, forests 800

The population score adds about 100 per ×10 (1,000 → 410, 1 million → 710, 10 million → 810).

The full-text index place_search stores places in importance order. Asking for “the first 200 matches” therefore returns the 200 most important matches, and the database stops there. A common prefix like sa costs the same as santorini.

  1. Candidates (1 D1 batch): up to 200 name matches and 100 alias matches per region, plus up to 50 exact-name matches. In auto mode there are two regions, India and the world.
  2. Score each candidate: match tier + importance, plus 500 if it is in India and the mode is auto.
  3. Typo recovery (1 D1 batch, only if needed): runs only when no notable place matched. It reads the 300 most important places sharing the first two letters and keeps those within 1–2 edits.
  4. Hydrate (1 D1 query): joins names only for the page being returned.
Match tier Points Example
Exact name 1200 goa → Goa
Exact alias (3+ letters) 1100 bombay → Mumbai, nyc
Name starts with query 900 mumb → Mumbai
Every word starts a name word 500 saint p → Saint Petersburg
Alias starts with query 200
One-letter typo (only when recovery ran) 600 mumbia → Mumbai
Two-letter typo always last
  • Workers: each request has its own isolate. Requests never wait for each other here.
  • D1: one database runs one query at a time. Every tenant shares this queue, so query time is what matters.
  • Our numbers (full 13.47M dataset, local, no cache, people typing 34 destinations letter by letter):
Before Now
Average DB time per search 285 ms 6.6 ms
Slowest 1% 4.6 s 17 ms
One database’s ceiling ~3 searches/s ~150 searches/s
  • KV cache answers repeats (go, goa) with zero DB work, so real capacity is higher.
  • If it is ever not enough: turn on D1 read replication (see Roadmap). A Cloudflare Queue would not help: queues are for background jobs, not instant answers.
  • Case, accents and punctuation are ignored. Łódź matches lodz.
  • Native scripts work: मुंबई, சென்னை.
  • One-letter words are checked in code, not in the index.
  • Digit-only queries (PIN codes) return nothing. GeoNames has no postal data.
  • Unpopulated landmarks rank low: everest shows small towns named Everest first.
  • Typos in the first two letters are not recovered.
  • Very common words (lake) cost up to about 40 ms of DB time.