yii+sphinx 配置、管理
1.sphinx安装
http://sphinxsearch.com/docs/latest/installing-debian.html
There are two ways of getting Sphinx for Ubuntu: regular deb packages and the Launchpad PPA repository.
Deb packages:
-
Sphinx requires a few libraries to be installed on Debian/Ubuntu. Use apt-get to download and install these dependencies:
$ sudo apt-get install mysql-client unixodbc libpq5
-
Now you can install Sphinx:
$ sudo dpkg -i sphinxsearch_2.2.11-dev-0ubuntu12~trusty_amd64.deb
PPA repository (Ubuntu only).
Installing Sphinx is much easier from Sphinxsearch PPA repository, because you will get all dependencies and can also update Sphinx to the latest version with the same command.
-
First, add Sphinxsearch repository and update the list of packages:
$ sudo add-apt-repository ppa:builds/sphinxsearch-rel22
$ sudo apt-get update
-
Install/update sphinxsearch package:
$ sudo apt-get install sphinxsearch
Sphinx searchd
daemon can be started/stopped using service command:
$ sudo service sphinxsearch start
2.sphinx配置
https://yq.aliyun.com/articles/66520?spm=5176.100240.searchblog.88.ovXgTm
一、sphinx的配置
1. sphinx配置文件结构介绍
Sphinx的配置文件结构如下:
Source 源名称1{ #添加数据源,这里会设置一些连接数据库的参数比如数据库的IP、用户名、密码等 #设置sql_query、设置sql_query_pre、设置sql_query_range等后面会结合例子做详细介绍 …… } Index 索引名称1{ Source=源名称1 #设置全文索引 …… } Indexer{ #设置Indexer程序配置选项,如内存限制等 …… } Searchd{ #设置Searchd守护进程本身的一些参数 …… }
Source和Index都可以配置多个。
2. spinx配置案例详细解释
接下来就来针对一个配置案例来做详细的配置介绍:
#定义一个数据源 source search_main { #定义数据库类型 type = mysql #定义数据库的IP或者计算机名 sql_host = localhost #定义连接数据库的帐号 sql_user = root #定义链接数据库的密码 sql_pass = test123 #定义数据库名称 sql_db = test #定义连接数据库后取数据之前执行的SQL语句 sql_query_pre = SET NAMES utf8 sql_query_pre = SET SESSION query_cache_type=OFF #创建一个sph_counter用于增量索引 sql_query_pre = CREATE TABLE IF NOT EXISTS sph_counter \ ( counter_id INTEGER PRIMARY KEY NOT NULL,max_doc_id INTEGER NOT NULL) #取数据之前将表的最大id记录到sph_counter表中 sql_query_pre = REPLACE INTO sph_counter SELECT 1, MAX(searchid) FROM v9_search #定义取数据的SQL,第一列ID列必须为唯一的正整数值 sql_query = SELECT searchid,typeid,id,adddate,data FROM v9_search where \ searchid<( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 ) \ and searchid>=$start AND searchid<=$end # sql_attr_uint和sql_attr_timestamp用于定义用于api过滤或者排序,写多行制定多列 sql_attr_uint = typeid sql_attr_uint = id sql_attr_timestamp = adddate #分区查询设置 sql_query_range = SELECT MIN(searchid),MAX(searchid) FROM v9_search #分区查询的步长 sql_range_step = 1000 #设置分区查询的时间间隔 sql_ranged_throttle = 0 #用于CLI的调试 sql_query_info = SELECT * FROM v9_search WHERE searchid=$id } #定义一个增量的源 source search_main_delta : search_main { sql_query_pre = set names utf8 #增量源只查询上次主索引生成后新增加的数据 #如果新增加的searchid比主索引建立时的searchid还小那么会漏掉 sql_query = SELECT searchid,typeid,id,adddate,data FROM v9_search where \ searchid>( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 ) \ and searchid>=$start AND searchid<=$end sql_query_range = SELECT MIN(searchid),MAX(searchid) FROM v9_search where \ searchid>( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 ) } #定义一个index_search_main索引 index index_search_main { #设置索引的源 source = search_main #设置生成的索引存放路径 path = /usr/local/coreseek/var/data/index_search_main #定义文档信息的存储模式,extern表示文档信息和文档id分开存储 docinfo = extern #设置已缓存数据的内存锁定,为0表示不锁定 mlock = 0 #设置词形处理器列表,设置为none表示不使用任何词形处理器 morphology = none #定义最小索引词的长度 min_word_len = 1 #设置字符集编码类型,我这里采用的utf8编码和数据库的一致 charset_type = zh_cn.utf-8 #指定分词读取词典文件的位置 charset_dictpath = /usr/local/mmseg3/etc #不被搜索的词文件里表。 stopwords = /usr/local/coreseek/var/data/stopwords.txt #定义是否从输入全文数据中取出HTML标记 html_strip = 0 } #定义增量索引 index index_search_main_delta : index_search_main { source = search_main_delta path = /usr/local/coreseek/var/data/index_search_main_delta } #定义indexer配置选项 indexer { #定义生成索引过程使用索引的限制 mem_limit = 512M } #定义searchd守护进程的相关选项 searchd { #定义监听的IP和端口 #listen = 127.0.0.1 #listen = 172.16.88.100:3312 listen = 3312 listen = /var/run/searchd.sock #定义log的位置 log = /usr/local/coreseek/var/log/searchd.log #定义查询log的位置 query_log = /usr/local/coreseek/var/log/query.log #定义网络客户端请求的读超时时间 read_timeout = 5 #定义子进程的最大数量 max_children = 300 #设置searchd进程pid文件名 pid_file = /usr/local/coreseek/var/log/searchd.pid #定义守护进程在内存中为每个索引所保持并返回给客户端的匹配数目的最大值 max_matches = 100000 #启用无缝seamless轮转,防止searchd轮转在需要预取大量数据的索引时停止响应 #也就是说在任何时刻查询都可用,或者使用旧索引,或者使用新索引 seamless_rotate = 1 #配置在启动时强制重新打开所有索引文件 preopen_indexes = 1 #设置索引轮转成功以后删除以.old为扩展名的索引拷贝 unlink_old = 1 # MVA更新池大小,这个参数不太明白 mva_updates_pool = 1M #最大允许的包大小 max_packet_size = 32M #最大允许的过滤器数 max_filters = 256 #每个过滤器最大允许的值的个数 max_filter_values = 4096 }
二、sphinx的管理
1. 生成Sphinx中文分词词库(新版本的中文分词库已经生成在了/usr/local/mmseg3/etc目录下)
cd /usr/local/mmseg3/etc /usr/local/mmseg3/bin/mmseg -u thesaurus.txt mv thesaurus.txt.uni uni.lib
2. 生成Sphinx中文同义词库
#同义词库是说比如你搜索深圳的时候,含有深圳湾等字的也会被搜索出来
/data/software/sphinx/coreseek-3.2.14/mmseg-3.2.14/script/build_thesaurus.py unigram.txt > thesaurus.txt /usr/local/mmseg3/bin/mmseg -t thesaurus.txt 将thesaurus.lib放到uni.lib同一目录
3. 生成全部索引
/usr/local/coreseek/bin/indexer --config /usr/local/coreseek/etc/sphinx.conf –all
若此时searchd守护进程已经启动,那么需要加上—rotate参数:
/usr/local/coreseek/bin/indexer --config /usr/local/coreseek/etc/sphinx.conf --all --rotate
4. 启动searchd守护进程
/usr/local/coreseek/bin/searchd --config /usr/local/coreseek/etc/sphinx.conf
5. 生成主索引
写成shell脚本,添加到crontab任务,设置成每天凌晨1点的时候重建主索引
/usr/local/coreseek/bin/indexer --config /usr/local/coreseek/etc/sphinx.conf --rotate index_search_main
6. 生成增量索引
写成shell脚本,添加到crontab任务,设置成每10分钟运行一次
/usr/local/coreseek/bin/indexer --config /usr/local/coreseek/etc/sphinx.conf --rotate index_search_main_delta
7. 增量索引和主索引的合并
写成shell脚本,添加到计划任务,每15分钟跑一次
/usr/local/coreseek/bin/indexer --config /usr/local/coreseek/etc/sphinx.conf --merge index_search_main index_search_main_delta --rotate
8. 使用search命令在命令行对索引进行检索
/usr/local/coreseek/bin/search --config /usr/local/coreseek/etc/sphinx.conf 游戏
3.yii
http://www.yiiframework.com/doc-2.0/ext-sphinx-index.html
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yiisoft/yii2-sphinx
or add
"yiisoft/yii2-sphinx": "~2.0.0"
to the require section of your composer.json.
Configuration
This extension interacts with Sphinx search daemon using MySQL protocol and SphinxQL query language. In order to setup Sphinx "searchd" to support MySQL protocol following configuration should be added:
searchd
{
listen = localhost:9306:mysql41
...
}
To use this extension, simply add the following code in your application configuration:
return [
//....
'components' => [
'sphinx' => [
'class' => 'yii\sphinx\Connection',
'dsn' => 'mysql:host=127.0.0.1;port=9306;dbname=xxx',【这里127.0.0.1换成localhost】
'username' => 'root',
'password' => 'root',
],
],
];
$sql = 'SELECT * FROM documents WHERE content like :content';
$params = [
':content' => '%another%'
];
$rows = Yii::$app->sphinx->createCommand($sql, $params)->queryAll();
var_dump($rows);
另:http://www.xunsearch.com/ 讯搜