Option to disallow (or penalise) typos on the first character of a token
Description
Please add an option to disallow (or penalise) typos on the first character of a token.
Today a substitution on the first character is treated like any other typo, so short words that differ only in their first letter match each other: shanel → chanel, boat → coat, mice → rice. In product search these are almost always different words, not misspellings. People rarely mistype the first letter of what they are searching for.
Algolia handles this by counting a typo on the first letter as two typos. With the usual minWordSizefor1Typo / minWordSizefor2Typos of 5 / 8, a first-letter typo is therefore rejected for any word shorter than 8 characters. We are migrating from Algolia, and this is one of the largest remaining relevance differences we see.
Steps to reproduce (v30.2)
curl -X POST "$TS/collections" -H "X-TYPESENSE-API-KEY: $KEY" -d '{
"name": "repro", "fields": [{"name": "title", "type": "string"}]
}'
curl -X POST "$TS/collections/repro/documents/import?action=create" -H "X-TYPESENSE-API-KEY: $KEY" --data-binary '
{"id":"1","title":"chanel lipstick"}'q (query_by: title, num_typos: 1, prefix: false) |
typo position | Typesense | Algolia (same settings) |
|---|---|---|---|
chanal |
5th character | matches chanel |
matches (1 typo) |
shanel |
1st character | matches chanel |
0 hits |
Measured on a real catalog: Algolia returned 0 for shanel, while Typesense returned all 1,097 chanel products.
Expected behaviour
An opt-in search parameter, for example either of these:
typo_first_char: false: never accept a typo on the first character;first_char_typo_cost: 2: count a first-character typo as two (Algolia's behaviour), somin_len_2typogoverns it.
Why it matters beyond single words
This interacts with synonyms and drop_tokens (see #552): once a synonym expansion has been reduced to one word, a first-letter typo of that word can pull in unrelated documents. In Thai, for example, เสื้อ ("shirt") matched เนื้อ ("texture") this way, and the two differ only in their first consonant. Rejecting first-character typos would stop that whole class of noise, while keeping typo tolerance for the misspellings people actually make.
Source: typesense/typesense