PHP code example of signify-nz / silverstripe-solr-search

1. Go to this page and download the library: Download signify-nz/silverstripe-solr-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/ */

    

signify-nz / silverstripe-solr-search example snippets


use Firesphere\SolrSearch\Indexes\BaseIndex;
use SilverStripe\Assets\File;
use SilverStripe\CMS\Model\SiteTree;

class MyIndex extends BaseIndex
{
    public function init()
    {
        $this->addClass(SiteTree::class);
        $this->addClass(File::class);

        $this->addFulltextField('Title');
        $this->addFulltextField('Content');

        $this->addFilterField('ClassName');
    }

    public function getIndexName()
    {
        return 'mysite-search';
    }
}

use Firesphere\SolrSearch\Indexes\BaseIndex;
use Firesphere\SolrSearch\Queries\BaseQuery;
use SilverStripe\Core\Injector\Injector;

class SearchPageController extends PageController
{
    public function getResults()
    {
        $term = $this->getRequest()->getVar('Search');
        if (!$term) {
            return null;
        }

        /** @var BaseIndex $index */
        $index = Injector::inst()->get(MyIndex::class);

        $query = Injector::inst()->get(BaseQuery::class);
        $query->addTerm($term);
        $query->setStart((int) $this->getRequest()->getVar('start'));

        return $index->doSearch($query); // returns a SearchResult
    }
}