Integrated Search Engine
Pragma CMS includes a highly optimized, universally integrated Search Engine out of the box. It indexes all content types, taxonomies, and custom pages into a single, unified table, allowing for lightning-fast, typo-tolerant queries across your entire application.
The Hybrid Search Architecture (Fuzzy Search)
Instead of relying on heavy external dependencies like Elasticsearch or Algolia for basic sites, Pragma CMS implements a hybrid SQL + PHP Levenshtein algorithm to achieve high-performance "Fuzzy Search" (Typo tolerance).
This algorithm operates in two phases inside the SearchManager::search() method:
1. The "Wide Net" (SQL)
To prevent PHP memory exhaustion, the database first retrieves a maximum of 100 potential candidates. It uses a dynamic LIKE query to find any record where the title or searchable content contains at least one word from the normalized search query.
2. The "Fine Sorting" (PHP Levenshtein)
Once the candidates are retrieved, PHP evaluates them in memory using the Levenshtein distance algorithm.
- It calculates the exact number of character differences between the search query and the candidate titles.
- Exact matches are given an absolute priority bonus.
- Results with a distance strictly below the defined threshold (e.g.,
< 3typos) are preserved. - Finally, the array is strictly sorted by relevance (lowest distance first), and the
SearchManagerdynamically generates a contextual text excerpt highlighting the keyword.
Automatic Content Indexing
You do not need to manually manage the search index. Pragma CMS uses the core event system (Hooks::class) to keep the index perfectly synchronized in real-time.
ON_ENTRY_SAVE: Whenever a Page, Article, or custom Entry is saved, the core automatically strips HTML tags, resolves media metadata (Alt texts), flattens taxonomy relationships into JSON, and performs anINSERT ON DUPLICATE KEY UPDATE(Upsert) into thesearch_indextable.ON_TAXONOMY_TERM_SAVE: Automatically indexes tags, categories, and custom taxonomy terms. If a taxonomy term is renamed, the CMS intelligently finds all connected entries and triggers a background re-index to keep their JSON metadata up-to-date.
The Polymorphic Search API
Pragma CMS exposes a universal JSON endpoint (api/content/search.php) that acts as the backbone for both the Admin UI (e.g., Autocomplete fields, Gallery pickers) and the Front-end UI (Live AJAX Search).
API Modes
Depending on the provided $_GET parameters, the endpoint behaves differently:
- Search Mode (
?search=keyword): Returns a flat list of matching items across all content types. It natively enforces security logic (Administrators can see Drafts, Public users only see Published content). - Navigation Mode (Empty query): Returns a categorized, tree-like structure of the most recent content. This is used when a user clicks an empty autocomplete field in the Admin panel.
- Link Resolution (
?link_data={json}): Decodes an internal link payload to retrieve the exact Title, HTTP URL, and Slugs for a given ID without requiring complex database joins on the front end.
Front-end Implementation (Live Search)
By default, Pragma CMS provides a robust, accessible Javascript UI for the search modal (initSearchUI() and initSearchLogic()).
Features of the Native JS Implementation:
- Debouncing: Queries are delayed by 300ms while the user types to prevent database flooding.
- Keyboard Accessibility: Users can close the modal using the
Escapekey. - Polymorphic Display: Results dynamically display their content type badge (e.g., "Article", "Page", "Product").
- Fallback to Traditional Search: If the user presses "Enter" or clicks "View all results", they are seamlessly redirected to the physical
pages/search.phpcontroller, which utilizes theSearchManagerdirectly for a traditional, paginated result view.