I'm Ashraf Magdy ghs, and I build AshMovis, an Arabic index of films, series and actors. The hardest part of the project was never importing the data. It was the search box.
Type a film title into a database that already holds that exact title and you can get nothing back. The two strings look identical on screen and compare as different bytes, because Arabic text arrives with variants that are invisible.
These are the rules I ended up with, in the order they mattered.
1. Strip what nobody types
Tashkeel (fatha, damma, kasra, shadda, sukun), tatweel, and the small marks around letters.
import re, unicodedata MARKS = re.compile(r'[\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06ED\u0640]') def normalize_ar(text: str) -> str: text = unicodedata.normalize('NFKC', text) text = MARKS.sub('', text) # tashkeel + tatweel text = re.sub('[أإآٱ]', 'ا', text) # alef forms -> bare alef text = text.replace('ة', 'ه') # ta marbuta -> ha text = re.sub('[ىي]', 'ي', text) # alef maqsura -> ya text = text.replace('ؤ', 'و').replace('ئ', 'ي') text = re.sub(r'\s+', ' ', text).strip() return text Two notes on that function. Replacing ta marbuta with ha and alef maqsura with ya changes how a word is spelled, so run it on a shadow column, never on the text you display. And normalize to NFKC first: some sources hand you Arabic presentation forms, which are the same letters in different codepoints.
2. Treat the definite article as optional
"الكيت كات" and "كيت كات" are the same film to a person and two different keys to a database. Index both forms: the normalized string, and the same string with a leading "ال" removed.
ALTER TABLE titles ADD COLUMN search_ar VARCHAR(255) AS (normalize_ar(title_ar)) STORED, ADD COLUMN search_ar_bare VARCHAR(255) AS (TRIM(LEADING 'ال' FROM normalize_ar(title_ar))) STORED, ADD INDEX (search_ar), ADD INDEX (search_ar_bare); MySQL has no built-in normalize_ar, so in my case the value is computed in the application before insert; the point is that both forms are stored and indexed, not calculated at query time.
3. Latin spellings are a many-to-one problem
Half the people looking for an Egyptian actor type his name in Latin letters. "Abdel Rahman", "Abd El Rahman", "Abdulrahman" and "عبد الرحمن" are one person, and no algorithm turns those into each other reliably. So I store what people actually type: an alias table, one row per spelling, pointing at the canonical person.
CREATE TABLE person_aliases ( person_id INT NOT NULL, alias VARCHAR(190) NOT NULL, alias_norm VARCHAR(190) NOT NULL, PRIMARY KEY (alias_norm), KEY (person_id) ); Every new spelling that shows up in the search log becomes a row. The table grows slowly and search gets better every month.
4. Let the year break ties
A title on its own is often ambiguous, because remakes and same-name productions are normal in Arabic cinema. Put the year next to every result, and when the query contains a year, prefer the exact match over a fuzzy one.
5. Test with the queries people actually type
Not with your own data. Pull the top queries from the search log, take the ones that returned zero results, and check whether normalization would have found something. That list taught me more about Arabic search than any general advice, and it keeps producing work: every zero-result query is either a missing record or a missing alias.
The whole trick is small: normalize on write, store both forms, keep an alias table for the spellings nobody can predict, and let the year settle the rest.
