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.
Installation
composer require neutron/sphinxsearch-api
Add to config/app.php under providers (if using Laravel):
Neutron\SphinxSearch\SphinxSearchServiceProvider::class,
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"
First Query
use Neutron\SphinxSearch\SphinxSearch;
$sphinx = app(SphinxSearch::class);
$results = $sphinx->query('search term')->getResults();
$sphinx = app(SphinxSearch::class);
$results = $sphinx->query('laravel framework')
->limit(10)
->getResults();
foreach ($results as $result) {
echo $result->getAttribute('title') . "\n";
}
Basic Query
$query = app(SphinxSearch::class)
->query('search term')
->limit(10)
->offset(5);
Filtering
$query->addFilter('category_id', 5);
$query->addFilter('status', 'active');
Sorting
$query->sortBy('price', SphinxSearch::SORT_ASC);
$query->sortBy('relevance', SphinxSearch::SORT_DESC);
Grouping
$query->groupBy('author_id', '@group_weight price');
Service Provider Binding
// In AppServiceProvider@boot()
$this->app->singleton(SphinxSearch::class, function ($app) {
return new SphinxSearch(config('sphinxsearch'));
});
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'));
}
}
API Responses
Route::get('/search', function () {
$sphinx = app(SphinxSearch::class);
$results = $sphinx->query(request('q'))->getResults();
return response()->json($results);
});
$sphinx = app(SphinxSearch::class);
$sphinx->indexer()->rotate();
$sphinx->indexer()->build('your_index_name');
Connection Issues
netstat -tulnp | grep 9312).host and port in config match your Sphinx server.Attribute Mismatches
describe to check:
$sphinx->describe('your_index_name');
Case Sensitivity
MATCH_MODE_ANY for case-insensitive:
$query->setMatchMode(SphinxSearch::MATCH_MODE_ANY);
Memory Limits
limit() and pagination.Enable Logging
$sphinx = app(SphinxSearch::class);
$sphinx->setDebug(true);
$results = $sphinx->query('test')->getResults();
// Check Laravel logs for raw SphinxQL
Raw SphinxQL
$query = $sphinx->query('test')->addFilter('id', 1);
$rawQuery = $query->getRawQuery();
// Execute manually in Sphinx CLI for testing
Index Optimization
$sphinx->indexer()->optimize('your_index_name');
Caching Results
$cacheKey = 'sphinx_results_' . md5(request('q'));
$results = Cache::remember($cacheKey, now()->addMinutes(5), function () {
return $sphinx->query(request('q'))->getResults();
});
Async Indexing Use Laravel queues for heavy indexing tasks:
dispatch(new IndexProductsJob($sphinx, $products));
Custom Attributes
Extend the Result class to handle custom attributes:
class CustomResult extends \Neutron\SphinxSearch\Result
{
public function getCustomAttribute()
{
return $this->getAttribute('custom_field');
}
}
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;
}
}
Event Listeners Listen for Sphinx events (e.g., index updates):
// In EventServiceProvider
protected $listen = [
'sphinx.indexing' => [
\App\Listeners\LogIndexing::class,
],
];
How can I help you explore Laravel packages today?