PHP code example of contentreactor / craft-search

1. Go to this page and download the library: Download contentreactor/craft-search library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

contentreactor / craft-search example snippets


return [
    'facets' => [
        // A path of field and attribute handles resolved on each page, as a keyword
        'units' => 'units.id',
        // Paths can be scoped to nested entries of a type, like eager-loading paths
        'speakers' => 'blocks.talk:speaker.title',
        'published' => ['type' => 'date', 'value' => 'postDate', 'label' => 'Published', 'width' => 'full'],
    ],
];

return [
    'searchIndex' => [
        'class' => \ContentReactor\Search\Indexes\DatabaseIndex::class,
        'textSearchConfigs' => ['de' => 'german_unaccent', 'pl' => 'polish'],
    ],
];

$event->query->synonyms = [['concert', 'gig', 'show']];

use ContentReactor\Search\Events\DefineIndexableEvent;

Event::on(Search::class, Search::EVENT_DEFINE_INDEXABLE, function (DefineIndexableEvent $event) {
    // e.g. leave out thank-you pages, wherever they are
    $event->isIndexable = $event->isIndexable && !str_starts_with((string)$event->page->slug, 'thank-you');
});

use ContentReactor\Search\Events\SearchEvent;
use ContentReactor\Search\Services\Search;
use yii\base\Event;

// Change the query. Filters are added the way requests send them.
Event::on(Search::class, Search::EVENT_BEFORE_SEARCH, function (SearchEvent $event) {
    $event->query->filters['published'] = ['min' => '2024-01-01'];

    // Or answer the search without the index, e.g. from a cache
    // $event->results = $cachedResults;
});

// Change or replace the results
Event::on(Search::class, Search::EVENT_AFTER_SEARCH, function (SearchEvent $event) {
    $event->results->elements = array_filter($event->results->elements, fn($element) => $element->enabled);
});