quran-search-engine

Search Syntax

quran-search-engine processes incoming queries using string tokenization and AND logic intersecting.

AND Logic Requirements

When submitting multi-word queries, the query string is broken down.

The engine intersects matches per token. This guarantees all returned results successfully fulfilled every query term either by exact match, lemma fallback, root fallback, or fuzzy match fallback conditionally based on configuration.

const response = search('الله الرحمن', quranData, morphologyMap, wordMap, {
  lemma: true,
  root: true,
});
// Internally:
// Token 1 => Matches 'الله'
// AND
// Token 2 => Matches 'الرحمن'
// Result => Sura 1:1, 1:3, etc.

Advanced Search Types

Beyond multi-word string queries, the engine supports multiple alternative syntaxes enabled via the SearchOptions:

// Find all verses ending with "ون"
const response = search('^.*ون$', quranData, morphologyMap, wordMap, {
  lemma: false,
  root: false,
  isRegex: true,
});

// Find verses containing "الله" followed by "الرحمن" with any text between them
const response2 = search('الله.*الرحمن', quranData, morphologyMap, wordMap, {
  lemma: false,
  root: false,
  isRegex: true,
});

Boolean Search Helper Functions

The library exposes two utility functions for working with boolean queries:

hasBooleanOperators(query)

Checks if a query string contains boolean operators (+, -, |).

import { hasBooleanOperators } from 'quran-search-engine';

hasBooleanOperators('+الله -الرحمن'); // Returns: true
hasBooleanOperators('الله الرحمن'); // Returns: false
clearBooleanOperators(query)

Removes all boolean operators from a query and normalizes whitespace. Useful for creating a clean fallback query when boolean mode isn’t enabled.

import { clearBooleanOperators } from 'quran-search-engine';

clearBooleanOperators('+الله | الرحمن -الجحيم');
// Returns: 'الله الرحمن الجحيم'

clearBooleanOperators('محمد | رسول');
// Returns: 'محمد رسول'

String Checking For Arbitrary Filters

For developers requiring strict Boolean checks independent of scoring across any internal dataset.

import { normalizeArabic } from 'quran-search-engine';

export function containsAllTokens(value: string, query: string): boolean {
  const normalizedQuery = normalizeArabic(query);
  if (!normalizedQuery) return false;

  const tokens = normalizedQuery.split(/\s+/);
  const normalizedValue = normalizeArabic(value);
  return tokens.every((token) => normalizedValue.includes(token));
}