Arabic-aware search algorithm
This document describes the search behavior independently of C#, Entity Framework Core, and PostgreSQL. It can be used to reproduce the same behavior in another language or database. It reflects the implementation as of July 30, 2026. The algorithm is a pragmatic text search rather than a linguistic analyzer. It does not stem words, infer roots, transliterate Arabic, or correct arbitrary spelling mistakes. It expands a query into a bounded set of forms that account for:- an accidentally selected Arabic or QWERTY keyboard layout;
- common Arabic and Persian forms of kaf and yeh;
- alif maqsura;
- terminal heh and taa marbuta;
- case-insensitive substring matching;
- weighted relevance for a primary field, normally a person’s full name.
Terminology
Unless stated otherwise, variation sets remove duplicate strings. The current
implementation compares variation-set entries case-insensitively at the outer
level and exactly at the inner Arabic/Persian expansion levels. A port may use
one case-insensitive ordered set throughout if it produces the same observable
matches.
High-level process
For ranked search:- Trim the query and split it into terms.
- Generate variations for every term.
- Generate variations for the complete trimmed phrase.
- Keep a record if at least one term variation occurs in at least one searchable field.
- Compute an additive relevance score.
- Sort records by descending score.
- Apply an application-defined tie-breaker, such as descending record ID.
Query tokenization
Split the query on the ordinary space character (U+0020). Remove empty
entries and trim each result.
For example:
Variation generation
Generate variations for a string in three stages.1. Initial forms
Start with a case-insensitive set containing:- the original input;
- the input interpreted as QWERTY keystrokes and converted to Arabic;
- the input interpreted as Arabic-layout keystrokes and converted to QWERTY.
2. Arabic and Persian character forms
For each initial form, produce whole-string styles for kaf:3. Terminal heh and taa marbuta
Split each form into words. For every word ending inة or ه, produce both
endings while preserving the stem:
Keyboard-layout mapping
The mapping represents the Arabic keyboard layout used by the implementation. Conversion leaves characters that do not appear in the table unchanged.
When converting Arabic to QWERTY:
- recognize
لاbefore processing individual characters and emitb; - treat Persian
کlike Arabicك; - treat Persian
یlike Arabicي.
- look up Latin letters in lowercase, so Caps Lock does not prevent repair;
- map
bto the two-character sequenceلا; - preserve unmapped characters.
Matching
For a term, a record matches when any variation is contained in any searchable field:ANY semantics between terms:
ILIKE:
Compatibility warning: The current implementation does not escape SQL pattern characters in the query.Null field values behave as non-matches.%and_therefore act as wildcards under SQLLIKE/ILIKEsemantics. A new implementation should either preserve this for strict compatibility or explicitly define and document escaping.
Ranked relevance score
The score is additive. Every true condition contributes its points, including conditions that overlap. Let:T0be the first term;Tibe a later term at indexi;Pbe the primary field;Fbe all searchable fields, includingP.
Each occurrence test means that at least one variation satisfies the
condition. It does not count the number of matching variations or repeated
occurrences in a field.
Unranked filtering variant
The implementation also defines an unranked filter. It generates the same per-term variations and supports two ways to combine terms:- strict mode: every term must match at least one searchable field;
- loose mode: at least one term must match at least one searchable field.
AND.
Canonical text normalization
Canonical normalization is a separate utility. The query-variation algorithm does not currently invoke it automatically. Apply these steps in order:- Return an empty or whitespace-only input trimmed.
- Apply Unicode compatibility normalization, NFKC.
- Normalize Arabic characters to their Persian forms:
كtoک;يtoی;ىtoی.
- Remove Unicode non-spacing marks and tatweel (
ـ). - Apply the following mappings:
- Replace every Unicode whitespace run with one ordinary space.
- Trim leading and trailing spaces.
Complexity and limits
For each initial form, character expansion produces at most three kaf styles and four yeh styles. Terminalه/ة alternatives can double for every
affected word. Before duplicate removal, the upper bound is approximately:
E is the number of words ending in ه or ة.
For ordinary single-term searches the set stays small, and duplicate removal
usually reduces it substantially. Long phrases with many affected endings can
grow exponentially. Implementations should set reasonable query-length and
term-count limits, or cap generated variations.
Database predicate count grows approximately with:
%value% usually cannot
use a normal B-tree index efficiently.
Compatibility checklist
A port should test at least:- empty and whitespace-only queries;
- Arabic and Persian kaf:
كandک; - Arabic yeh, Persian yeh, and alif maqsura:
ي,ی, andى; - terminal
هandة; - Arabic text typed with a QWERTY layout selected, and the reverse;
- Caps Lock during QWERTY-to-Arabic repair;
- the
لاtwo-character mapping; - null searchable fields;
- one-term and multi-term candidate filtering;
- overlapping additive ranking conditions;
- records tied on relevance score;
%and_behavior if the backend uses SQL patterns;- canonical normalization of diacritics, tatweel, joiners, whitespace, and Arabic-script digits.
Worked examples
These examples combine the individual rules into complete inputs and outputs. Variation lists labeled “selected” are illustrative subsets because keyboard-layout expansion can add forms that are not relevant to the point being demonstrated.Canonical normalization
Wrong keyboard layout
If the user intended to enterاحمد but had the QWERTY layout selected, the
physical keystrokes appear as hpl].
Arabic and Persian forms
For the inputعلي, the selected variations include:
فاطمة, terminal-ending expansion includes:
Multiword ranking
Assume the query is:fullName and address, with fullName as the primary field:
All three records pass the candidate filter:
- A contains both terms in the primary field.
- B contains both terms in the primary field, but in the reverse order.
- C contains
عليin the primary field andاحمدin another searchable field.
The resulting order is:
Strict and loose filtering
Using the same records and the queryاحمد علي:
address = "كربلاء", it would match only علي:
