PHP code example of sergebezborodov / sphinx-yii

1. Go to this page and download the library: Download sergebezborodov/sphinx-yii library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sergebezborodov / sphinx-yii example snippets


'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
    ),
),

'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
    ),
),

Yii::app()->sphinx->executeQuery(new ESphinxQuery('Hello world!'), 'blog');

$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 = 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....

$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));

$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();