Writings

Fuzzy search for romanised Nepali, without breaking exact matches

· 4 min read

Type kathmandau into most search boxes over Nepali content and you get nothing back, even when there are thousands of documents about Kathmandu.

The problem is how people write Nepali on a keyboard. The same word arrives as काठमाडौं in Devanagari, Kathmandu in standard English, or any of a dozen romanised spellings someone typed by ear. Romanised Nepali has no agreed spelling, so exact matching treats every variation as a different word.

This post is about fixing that in OpenSearch or Elasticsearch without making ordinary searches worse, which turns out to be the hard part.

why exact matching fails on romanised Nepali

A search index stores each word as a token and looks queries up token by token. kathmandu and kathmandau are different tokens, so a search for one never finds the other.

English search gets away with this because English spelling is mostly fixed, and stemming handles the rest. Romanised Nepali has neither. Vowel length, doubled consonants and aspirated letters all get spelled differently by different people: bhaktapur or bhatkapur, nagarik or nagrik.

So the search engine needs some tolerance for spelling. The question is where to put it.

the options, and why most fall short

  • Fuzzy matching on every query. The simplest switch to flip. But it makes every search looser to rescue the misspelled ones, so exact matches start sharing the first page with words that are merely close.
  • Transliterating the query into Devanagari. Appealing, since the documents are often in Devanagari anyway. But one romanised word can map to several Devanagari spellings, so you'd need a dictionary or model to pick, and a wrong guess silently finds the wrong thing.
  • Phonetic analyzers like Soundex or Metaphone. They're designed around English pronunciation, so they group Nepali vowels and aspirated consonants in ways that don't match how Nepali sounds.
  • Edge n-grams. Good for search-as-you-type, since they match prefixes. They don't help with a typo in the middle of a word, and they make the index much bigger.

None of these is wrong everywhere. But for a general search box, the best trade-off I've found is fuzzy matching that only kicks in where it's useful.

fuzzy matching as a second, weaker route

Instead of making the main query fuzzy, add fuzzy matching beside it, as a second way for a document to match. Exact matches keep their full score, and fuzzy matches score lower, so when the exact words exist they always rank first.

In practice, the fuzzy route only changes anything for a query that would otherwise come back empty or thin.

json
{
  "query": {
    "bool": {
      "should": [
        { "multi_match": { "query": "kathmandau", "fields": ["title^3", "body"] } },
        {
          "multi_match": {
            "query": "kathmandau",
            "fields": ["title^3", "body"],
            "fuzziness": "AUTO",
            "max_expansions": 20,
            "boost": 0.3
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

The second clause is the same search with fuzziness on and a lower boost. A document matching either clause is a result, and one matching both, an exact hit, scores highest. The max_expansions setting caps how many similar words each query term can expand into, which keeps the fuzzy route cheap.

choosing which words get fuzzed

This matters more than the fuzziness setting itself. Not every word in a Nepali query should be loosened:

  • Devanagari words stay exact. Changing a single character can change the word's meaning entirely, and a romanised spelling is never one or two edits away from a Devanagari one anyway.
  • Numbers stay exact. A case number like 082-CR-0154 that's one character off is a different record, not a typo.
  • Years and IDs stay exact for the same reason.

One check covers nearly all of it: only fuzz words made entirely of English letters. Do that before building the query, and only add the fuzzy clause for the words that pass.

When no word in a query passes, the query should be exactly what it would have been with no fuzzy matching at all. That makes the change safe to ship. Searches in Devanagari, or for a case number, behave exactly as before.

"did you mean" for the near-misses

Fuzzy matching finds results quietly. People also need to see the right spelling, so they can search for it next time.

A term suggester can ride along in the same request as the search, so it costs no extra round trip. Set to suggest only for missing words, it skips words that are already in the index, so a correctly spelled query costs almost nothing.

Where the suggestions come from matters. Drawing them from English titles that have been stemmed offers corrupt for coruption, because the index stores stems, not words. Suggest from fields that hold whole, unstemmed words, such as romanised titles or a curated list of tags. Avoid OCR'd text too, since it's full of its own typos.

One last detail: only show the suggestion on the first page of a relevance-sorted search. That's the only page whose scores say anything about how good the match was. On page four, or when sorting by date, a weak-looking page doesn't mean the search went wrong.

where this still falls short

Fuzziness works on edit distance, and with the AUTO setting the allowed distance depends on word length: short words must match exactly, and longer ones can be one or two edits off. That covers most romanised spellings, but not abbreviations. ktm will never fuzzy-match kathmandu.

Abbreviations and nicknames need synonyms, a hand-made list of words that mean the same thing. It's tedious, but it's the honest fix for words that aren't really misspellings.

For the details, OpenSearch's docs on fuzzy queries and did-you-mean suggestions cover every option used here.

book a chat