Sphinx plugin for CakePHP
, (*1)
For earlier versions of CakePHP (2.x)
Please use the 2.x branch and follow the readme instructions under that for usage instructions, (*2)
Installation
You can install this plugin into your CakePHP application using composer., (*3)
The recommended way to install composer packages is:, (*4)
composer require voycey/cakephp-sphinxsearch
Basic Documentation
I designed this as a replacement for the binary API access for Sphinxsearch that I was using on 2.x (https://github.com/voycey/sphinxsearch-cakephp2), (*5)
It currently has one function and that is to query the provided index and return the matching records in a CakePHP friendly format (In this case as Query objects and Entities)., (*6)
How to use
- Install the package with composer as above
- Add
Plugin::load('Sphinx'); to your bootstrap.php
- Attach the behaviour to a table you wish to search on
(There must be an index that is generated from this model - the behaviour works by pulling the ID's from Sphinx and then fetching them from the DB (See TODO's for improving this)
- Behavior config options:
-
'connection' => ['host' => 'hostname', 'port' => 'port'] (default hostname is 'localhost' and default port is 9306)
'defaultIndex' => 'index_name'
<?php
class PostsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('posts');
$this->displayField('title');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->addBehavior('Sphinx.Sphinx');
}
}
Here is an example unit test that works for me., (*8)
public function testBehaviour()
{
$paginate = [
'order' => [
'Posts.id asc'
],
'fields' => [
'id', 'title', 'user_id'
],
'contain' => [
'Comments' => [
'fields' => ['id', 'post_id']
],
'Categories' => [
'fields' => ['id', 'CategoriesPosts.post_id']
],
'Types' => [
'fields' => ['id', 'name']
]
]
];
$query = $this->Posts->search([
'index' => 'idx_toolkit',
'term' => 'Ten',
'match_fields' => 'title',
'paginate' => $paginate,
'limit' => 50
]);
$row = $query->first();
$this->assertInstanceOf('Cake\ORM\Query', $query);
$this->assertInstanceOf('Cake\ORM\Entity', $row);
}
TODO
- Give option for all data to be pulled from Sphinxsearch directly rather than then querying DB
- Hook into afterSave and have the Sphinx index updated (this isn't a priority for me as my indexes don't need to be live but please submit a pull request if you want to add this)
- Work out how to test this easily on Travis (again - help appreciated)