Add content to readme
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
# PCM Search Bundle
|
||||
|
||||
Provides a search system for Symfony projects.
|
||||
|
||||
## Installation
|
||||
|
||||
Add the repo to your `composer.json` file:
|
||||
```json
|
||||
|
||||
"repositories": [
|
||||
{
|
||||
"type": "vcs",
|
||||
"url": "https://git.pcmdev.co.uk/pcm-libraries/pcm-search-bundle.git"
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
Then install the bundle via Composer:
|
||||
|
||||
```sh
|
||||
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:
|
||||
|
||||
```sh
|
||||
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`:
|
||||
|
||||
```php
|
||||
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:
|
||||
|
||||
```php
|
||||
#[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]`.
|
||||
|
||||
Reference in New Issue
Block a user