Installation
Add the bundle to your composer.json:
composer require delocker/sphinxsearch-bundle
Enable it in config/bundles.php:
return [
// ...
Delocker\SphinxSearchBundle\DelockerSphinxSearchBundle::class => ['all' => true],
];
Configuration
Define Sphinx connection in config/packages/delocker_sphinx_search.yaml:
delocker_sphinx_search:
clients:
default:
host: '127.0.0.1'
port: 9306
indexer: true
searchd: true
First Use Case Inject the client into a service/controller:
use Delocker\SphinxSearchBundle\Client\ClientInterface;
class SearchController
{
public function __construct(private ClientInterface $sphinxClient) {}
public function search(string $query): array
{
return $this->sphinxClient->query($query)->getResults();
}
}
Basic Search
$sphinxClient->query('search term')
->setLimit(10)
->getResults();
Filtering
$sphinxClient->query('search term')
->addFilter('category_id', [1, 2, 3])
->getResults();
Sorting
$sphinxClient->query('search term')
->setSort('@weight desc, @id asc')
->getResults();
Reindexing
$sphinxClient->indexer()->rotate('your_index_name');
$sphinxClient->indexer()->build('your_index_name');
Real-Time Updates
$sphinxClient->indexer()->replace('your_index_name', [
['id' => 1, 'title' => 'New Title', 'content' => 'New Content']
]);
Sync Doctrine Entities
# config/packages/delocker_sphinx_search.yaml
delocker_sphinx_search:
doctrine:
enabled: true
entities:
App\Entity\Product:
index_name: products
fields:
- { name: 'title', type: 'string' }
- { name: 'description', type: 'string' }
Automatic Indexing
Use the SphinxIndexerListener to sync entities on prePersist, preUpdate, and preRemove:
// src/EventListener/SphinxIndexerListener.php
use Delocker\SphinxSearchBundle\EventListener\SphinxIndexerListener;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
class SphinxIndexerListener extends SphinxIndexerListener
{
public function __construct(
private ServiceEntityRepository $productRepository,
private ClientInterface $sphinxClient
) {
parent::__construct($this->sphinxClient);
}
public function getIndexedEntities(): array
{
return ['App\Entity\Product'];
}
}
Connection Issues
netstat -tulnp | grep 9306).sphinxsearch.log for errors.host and port in config match your Sphinx setup.Query Performance
EXPLAIN to analyze slow queries:
$sphinxClient->query('search term')->setExplain(true)->getResults();
*) at the start of search terms.Multi-Index Setups
clients config:
clients:
primary:
host: '127.0.0.1'
port: 9306
replica:
host: '192.168.1.100'
port: 9306
searchd: true
indexer: false
ClientInterface and using dependency injection tags.Field Types
string, int, float) must match your schema. Mismatches cause runtime errors.Custom Query Builders
Extend Delocker\SphinxSearchBundle\Query\QueryBuilder for reusable logic:
class CustomQueryBuilder extends QueryBuilder
{
public function withBoost(string $field, float $boost): self
{
$this->query .= " @$field^$boost";
return $this;
}
}
Event Listeners
Extend SphinxIndexerListener for custom indexing logic:
public function onPrePersist($entity)
{
if ($entity instanceof Product && $entity->isActive()) {
$this->sphinxClient->indexer()->replace('products', [$entity->toArray()]);
}
}
Schema Validation Validate Sphinx schema against Doctrine entities using a custom command:
use Delocker\SphinxSearchBundle\Validator\SphinxSchemaValidator;
class ValidateSchemaCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$validator = new SphinxSchemaValidator($this->sphinxClient, $this->entityManager);
$errors = $validator->validate();
if (!empty($errors)) {
$output->writeln('<error>Schema validation failed:</error>');
foreach ($errors as $error) {
$output->writeln($error);
}
} else {
$output->writeln('<info>Schema is valid.</info>');
}
}
}
How can I help you explore Laravel packages today?