Strip Vietnamese accents and search finds the wrong page
Users type with and without diacritics, so a RAG system needs both an original and a folded index, with weights set by a labeled test set.
In brief
- Folding the whole corpus lets unaccented queries find results but breaks accented ones: 'khóa học' (course) gets sent to 'khoa học' (science).
- Keep two fields, original and folded, normalize to NFC first, then merge with a vector branch using RRF.
- Measure recall on paired queries with and without diacritics. The gap between the two figures is the metric to bring down.
A bug report on Wikimedia’s Phabricator records that typing “khóa học” (course) into the search box of Vietnamese Wikipedia sends the reader to “khoa học” (science), an entirely different subject. The system had stripped diacritics to accommodate people who type without them, and people who typed correctly paid for it.
You will meet the same problem when you deploy Vietnamese RAG for a client. The query logs will mix “đường đi” (route) with “duong di”, and “hóa đơn” (invoice) with “hoa don”.
Search only the accented text and every query typed without diacritics is at risk of missing. How large a share that is, you will know only by counting in the logs.
Strip every diacritic and you repeat Wikipedia’s mistake.
The FDE’s job is to design a search path that serves both kinds of typing, then prove with numbers that it works. The whole thing takes a few dozen lines of Python, provided the steps run in the right order.
Folding fixes one half and breaks the other
Elasticsearch’s guide explains that removing diacritics lets “rôle” match “role” and vice versa. It uses the asciifolding filter for Western languages. The same documentation then warns that stripping diacritics loses meaning, citing “está” and “esta” in Spanish, which are different words.
In Vietnamese the cost is higher still. The Wikimedia report judges the impact on Vietnamese-language wikis to be very large, because once diacritics are removed most words collide with entirely unrelated ones. “Đường”, “dương” and “dưỡng” all become “duong”.
Elasticsearch’s proposed solution carries straight over to Vietnamese: index the text twice, once in its original form and once folded. A query with diacritics relies mainly on the original field. A query without them relies on the folded field, because the user has given you no information about diacritics.
Normalize Unicode before folding
This is the step most often forgotten. The Python documentation explains that the same character on screen can be stored as different sequences of code points. The NFD form splits each character into a base letter plus combining marks.
The documentation also warns that two strings that look identical may still compare as unequal if one uses combining characters and the other does not.
Both corpus and queries therefore have to be brought to the same form, usually NFC, before any comparison or folding. Here is the core:
import unicodedata
def nfc(s: str) -> str:
return unicodedata.normalize("NFC", s)
def fold(s: str) -> str:
# Split base letters from marks, drop combining marks (category Mn)
s = unicodedata.normalize("NFD", s.lower())
s = "".join(c for c in s if unicodedata.category(c) != "Mn")
# U+0111 (d with stroke) is a separate character that does not decompose,
# so replace it by hand
return s.replace(chr(0x111), "d")
def has_marks(q: str) -> bool:
q = nfc(q.lower())
return fold(q) != q
Note the line replace(chr(0x111), "d"), where chr(0x111) is the letter đ. Run fold on “đường” without that line and the đ survives, so a user’s “duong” will never match. At write time, each passage is stored in two fields: text = nfc(raw) and text_folded = fold(raw).
How a query about courses is scored
Say you are building an assistant that looks up a university’s regulations, and the corpus holds documents on course registration as well as documents on scientific research. The search function chooses a branch based on the query, then always runs the vector branch too.
A 2026 study in EACL Findings compared keyword, dense and hybrid search for Vietnamese across several domains. Its results support combining BM25 with vectors.
def rrf(ranked_lists, weights, k=60):
scores = {}
for lst, w in zip(ranked_lists, weights):
for rank, doc_id in enumerate(lst, start=1):
scores[doc_id] = scores.get(doc_id, 0.0) + w / (k + rank)
return sorted(scores, key=scores.get, reverse=True)
def search(q, top=10, n=150):
q = nfc(q)
branches, weights = [], []
if has_marks(q):
branches.append(bm25("text", q, limit=n)); weights.append(1.0)
branches.append(bm25("text_folded", fold(q), limit=n)); weights.append(0.3)
else:
branches.append(bm25("text_folded", fold(q), limit=n)); weights.append(1.0)
branches.append(vector_search(q, limit=n)); weights.append(1.0)
return rrf(branches, weights)[:top]
The value k=60 comes from ParadeDB’s documentation on Reciprocal Rank Fusion, which calls it a common default and a good starting point. The same documentation recommends 100 to 200 candidates per branch when you need a top 10, so the code sets n=150 for every branch. The 0.3 weight is only an initial guess, to be tuned later.
Now work through the query “khóa học đăng ký muộn” (late course registration) by hand. It has diacritics, so the original field leads. The document on course registration ranks first in original-field BM25 and third in vector search, giving it 1/61 + 1/63 ≈ 0.0323.
The document on scientific research, if it ranks first only in the folded branch, scores 0.3/61 ≈ 0.0049. That is more than six times lower, so it cannot overtake.
With “khoa hoc dang ky muon”, the folded field matches both kinds of document. “Khoa hoc” alone cannot tell them apart, so the words “dang ky muon” and the vector branch have to decide. Whether the system does that well has to be measured, and the next section shows how.
Weights have to come from a test set
ParadeDB advises tuning weights on a labeled set of queries rather than by feel.
For Vietnamese, the most convenient format is a table. Each row is one question: the accented column holds the correctly typed version, the unaccented column holds the hurried one, and both point to the same correct document. Then compute recall@10 separately for each column.
Back to the regulations assistant. The table below uses invented numbers for a test set of 30 query pairs, purely to show how to read the results:
| Configuration | Recall@10, with diacritics | Recall@10, without diacritics |
|---|---|---|
| Original field only, plus vector | 27/30 | 14/30 |
| Add folded field, secondary weight 0.3 | 27/30 | 24/30 |
| Add folded field, secondary weight 1.0 | 23/30 | 24/30 |
The first row is the system before it has a folded field: more than half of the unaccented queries miss. The second row lifts the unaccented column and leaves the accented column unchanged.
The third row gives the folded branch the same weight as the original field, even for accented queries. Documents about “khoa học” (science) then start pushing into the top 10 for queries such as “khóa học” (course), and the accented column drops.
The secondary weight affects only accented queries, because unaccented queries always use the folded field as their main one. That is why the unaccented column is the same in rows two and three. In this example, 0.3 lifts the unaccented column without lowering the accented one. The six unaccented queries that still miss are the next job for the vector branch.
This work also belongs on a CV. A line such as “measured and narrowed the recall@10 gap between queries with and without diacritics on a test set of N queries” shows an interviewer that you know how to measure, and it holds in any language with diacritics.
When an FDE job posting mentions multilingual search, this is an example to take into the interview.
Common mistakes
The first mistake is keeping only a folded index; “khóa học” turning into “khoa học” is the result. The second is folding before normalizing to NFC, so that two strings that look the same still fail to match. The third is forgetting the letter đ, after which every query containing “duong” or “dang” misses.
Two others are harder to spot. One is folding the corpus with one function and queries with another, such as the search library’s function on the index side and a hand-written one on the API side. The two then diverge on the rare characters.
To avoid it, use a single fold function on both sides and cover it with unit tests.
The other is taking too few candidates per branch, which leaves RRF without enough documents to merge. ParadeDB’s suggestion of 100 to 200 candidates per branch is the place to start.
On arriving at a client, first ask for the query logs and count the percentage of queries typed without diacritics. That figure tells you how many unaccented queries the test set needs. Because it comes from the client’s own data, it is also a good opening for a conversation about search quality.
Was this article useful?
Thanks for the feedback!
5 sources
- asciifolding token filter (Elasticsearch: The Definitive Guide)
- Wikimedia Phabricator T78485 (Vietnamese search and diacritics) · 2014-12-13
- unicodedata — Unicode Database (Python docs)
- Which Works Best for Vietnamese? A Practical Study of Information Retrieval Methods across Domains · 2026-03
- Reciprocal Rank Fusion (ParadeDB pg_search docs)