dezinger/yii2-rtsphinxbehavior

用于与 Sphinx 实时索引工作的 Yii2 行为

安装: 83

依赖关系: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 2

类型:yii2-extension

dev-master / 1.0.x-dev 2018-03-19 14:36 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:21:07 UTC


README

安装

安装此扩展的首选方法是通过 Composer

运行以下命令:

php composer.phar require dezinger/yii2-rtsphinxbehavior

或者在您的 composer.json 文件的 require 部分添加以下内容:

"dezinger/yii2-rtsphinxbehavior": "dev-master"

to the require section of your composer.json file.

如何附加

namespace modules\blog\models;
use modules\blog\Module;
use modules\blog\components\RtSphinxBehavior;

class Post extends \yii\db\ActiveRecord {

    public function behaviors() {
        return [
                    'rtSphinxBehavior' => [
                        'class' => RtSphinxBehavior::className(),
                        'rtIndex' => Yii::$app->getModule('blog')->getParam('sphinxRtIndex'),
                        'idAttributeName' => 'id',
                        'rtFieldNames' => [
                            'name', 
                            'title', 
                            'description', 
                            'text'
                        ],
                        'rtAttributeNames' => [
                            'category_id', //RT attr and Post same name
                            'post_id' => 'id', //Attr mapping examle: rt.post_id = post.id
                            'parent_category_id' => 'category.parent_id', //Attr shortcut syntax example
                            'rating' => functon($model){return floatval($model->rating);}, //Usage closure example
                        ],
                        'enabled' => Yii::$app->getModule('blog')->getParam('isSphinxEnabled'),
                    ],
                ];   
    }

在这个示例中,rtIndex 参数的值来自 blog-module 的 sphinxRtIndex 参数。然后提供来自我们主要文档检索查询的属性和字段名称,该查询在 sphinx.conf 的源代码块中描述。

如何为 Sphinx 配置实时索引

source is_src
{
	type			= mysql

	sql_host		= localhost
	sql_user		= root
	sql_pass		=
	sql_db			= cms_db
	sql_port		= 3306	# optional, default is 3306

	sql_query_pre = SET NAMES utf8
    sql_query_pre = SET CHARACTER SET utf8
	
	sql_query = \
		SELECT id, category_id, UNIX_TIMESTAMP(date) AS date, name, title, description, text \
		FROM post

	sql_attr_uint		= category_id
	
	sql_field_string    = name
	sql_field_string    = title
	sql_field_string    = description	
	sql_field_string    = text	
}

RT 索引的配置块如下所示

index is_rt
{
	type			= rt
	docinfo			= extern
	
	rt_mem_limit	= 512M

	path			= /sphinx/data/is_rt
	stopwords		= /sphinx/stop/words.txt
	dict			= keywords
	morphology		= stem_ru, stem_en, soundex
	min_word_len	= 3
	min_prefix_len 	= 3
	expand_keywords	= 1
	index_exact_words = 1
	html_strip 		= 1
	
	rt_field = name 	
	rt_field = title
	rt_field = description
	rt_field = text
	
	rt_attr_uint = category_id
}

工作原理

RtSphinxBehaviorActiveRecord 类处理的插入、更新、删除事件上触发。