PHP code example of lmc / cqrs-solr

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

    

lmc / cqrs-solr example snippets


$searchInput = $_GET['search'];

$selectPersons = $client->createSelect();
$selectPersons->getEDisMax()->setQueryFields('name^100 age^50');
$selectPersons->setQuery($searchInput);
$selectPersons->setNumberOfRows(30);

$result = $client->execute($selectPersons);

class PersonSearch implements FulltextInterface
{
    private string $searchInput;

    public function __construct(string $searchInput)
    {
        $this->searchInput = $searchInput;
    }

    public function getKeywords(): array
    {
        return explode(' ', $this->searchInput);
    }

    public function getNumberOfRows(): int
    {
        return 30;
    }

    public function getQueryFields(): array
    {
        return [
            'name^100',
            new SolrField('age', '', 0, 50),     // you can also use a SolrField value object, so you don't need to remember how is a prioritized value built
        ];
    }

    public function isEDisMaxEnabled(): bool
    {
        return true;
    }

    public function useEDisMaxGlobally(): bool
    {
        return true;
    }

    // Note: there are more methods, you need to implement, but we want to keep this example simple as possible. If you don't need other functionality, simply return null or empty variant from a method.
}

$searchInput = $_GET['search'];

$selectPersonsEntity = new PersonSearch($searchInput);
$selectPersonsQuery = $queryBuilder->buildQuery($selectPersonsEntity);

$result = $queryFetcher->fetchAndReturn($selectPersonsQuery);