PHPackages                             mnshankar/sphinxql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. mnshankar/sphinxql

ActiveLibrary[Search &amp; Filtering](/categories/search)

mnshankar/sphinxql
==================

Integrate SphinxQL with Laravel 4. Simplifies Laravel-Sphinx(RT) search

1.1(11y ago)103.7k14[1 PRs](https://github.com/mnshankar/SphinxQL/pulls)MITPHPPHP &gt;=5.3.0

Since Dec 5Pushed 10y ago1 watchersCompare

[ Source](https://github.com/mnshankar/SphinxQL)[ Packagist](https://packagist.org/packages/mnshankar/sphinxql)[ RSS](/packages/mnshankar-sphinxql/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

SphinxQL library for Laravel 4
------------------------------

[](#sphinxql-library-for-laravel-4)

This is a simple library that will help you to query a sphinx search server using SphinxQL.
My main motivation for putting together this package was to interface easily with Sphinx Real-time indexes on Laravel 4 (Updating rt indexes is ONLY possible using SphinxQL)

As an added bonus, SphinxQL is much more performant than SphinxAPI:

Installation
------------

[](#installation)

Add `mnshankar/sphinxql` to `composer.json`.

```
    "mnshankar/sphinxql": "1.0"
```

Run `composer update` to pull down Sphinxql. Note that Sphinxql has a dependency on 'FoolCode/SphinxQL-Query-Builder', which does much of the weight lifting ()

Now open up `app/config/app.php` and add the service provider to your `providers` array.

```
    'providers' => array(
        'mnshankar\Sphinxql\SphinxqlServiceProvider',
    )
```

and the alias:

```
    'aliases' => array(
        'SphinxQL'         => 'mnshankar\Sphinxql\Facades\SphinxqlFacade',
    )
```

If you need to override the default configuration options (server/port), please use the config publish command

```
php artisan config:publish mnshankar/sphinxql
```

RT (Real-Time) Indexes in Sphinx
--------------------------------

[](#rt-real-time-indexes-in-sphinx)

The main differences between the conventional and RT indexes in Sphinx are:

1. RT uses a "push" model. This means that The task of keeping the index in sync with your database is delegated to your application (remember there is no "indexer" in an RT scheme). So, typically when using an RT index, the index starts out empty and gets populated over time using SphinxQL queries sent by your application.
2. RT uses more RAM than the conventional indexer approach. Here is an informative blog detailing why: Also, for more information, be sure to read the internals of RT at : In either type of indexing strategy (conventional or RT), for best results, you are recommended to dedicate *as much RAM* as is possible to your sphinx server. So, the additional RAM requirement should not really deter you from using RT.
3. A major pro for RT indexes is that they are much easier to setup and manage. There is no need for messing with cron jobs as the "indexer" component is not used (fewer moving parts). No main-delta schemes to worry about (typically). And, ofcourse your index is live with search data instantly!

The more recent versions of Sphinx (2.1.1+) have made enormous strides in making RT indexes production ready:

The current version uses sensible configuration defaults.. so you can have a clean sphinx.conf file that takes care of the most common scenarios out of the box.

Here is the (minimal) sphinx.conf file used in examples below (for rt indexing):

```
index rt_test
{
    type = rt
    path = /var/lib/sphinxsearch/data/rt
    rt_field = title
    rt_field = content
    rt_attr_uint = gid
}
searchd
{
    # Configure the searchd listening port.
    listen = 9306:mysql41
    binlog_path = /var/lib/sphinxsearch/data
    pid_file = /var/www/sphinx-rt/app/storage/sphinx/searchd.pid

    # sudo searchd -c sphinx.conf - to start search daemon listening on above port
    # mysql -P 9306 -h 127.0.0.1 - connect to sphinx server daemon
}
```

Query Builder Documentation
---------------------------

[](#query-builder-documentation)

The SphinxQL query builder package for PHP developed and made available by the kind folks at foolcode is VERY well documented and tested. I strongly recommend you go through their webpage at :

Misc Usage Tips
---------------

[](#misc-usage-tips)

Using Laravel 4 model events (), it is trivial to ensure that your model stays in sync with your index!

Consider the following snippets that can be easily added into "created", "updated" and "deleted" events for your model:

```
Blog::created(function($model){
	$qins = SphinxQL::query()->insert()->into('rt_test');
	$qins->set(array('id'=>99, 'title'=>'My Title', 'content'=>'My Content', 'gid'=>444))->execute();
	//more realistically, it will look something like
	//$qins->set($model->toArray())->execute();
});
```

Similarly, Replace and delete can be easily handled like so:

```
Blog::updated(function($model){
	$qrepl = SphinxQL::query()->replace()->into('rt_test');
	$qrepl->set(array('id'=>99, 'title'=>'My Title', 'content'=>'My Content', 'gid'=>444))->execute();
});
```

```
Blog::deleted(function($model){
	SphinxQL::query()->delete()->from('rt_test')->where('id',$model->id)->execute();
});
```

A search query can be constructed as:

```
$q = SphinxQL::query()->select()->from('rt_test')->match('content', 'test');
	       ->execute();
```

The above statement returns an array of hits (if found).

View the generated sql statement:

```
dd($q->compile()->getCompiled());
```

Please refer to the documentation () for all available options.

Get the Meta info:

```
SphinxQL::query()->meta();
```

It is also possible to run a raw sql query against the server like so:

```
$q = SphinxQL::raw('select * from rt_test');
```

You can pass in any valid SphinxQL statement as a parameter to the raw() function.

Integration with Eloquent
-------------------------

[](#integration-with-eloquent)

This package makes it really easy to integrate the results of a search with database rows. Remember that a sphinx query only returns a hit array containing ID's. It is upto the application to issue queries against the database to retrieve the actual table rows.

```
$q = SphinxQL::query()->select()
			->from('rt_test')
			->match('content', 'test')
	       	->execute();

dd(SphinxQL::with($q)->get('Blog'));
```

The first statement runs the query against the sphinx server and returns an array.

The "with()" function takes the "hit" array returned by the Sphinx search engine and chains it to the "get". The "get()" function has the following signature:

```
public function get($name=null, $key='id')
```

where $name is either:

1. null - The function simply returns an array of ID's
2. An eloquent model - The function returns an EloquentCollection containing table rows matching the ids
3. A string representing a table name - The function returns an array of rows from the table specified (using DB::table('name'))

The $key parameter can be used to change the primary key column name (defaults to 'id')

### License

[](#license)

This software licensed under the [MIT license](http://opensource.org/licenses/MIT)

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 57.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~405 days

Total

2

Last Release

4138d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ec7d6db9c6a284d9ca1fb934910bb11c1eb3aaab1d9fc9daa5e92f98a43a37d6?d=identicon)[mnshankar](/maintainers/mnshankar)

---

Top Contributors

[![mnshankar](https://avatars.githubusercontent.com/u/2873887?v=4)](https://github.com/mnshankar "mnshankar (4 commits)")[![mfjordvald](https://avatars.githubusercontent.com/u/1022517?v=4)](https://github.com/mfjordvald "mfjordvald (2 commits)")[![iloverink](https://avatars.githubusercontent.com/u/47754304?v=4)](https://github.com/iloverink "iloverink (1 commits)")

---

Tags

searchlaravellaravel4sphinxqlSphinx RT

### Embed Badge

![Health badge](/badges/mnshankar-sphinxql/health.svg)

```
[![Health](https://phpackages.com/badges/mnshankar-sphinxql/health.svg)](https://phpackages.com/packages/mnshankar-sphinxql)
```

###  Alternatives

[mailerlite/laravel-elasticsearch

An easy way to use the official PHP ElasticSearch client in your Laravel applications.

934529.3k2](/packages/mailerlite-laravel-elasticsearch)[jeroen-g/explorer

Next-gen Elasticsearch driver for Laravel Scout.

397612.3k](/packages/jeroen-g-explorer)[remoblaser/search

A simple to implement Search for your Application

101.5k](/packages/remoblaser-search)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
