madebybramble / craft-bramble-search
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 1
Type:craft-plugin
pkg:composer/madebybramble/craft-bramble-search
Requires
- php: >=8.2
- craftcms/cms: ^5.5.0
Requires (Dev)
- craftcms/ecs: dev-main
- craftcms/phpstan: dev-main
- phpunit/phpunit: ^10.5
Suggests
- ext-mongodb: Required for MongoDB storage adapter
- ext-redis: Required for Redis storage adapter
This package is auto-updated.
Last update: 2025-12-26 14:52:51 UTC
README
A powerful search engine plugin for Craft CMS that replaces the default search service with an enhanced inverted index implementation. Designed for performance, accuracy, and flexibility.
โจ Features
- ๐ Inverted Index Architecture - Fast, efficient search using modern indexing techniques
- ๐ค Fuzzy Search - Find results even with typos using n-gram similarity with adaptive thresholds
- ๐งฎ BM25 Ranking Algorithm - Industry-standard relevance scoring for better results
- ๐ Multiple Storage Backends - Choose between Redis (fastest), File Storage, MySQL, MongoDB, or Craft cache
- ๐ Stop Word Removal - Filter out common words to improve search relevance
- ๐ Title Field Boosting - Prioritize matches in title fields (5x boost factor)
- ๐ Exact Phrase Matching - Boost results that contain the exact search phrase (3x boost factor)
- ๐๏ธ Craft Search Replacement - Seamlessly replaces Craft's built-in search service
- ๐ Multi-site Support - Each site has its own search index
- ๐ Automatic Indexing - Content is automatically indexed when created, updated, or deleted
- ๐งน Orphaned Term Cleanup - Automatically removes terms with no associated documents
- ๐ ๏ธ Console Commands - Tools for viewing index statistics and rebuilding the index
- ๐ AND Logic for Multiple Terms - Narrows results as more terms are added (unlike Craft's default OR logic)
- ๐ซ Revision Handling - Properly skips drafts, revisions, and provisional drafts during indexing
๐ Requirements
- ๐ง Craft CMS 5.5.0 or later
- ๐ป PHP 8.2 or later
- ๐ Redis 5.0+ (for Redis driver)
- ๐๏ธ MySQL 5.7.8+ or MariaDB 10.2.7+ (for MySQL driver)
- ๐ MongoDB 4.4+ (for MongoDB driver)
๐ฆ Installation
Via Plugin Store
- ๐ Open your Craft Control Panel
- ๐ Navigate to Plugin Store โ Search for "Bramble Search"
- โฌ๏ธ Click Install
Via Composer
# Navigate to your project directory cd /path/to/your-project # Install the plugin composer require made-by-bramble/craft-bramble-search # Install the plugin in Craft ./craft plugin/install bramble-search
๐ ๏ธ Configuration
Bramble Search can be configured via the Control Panel or a configuration file.
Configuration File Setup
Create a new file at config/bramble-search.php in your Craft project:
<?php // config/bramble-search.php return [ // Whether to enable the plugin and replace Craft's search service 'enabled' => false, // Storage driver: 'redis', 'file', 'mysql', 'mongodb', or 'craft' 'storageDriver' => 'mysql', // Redis connection settings (only needed if using Redis driver) 'redisHost' => 'localhost', 'redisPort' => 6379, 'redisPassword' => null, // MongoDB connection settings (only needed if using MongoDB driver) 'mongoDbUri' => 'mongodb://localhost:27017', 'mongoDbDatabase' => 'craft_search', // BM25 algorithm parameters (optional - defaults shown) 'bm25K1' => 1.5, // Term frequency saturation (0.1-5.0) 'bm25B' => 0.75, // Document length normalization (0.0-1.0) 'titleBoostFactor' => 5.0, // Title field boost multiplier (1.0-20.0) 'exactMatchBoostFactor' => 3.0, // Exact phrase match boost (1.0-20.0) // N-gram fuzzy search parameters (optional - defaults shown) 'ngramSizes' => [1, 2, 3], // N-gram sizes for fuzzy matching 'ngramSimilarityThreshold' => 0.25, // Minimum similarity threshold (0.0-1.0) 'fuzzySearchMaxCandidates' => 100, // Max fuzzy candidates to process ];
All settings can be overridden using environment variables:
BRAMBLE_SEARCH_DRIVER- Storage driver ('redis', 'file', 'mysql', 'mongodb', or 'craft')BRAMBLE_SEARCH_REDIS_HOST- Redis hostBRAMBLE_SEARCH_REDIS_PORT- Redis portBRAMBLE_SEARCH_REDIS_PASSWORD- Redis passwordBRAMBLE_SEARCH_MONGODB_URI- MongoDB connection URIBRAMBLE_SEARCH_MONGODB_DATABASE- MongoDB database name
๐ Getting Started
Initializing the Search Index
After installing and enabling the plugin, you need to build the search index:
- Go to Utilities โ Clear Caches in the Control Panel
- Check the Bramble Search option
- Click Clear Caches
Alternatively, you can use the command line:
# Build the search index
./craft clear-caches/bramble-search
This will queue a job to rebuild the search index for the current site.
Basic Usage
Since Bramble Search replaces Craft's built-in search service, you can use Craft's standard search functionality in your templates. No special template variables are needed.
Standard Craft Search
{# Use Craft's standard search functionality #} {% set entries = craft.entries() .search('your search query') .all() %} {# Display results #} {% for entry in entries %} <article class="search-result"> <h2><a href="{{ entry.url }}">{{ entry.title }}</a></h2> <p>{{ entry.summary }}</p> </article> {% endfor %}
Search with Pagination
{# Use Craft's standard search with pagination #} {% set entriesQuery = craft.entries() .search('your search query') %} {# Paginate the results #} {% paginate entriesQuery as pageInfo, pageEntries %} {# Display results #} {% for entry in pageEntries %} <article class="search-result"> <h2><a href="{{ entry.url }}">{{ entry.title }}</a></h2> </article> {% endfor %} {# Display pagination #} {% if pageInfo.totalPages > 1 %} <nav class="pagination"> {% if pageInfo.prevUrl %} <a href="{{ pageInfo.prevUrl }}">Previous</a> {% endif %} <span class="current">{{ pageInfo.currentPage }}</span> {% if pageInfo.nextUrl %} <a href="{{ pageInfo.nextUrl }}">Next</a> {% endif %} </nav> {% endif %}
๐ Advanced Usage
AJAX Search with Craft
You can use Craft's built-in ElementAPI or GraphQL to create AJAX search experiences:
// In your JavaScript file document.getElementById('search-form').addEventListener('submit', function(e) { e.preventDefault(); const query = this.querySelector('input[name="q"]').value; // Using Craft's Element API endpoint fetch(`/api/entries.json?search=${encodeURIComponent(query)}`) .then(response => response.json()) .then(data => { const resultsContainer = document.getElementById('search-results'); resultsContainer.innerHTML = ''; if (data.length === 0) { resultsContainer.innerHTML = '<p class="no-results">No results found</p>'; return; } const resultsList = document.createElement('ul'); data.forEach(entry => { const item = document.createElement('li'); item.innerHTML = ` <a href="${entry.url}"> <h3>${entry.title}</h3> </a> `; resultsList.appendChild(item); }); resultsContainer.appendChild(resultsList); }); });
Programmatic Search
You can use Craft's standard ElementQuery API with search parameters:
// In your PHP code use craft\elements\Entry; // Perform a search $entries = Entry::find() ->search('your search query') ->siteId(Craft::$app->sites->currentSite->id) ->section(['blog', 'news']) ->limit(20) ->orderBy('score') ->all(); // Process results foreach ($entries as $entry) { echo $entry->title; }
โ๏ธ Performance Tuning
Storage Driver Selection
Choose the right storage driver based on your site's needs:
| Driver | Best For | Pros | Cons |
|---|---|---|---|
| Redis | Medium to large sites | Fastest performance, persistent storage | Requires Redis server setup |
| File Storage | Small to medium sites | Custom binary format, file locking for concurrent access | Slow on networked file systems |
| MySQL (default) | Small to medium sites | Persistent storage, n-gram optimization, no additional dependencies | Adds load to Craft's database |
| MongoDB | Medium to large sites | Flexible schema, excellent scalability, optimized n-gram aggregation | Requires MongoDB server setup |
| Craft Cache | Small to medium sites | Easy setup, no additional dependencies | Limited persistence, not scalable |
Indexing Considerations
- The plugin automatically skips drafts, revisions, and provisional drafts during indexing
- The plugin indexes all searchable attributes and fields as defined by Craft's ElementHelper::searchableAttributes()
- Title fields receive special handling with 5x boosting for better relevance
- N-gram indexing provides 10-100x faster fuzzy search compared to traditional Levenshtein distance
- Adaptive similarity thresholds optimize fuzzy matching for short terms (2-3 characters)
๐ ๏ธ Command Line Tools
Index Management
The primary way to rebuild the search index is through the Clear Caches utility in the Control Panel or by using the clear-caches command:
# Rebuild the search index
./craft clear-caches/bramble-search
This will queue a job that processes entries in batches to avoid memory issues, making it suitable for sites with large numbers of entries.
Statistics
View detailed information about your search index:
# View basic index statistics ./craft bramble-search/stats # View detailed statistics including top terms ./craft bramble-search/stats --detailed # Specify a storage driver ./craft bramble-search/stats --driver=redis ./craft bramble-search/stats --driver=file ./craft bramble-search/stats --driver=mysql ./craft bramble-search/stats --driver=mongodb ./craft bramble-search/stats --driver=craft
The statistics command provides information about:
- Total number of documents in the index
- Total number of unique terms
- Total number of tokens
- Average document length
- Estimated storage size
- Top terms by document frequency (with --detailed flag)
- Term-to-document ratio and other health metrics (with --detailed flag)
๐ Automatic Indexing
Bramble Search automatically indexes entries when they are:
- Created
- Updated
- Deleted
๐๏ธ How It Works
Bramble Search completely replaces Craft's built-in search service when enabled. This is the core functionality of the plugin.
When you enable the plugin by setting enabled = true, it:
- Registers its own search adapter as Craft's search service
- Intercepts all search queries from both the Control Panel and frontend
- Processes searches using its enhanced inverted index and BM25 ranking
- Implements AND logic for multiple search terms (requiring all terms to be present)
- Applies n-gram similarity fuzzy matching when exact matches aren't found
What This Affects
When enabled, Bramble Search will:
- ๐๏ธ Replace Admin CP Search - All searches in the Control Panel will use Bramble Search
- ๐ Replace Frontend Element Queries - Any
craft.entries.search()queries will use Bramble Search - ๐ Handle Element Exports - Search-based element exports will use Bramble Search
- ๐ข Fix Element Counts - Search result counts in the Control Panel will be accurate
- ๐ Improve Search Relevance - Results are ranked using BM25 with title and exact match boosting
โ ๏ธ Important: After enabling the plugin, you must build your search index using the Clear Caches utility in the Control Panel or by running
./craft clear-caches/bramble-search.
๐ License
Bramble Search is licensed under a proprietary license. See the LICENSE.md file for details.
๐จโ๐ป Support
For support, please contact hello@madebybramble.co.uk.
Made with โค๏ธ by Made By Bramble