dev-master
9999999-devExtension for work with Sphinx Search.
by Alex Sharov
Wallogit.com
2017 © Pedro Peláez
Extension for work with Sphinx Search.
Simple and powerful component for work with Sphinx search engine., (*1)
This is a beta version, please help with testing and bug reports. You can find old stable version at "releases" page., (*2)
"repositories": [
{
"type": "vcs",
"url": "https://github.com/sergebezborodov/sphinx-yii"
}
],
"require": {
"sergebezborodov/sphinx-yii": "dev-master"
}
in config (assumes you have 'vendor' alias to composer directory):, (*3)
'components' => array(
'sphinx' => array(
'class' => 'vendor.sergebezborodov.sphinx-yii.ESphinxApiConnection', // sphinx api mode
//'class' => 'vendor.sergebezborodov.sphinx-yii.ESphinxMysqlConnection', for sphinx ql mode
'server' => array('localhost', 3386),
'connectionTimeout' => 3, // optional, default 0 - no limit
'queryTimeout' => 5, // optional, default 0 - no limit
),
),
Download and extract source in protected/extensions folder. In config add:, (*4)
'import' => array(
// i hope remove this in new versions
'ext.sphinx.*',
'ext.sphinx.ql.*',
'ext.sphinx.enums.*',
),
'components' => array(
'sphinx' => array(
'class' => 'ext.sphinx.ESphinxApiConnection', // sphinx api mode
//'class' => 'ext.sphinx.ESphinxMysqlConnection', for sphinx ql mode
'server' => array('localhost', 3386),
'connectionTimeout' => 3, // optional, default 0 - no limit
'queryTimeout' => 5, // optional, default 0 - no limit
),
),
All component classes names begins with ESphinx. Main object we used for querying is ESphinxQuery., (*5)
Query in index:, (*6)
Yii::app()->sphinx->executeQuery(new ESphinxQuery('Hello world!'), 'blog');
Often we need search in index with some parametrs and options. For this task component has class ESphinxSearchCriteria. It's very similar to CDbCriteria and has the same idea., (*7)
Search in article index with some parametrs:, (*8)
$criteria = new ESphinxSearchCriteria(array(
'sortMode' => ESphinxSort::EXTENDED,
'orders' => array(
'date_created' => 'DESC',
'date_updated' => 'ASC',
),
'mathMode' => ESphinxMatch::EXTENDED,
));
$query = new ESphinxQuery('@(title,body) hello world', 'articles', $criteria);
Criteria can changing at work., (*9)
$criteria = new ESphinxSearchCriteria(array('mathMode' => ESphinxMatch::EXTENDED));
$criteria->addFilter('user_id', 1000); // add filter by user, we can use integer or integer array
$criteria->addFilter('site_id', 123, false, 'site'); // add filter by site_id field with key value (will used later)
// querying
$result = Yii::app()->sphinx->executeQuery(new ESphinxQuery('', 'products', $criteria));
// search same query by another site
$criteria->addFilter('site_id', 321, false, 'site'); // change site_id param value
// querying
$result = Yii::app()->sphinx->executeQuery(new ESphinxQuery('', 'products', $criteria));
// search same query but without site_id param
$criteria->deleteFilter('site'); // delete filter on site_id field
// querying....
One of the powerfull sphinx features is multi queries (packet queries). When you send two or more queries sphinx does internal optimisation for faster work., (*10)
$query1 = new ESphinxQuery('', 'products', array('filters' => array(array('site_id', 123))));
$query2 = new ESphinxQuery('', 'products', array('filters' => array(array('site_id', 321))));
$results = Yii::app()->sphinx->executeQueries(array($query1, $query2));
Another way to add queries:, (*11)
$query = new ESphinxQuery('', 'products', array('filters' => array(array('site_id', 123, 'key' => 'site_id')))));
Yii::app()->sphinx->addQuery($query);
// change previous site_id filter value
$query->criteria->addFilter('site_id', 321, false, 'site_id');
$results = Yii::app()->sphinx->runQueries();
ESphinxSearchCriteria includes all possible options of last sphinx beta. Be sure you are using right functions for your version., (*12)
For SPH_SORT_EXTENDED (ESphinxSort::EXTENDED) you should use setOrders() or addOrder() method. For others sort modes use setSortBy() for one field., (*13)
Extension for work with Sphinx Search.