sphinx-yii

Yii component for work with Sphinx Search

  • 所有者: sergebezborodov/sphinx-yii
  • 平台:
  • 許可證:
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

Yii Sphinx Component

Simple and powerful component for work with Sphinx search engine.

This is a beta version, please help with testing and bug reports.
You can find old stable version at "releases" page.

Features

  • Simple query methods
  • Extended ESphinxSearchCriteria for complex queries
  • Support connection by Sphinx API and Sphinx QL
  • Support packeted queries for both connections
  • Unit tests coverage

How to install

###1. via composer

"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):

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

###2. old school way

Download and extract source in protected/extensions folder. In config add:

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

How to use

All component classes names begins with ESphinx.
Main object we used for querying is ESphinxQuery.

Query in index:

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

Extended queries

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.

Search in article index with some parametrs:

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

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

Multi queries

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.

$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:

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

Options

####Versions
ESphinxSearchCriteria includes all possible options of last sphinx beta.
Be sure you are using right functions for your version.

####Sort Methods
For SPH_SORT_EXTENDED (ESphinxSort::EXTENDED) you should use setOrders() or addOrder() method.
For others sort modes use setSortBy() for one field.

主要指標

概覽
名稱與所有者sergebezborodov/sphinx-yii
主編程語言PHP
編程語言PHP (語言數: 2)
平台
許可證
所有者活动
創建於2013-02-13 11:53:15
推送於2013-10-24 10:08:09
最后一次提交2013-10-24 13:08:00
發布數1
最新版本名稱0.1 (發布於 )
第一版名稱0.1 (發布於 )
用户参与
星數18
關注者數7
派生數7
提交數75
已啟用問題?
問題數4
打開的問題數1
拉請求數1
打開的拉請求數0
關閉的拉請求數1
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?