PHP code example of toastnz / opensearch

1. Go to this page and download the library: Download toastnz/opensearch 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/ */

    

toastnz / opensearch example snippets


use SilverStripe\Assets\File;
use SilverStripe\CMS\Model\RedirectorPage;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\CMS\Model\VirtualPage;
use SilverStripe\ErrorPage\ErrorPage;
use Toast\OpenSearch\Search\OpenSearchIndex;

class SiteSearchIndex extends OpenSearchIndex
{
    public function __construct(?string $indexName = null)
    {
        $this->indexName = $indexName ?? 'default_index';
        $this->fields = [
            'Title' => ['type' => 'text'],
            'Content' => ['type' => 'text'],
            'PublishDate' => 'Datetime',
        ];
        $this->searchFields = [
            'Title^2',
            'Content',
            'SearchContent',
            'ElementalArea.Elements.Title',
        ];
        $this->filters = [
            'SubsiteID' => ['type' => 'integer'],
            'PublishDate' => 'Datetime',
        ];
        $this->

use Toast\OpenSearch\Helpers\OpenSearch;

$results = OpenSearch::singleton()->search('annual report', null, [
    'filters' => [
        'SubsiteID' => 2,
        'PublishDate' => ['gte' => '2026-01-01'],
    ],
]);