Skip to main content
The Quran Search Engine supports multi-word queries and uses AND logic to ensure all query tokens are present in matching verses.

Query tokenization

When you search for multiple words, the query is automatically tokenized:
Process:
  1. Strip non-Arabic characters (keeps only Arabic letters and spaces)
  2. Normalize the query (unify alef variants, remove diacritics)
  3. Split by whitespace into individual tokens
Each word in your query becomes a separate token that must be matched independently.

Example tokenization

Input query: الله الرحمن After processing:

AND logic implementation

All search layers use AND logic: every token must match for a verse to be included in results.

Simple search AND logic

The simple search layer uses Array.every() to enforce AND logic:
What this does:
  • For each verse, check if every token appears in the text
  • Only return verses where all tokens are found
  • If even one token is missing, the verse is excluded
The .every() method returns true only if all tokens pass the test. If any token fails, the entire verse is rejected.

Linguistic search AND logic

The advanced linguistic search performs AND logic through set intersection:
Process:
  1. Find matches for each token independently
  2. Start with the first token’s matching verse IDs
  3. Filter to keep only IDs that also match the second token
  4. Continue filtering for each subsequent token
  5. Result: Only verses that match all tokens
The algorithm short-circuits early if any token has zero matches, avoiding unnecessary computation.

Visual example of set intersection

Query: الله الرحمن
Only verses 1, 3, and 5 contain both tokens, so only they are returned.

Per-token match types

Each token can match through different layers:
  • Linguistic match: Lemma or root match found for this token
  • Fuzzy match: Fuse.js found an approximate match for this token
Key insight:
  • Each token independently tries linguistic search first
  • If that fails, it falls back to fuzzy search
  • The final results must satisfy all tokens via their respective match types

Multi-word scoring

Scoring accumulates across all matched tokens:

Scoring example

Query: الله الرحمن
Verse: “بسم الله الرحمن الرحيم”
Matches found:
  • Token الله: 1 exact match → +3 points
  • Token الرحمن: 1 exact match → +3 points
Total score: 6
Verses matching more tokens or matching tokens multiple times receive higher scores.

Examples

Example 1: Two-word exact match

Example 2: Three-word query

Example 3: Mixed match types

Query: صلى محمد Possible results:
  • Verse A: صلى (exact) + محمد (exact) → score: 6, matchType: ‘exact’
  • Verse B: يصلون (lemma for صلى) + محمد (exact) → score: 5, matchType: ‘exact’
  • Verse C: صلاة (root for صلى) + محمد (exact) → score: 4, matchType: ‘exact’
All results contain both tokens, but through different match layers. The matchType reflects the best match quality found in that verse.

Example 4: No results when missing a token

Query: الله قرآن Verse text: “بسم الله الرحمن الرحيم” Result: This verse is excluded because it contains الله but not قرآن. Both tokens must match.

Why AND logic?

AND logic provides: ✓ Precision: Results are more specific and relevant
✓ User expectation: Matches natural language search behavior
✓ Reduced noise: Eliminates verses that only partially match
✓ Better UX: Users can refine searches by adding words
To find verses with ANY of your search terms (OR logic), run separate searches and merge the results in your application code.

Common use cases

Searching for phrases

While this isn’t phrase search (word order doesn’t matter), multi-word search effectively finds verses containing all words:

Narrowing results

Adding more words reduces the result set to more specific matches.

Performance considerations

The search engine optimizes multi-word queries through:
  1. Early termination: If any token has zero matches, stop immediately
  2. Set operations: Efficient intersection using Set data structure
  3. Deduplication: Verses appear only once even if matched by multiple layers
For best performance with long queries, put more specific/rare words first. However, the search engine handles token order automatically through optimization.