#3062·typesense

stem_dictionary lookup runs after ASCII//TRANSLIT, so a key containing non-ASCII characters silently never matches on the default locale

Author: thorarinntCreated Sep 22, 2026Updated Sep 22, 2026

Description

A custom stemming dictionary entry whose word contains a non-ASCII character never matches at query or index time when the field uses the default locale. The entry is accepted, persisted, and read back verbatim — it simply never resolves.

The cause is an ordering mismatch. In the default path (locale empty or en), Tokenizer transliterates each token through an ASCII//TRANSLIT iconv descriptor and then calls stemmer->stem(out):

cpp
// src/tokenizer.cpp
cd = iconv_open("ASCII//TRANSLIT", "UTF-8");
...
iconv(cd, &inptr, &insize, &outptr, &outsize);   // "mjólkina" -> "mjolkina"
...
token = stemmer->stem(out);                      // stem() receives the TRANSLITERATED token

The dictionary lookup at the other end is an exact-match find against keys stored verbatim from the imported JSONL:

cpp
// src/stemmer_manager.cpp
auto found = dictionary.find(word);   // looking up "mjolkina"; key on file is "mjólkina"
...
// and on a miss, Stemmer::stem returns the word unchanged
stemmed_word = normalized_word.empty() ? word : normalized_word;

So the key can never be found, and the miss path is indistinguishable from "this word has no stem".

Steps to reproduce

Two entries, identical in shape; one key has an accent.

bash
docker run -d --name ts -p 8209:8108 typesense/typesense:29.1 \
  --data-dir=/tmp --api-key=k --enable-cors
until [ "$(curl -s http://localhost:8209/health)" = '{"ok":true}' ]; do sleep 1; done

printf '%s\n' '{"word":"mjólkina","root":"mjólk"}' '{"word":"peysum","root":"peysa"}' \
 | curl -s -X POST -H 'X-TYPESENSE-API-KEY: k' -H 'Content-Type: text/plain' --data-binary @- \
   'http://localhost:8209/stemming/dictionaries/import?id=d'

# Two collections, identical except for `locale`.
for loc in "" "is"; do
  cn="c${loc:-0}"
  fl="{\"name\":\"t\",\"type\":\"string\",\"stem_dictionary\":\"d\""
  [ -n "$loc" ] && fl="$fl,\"locale\":\"$loc\""; fl="$fl}"
  curl -s -X POST -H 'X-TYPESENSE-API-KEY: k' -H 'Content-Type: application/json' \
    -d "{\"name\":\"$cn\",\"fields\":[$fl]}" http://localhost:8209/collections
  printf '%s\n' '{"id":"1","t":"mjólk"}' '{"id":"2","t":"peysa"}' \
   | curl -s -X POST -H 'X-TYPESENSE-API-KEY: k' -H 'Content-Type: text/plain' --data-binary @- \
     "http://localhost:8209/collections/$cn/documents/import?action=create"
  for q in mjólkina peysum; do
    echo -n "locale='$loc' q=$q -> "
    curl -s -H 'X-TYPESENSE-API-KEY: k' --get "http://localhost:8209/collections/$cn/documents/search" \
      --data-urlencode "q=$q" --data-urlencode 'query_by=t' \
      --data-urlencode 'num_typos=0' --data-urlencode 'prefix=false' \
      | python3 -c 'import sys,json;d=json.load(sys.stdin);print(d["found"],[h["document"]["t"] for h in d["hits"]])'
  done
done

Expected behaviour

Both entries resolve, on both collections:

locale=''   q=mjólkina  -> 1 ['mjólk']
locale=''   q=peysum    -> 1 ['peysa']
locale='is' q=mjólkina  -> 1 ['mjólk']
locale='is' q=peysum    -> 1 ['peysa']

Actual behaviour

The accented key resolves only when a locale is set:

locale=''   q=mjólkina  -> 0 []          <-- accented key, never matches
locale=''   q=peysum    -> 1 ['peysa']
locale='is' q=mjólkina  -> 1 ['mjólk']
locale='is' q=peysum    -> 1 ['peysa']

The asymmetry is consistent with the ordering above: the non-empty-locale path preserves non-ASCII bytes, so stem() receives the untransliterated token and the key matches. The value is not language-specific — is, da and de behave identically.

Why this is expensive to diagnose

Every observable signal says the dictionary is healthy:

  • POST /stemming/dictionaries/import returns HTTP 200
  • GET /stemming/dictionaries/d returns the entry with the accent intact: {"root":"mjólk","word":"mjólkina"}
  • the collection reports "stem": true, "stem_dictionary": "d"
  • document import succeeds and num_documents is correct
  • queries answer normally and return plausible results

Only recall is wrong, and nothing counts it.

At scale it is worse, because the failure rate looks like a capacity limit rather than a correctness one. Importing 2,485,317 form→lemma pairs from BÍN (the Icelandic morphological database), all 2,485,317 read back complete and distinct, resolution was 22.1% overall and 0.0% for accented query words — 0 of 300 in a stratified sample. Bucketed by token length, the rate fell smoothly from 87% at 4 bytes to 7% at 20, which reads as a buffer limit. It is not: long Icelandic words almost always carry an accent, so length was a proxy for the real variable.

Pre-folding the keys to the same transliteration (á->a ð->d é->e í->i ó->o ú->u ý->y þ->th æ->ae ö->o) moves resolution to 97.8% — confirming the diagnosis, and serving as a workaround.

Impact

This affects any language whose dictionary keys are not ASCII — French, German, Spanish, Turkish, Czech, Polish, the Nordic languages — for the large class of users who do not set locale because the default path is otherwise what they want. For Icelandic specifically the default path is strongly preferable: setting locale fixes stemming but loses accent-insensitive matching, so mjolk stops finding mjólk, and accents are routinely omitted when typing.

Reproduced on 29.1, 30.2 and 31.0.rc15.

Suggested fix

Normalise dictionary keys on import with the same transformation the tokeniser applies before lookup, so that what is stored and what is looked up agree. Applying it on both sides at upsert_stemming_dictionary and get_normalized_word would keep the read-back API showing the author's original spelling if that is preferred.

Failing that, two smaller improvements would each have saved most of the diagnosis:

  1. Document the requirement on the stemming-dictionary page, including the locale interaction — that stemming dictionaries and the default locale's transliteration interact at all is not currently discoverable.
  2. Warn at import time on a key that will not survive the tokeniser, rather than accepting it silently.

Possibly related: #2093 (stemming and diacritics, Snowball path with locale: fr), #2585 (stemming dictionary API and documentation gaps).

Metadata

Typesense 29.1, 30.2, 31.0.rc15 — Docker, single node, --data-dir on local disk.