develop
PCM Search Bundle
Provides a search system for Symfony projects.
Installation
Add the repo to your composer.json file:
"repositories": [
{
"type": "vcs",
"url": "https://git.pcmdev.co.uk/pcm-libraries/pcm-search-bundle.git"
}
]
Then install the bundle via Composer:
composer require pcm/search-bundle
Usage
Make any entity searchable by implementing SearchableInterface. The bundle's SearchService will then index, re-index, and remove entries automatically via Doctrine event subscribers.
To (re)build the index for all searchable entities, run:
php bin/console pcm:search:reindex
Inject Pcm\SearchBundle\Service\SearchService into your own services to query, index, or un-index entities programmatically.
Adding a search bar
Make your entity searchable by implementing SearchableInterface:
use Pcm\SearchBundle\Interface\SearchableInterface;
#[ORM\Entity]
class Company implements SearchableInterface
{
public function getSearchTitle(): string
{
return $this->name;
}
public function getSearchValues(): array
{
return [
$this->name,
$this->website
];
}
}
Query the index from a controller and resolve each hit to a URL:
#[Route('/search', name: 'app_search', methods: ['GET'])]
public function search(Request $request, SearchIndexRepository $repo): Response
{
$routes = [Company::class => 'app_company_show'];
$rows = $repo->findAllPagination($request->query->get('q', ''))->getResult();
$results = [];
foreach ($rows as [$index]) {
$route = $routes[$index->getEntityClass()] ?? null;
if ($route === null) {
continue;
}
$results[] = [
'title' => $index->getTitle(),
'url' => $this->generateUrl($route, ['id' => $index->getEntityId()]),
];
}
return $this->render('search/_results.html.twig', ['results' => $results]);
}
Each row from findAllPagination() is [SearchIndex $hit, 'score' => float].
Description
Languages
PHP
96.8%
Just
2.2%
Dockerfile
1%