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 Api Laravel Package

neutron/sphinxsearch-api

Composer-distributed PHP client library for the SphinxSearch API, making it easy to integrate SphinxSearch (for indexing/searching MySQL and PostgreSQL data) into PHP projects via Composer. Licensed under the GNU GPL.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require neutron/sphinxsearch-api
    

    Add to config/app.php under providers (if using Laravel):

    Neutron\SphinxSearch\SphinxSearchServiceProvider::class,
    
  2. Basic Configuration Create a config file at config/sphinxsearch.php:

    return [
        'host' => '127.0.0.1',
        'port' => 9312,
        'index' => 'your_index_name',
    ];
    

    Publish the config with:

    php artisan vendor:publish --provider="Neutron\SphinxSearch\SphinxSearchServiceProvider"
    
  3. First Query

    use Neutron\SphinxSearch\SphinxSearch;
    
    $sphinx = app(SphinxSearch::class);
    $results = $sphinx->query('search term')->getResults();
    

First Use Case: Simple Search

$sphinx = app(SphinxSearch::class);
$results = $sphinx->query('laravel framework')
    ->limit(10)
    ->getResults();

foreach ($results as $result) {
    echo $result->getAttribute('title') . "\n";
}

Implementation Patterns

Query Building

  1. Basic Query

    $query = app(SphinxSearch::class)
        ->query('search term')
        ->limit(10)
        ->offset(5);
    
  2. Filtering

    $query->addFilter('category_id', 5);
    $query->addFilter('status', 'active');
    
  3. Sorting

    $query->sortBy('price', SphinxSearch::SORT_ASC);
    $query->sortBy('relevance', SphinxSearch::SORT_DESC);
    
  4. Grouping

    $query->groupBy('author_id', '@group_weight price');
    

Integration with Laravel

  1. Service Provider Binding

    // In AppServiceProvider@boot()
    $this->app->singleton(SphinxSearch::class, function ($app) {
        return new SphinxSearch(config('sphinxsearch'));
    });
    
  2. Eloquent Scopes

    use Neutron\SphinxSearch\SphinxSearch;
    
    class ProductRepository
    {
        public function scopeSearch($query, $searchTerm)
        {
            $sphinx = app(SphinxSearch::class);
            $results = $sphinx->query($searchTerm)->getResults();
    
            return $query->whereIn('id', $results->pluck('id'));
        }
    }
    
  3. API Responses

    Route::get('/search', function () {
        $sphinx = app(SphinxSearch::class);
        $results = $sphinx->query(request('q'))->getResults();
    
        return response()->json($results);
    });
    

Batch Processing

$sphinx = app(SphinxSearch::class);
$sphinx->indexer()->rotate();
$sphinx->indexer()->build('your_index_name');

Gotchas and Tips

Common Pitfalls

  1. Connection Issues

    • Ensure Sphinx is running (netstat -tulnp | grep 9312).
    • Verify host and port in config match your Sphinx server.
  2. Attribute Mismatches

    • Sphinx attributes must match the schema. Use describe to check:
      $sphinx->describe('your_index_name');
      
  3. Case Sensitivity

    • Sphinx is case-sensitive by default. Use MATCH_MODE_ANY for case-insensitive:
      $query->setMatchMode(SphinxSearch::MATCH_MODE_ANY);
      
  4. Memory Limits

    • Large queries may hit PHP memory limits. Use limit() and pagination.

Debugging

  1. Enable Logging

    $sphinx = app(SphinxSearch::class);
    $sphinx->setDebug(true);
    $results = $sphinx->query('test')->getResults();
    // Check Laravel logs for raw SphinxQL
    
  2. Raw SphinxQL

    $query = $sphinx->query('test')->addFilter('id', 1);
    $rawQuery = $query->getRawQuery();
    // Execute manually in Sphinx CLI for testing
    

Performance Tips

  1. Index Optimization

    $sphinx->indexer()->optimize('your_index_name');
    
  2. Caching Results

    $cacheKey = 'sphinx_results_' . md5(request('q'));
    $results = Cache::remember($cacheKey, now()->addMinutes(5), function () {
        return $sphinx->query(request('q'))->getResults();
    });
    
  3. Async Indexing Use Laravel queues for heavy indexing tasks:

    dispatch(new IndexProductsJob($sphinx, $products));
    

Extension Points

  1. Custom Attributes Extend the Result class to handle custom attributes:

    class CustomResult extends \Neutron\SphinxSearch\Result
    {
        public function getCustomAttribute()
        {
            return $this->getAttribute('custom_field');
        }
    }
    
  2. Query Builders Create a fluent interface for complex queries:

    class SphinxQueryBuilder
    {
        protected $query;
    
        public function __construct(SphinxSearch $sphinx)
        {
            $this->query = $sphinx->query('');
        }
    
        public function forProducts()
        {
            $this->query->addFilter('type', 'product');
            return $this;
        }
    }
    
  3. Event Listeners Listen for Sphinx events (e.g., index updates):

    // In EventServiceProvider
    protected $listen = [
        'sphinx.indexing' => [
            \App\Listeners\LogIndexing::class,
        ],
    ];
    
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky