Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Sphinxsearch Bundle Laravel Package

delocker/sphinxsearch-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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],
    ];
    
  2. 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
    
  3. 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();
        }
    }
    

Implementation Patterns

Querying Data

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();

Index Management

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']
]);

Integration with Doctrine

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'];
    }
}

Gotchas and Tips

Debugging

Connection Issues

  • Verify Sphinx server is running (netstat -tulnp | grep 9306).
  • Check sphinxsearch.log for errors.
  • Ensure host and port in config match your Sphinx setup.

Query Performance

  • Use EXPLAIN to analyze slow queries:
    $sphinxClient->query('search term')->setExplain(true)->getResults();
    
  • Avoid wildcards (*) at the start of search terms.

Configuration Quirks

Multi-Index Setups

  • Define multiple clients in clients config:
    clients:
        primary:
            host: '127.0.0.1'
            port: 9306
        replica:
            host: '192.168.1.100'
            port: 9306
            searchd: true
            indexer: false
    
  • Inject the specific client by type-hinting ClientInterface and using dependency injection tags.

Field Types

  • Sphinx field types (e.g., string, int, float) must match your schema. Mismatches cause runtime errors.

Extension Points

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>');
        }
    }
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver