/**
@author:luowen
@time:2013-08-01
[sphinx]
tar -zxvf sphinx-2.1.1-beta
cd sphinx-2.1.1-beta
./configure --prefix=/usr/local/sphinx/ --with-mysql
make && make install
编辑配置文件
四个块
1.主数据源(就是一段时间从数据库拿到的数据)
source main
设置好 数据库名称 密码 端口
初始执行语句 sql_query_pre
主的sql语句 sql_query
2.增量数据源(一段时间重新添加的数据,新增到增量数据源中)
source delta : main 继承主数据源
设置好初始语句 sql_query_pre
3.主索引
名称要想吻合
4.增量索引
继承主索引
5.索引器
设置好内存就可以了
5.检索器
基本不要设置
给PHP添加模块
先到sphinx-2.1.1-bate下的api包下面的api中sphinxClient目录中
./configure
make && make install
下载 sphinx-1.0.3.tgz
tar -zxvf sphinx-1.0.3.tgz
/usr/local/php/bin/phpnize //生成./configure 文件
./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx
make && make install
PHP测试文件
/*
<?php
//构造一个sphinx对象
$sp = new SphinxClient();
//链接到sphinx服务
$sp -> setServer('localhost',9312);
//获取关键字
$keyword = $_GET['keyword'];
//查询关键词
$res = $sp -> query($keyword,'*'); //*表示数据源,可以填写在配置文件中的main或者delta
//拿到匹配好的文档id
$ids = join(',',$res['matches']);
//链接到数据库,然后查询数据
$conn = mysql_connect('localhost','root','password');
mysql_query('set names utf8');
mysql_query('use database');
$sql = "select * from tableName where id in ($ids)";
$rst = mysql_query($sql);
while($row = mysql_fetch_assoc($rst)){
print_r($row);
}
/**
即时添加增量数据
在mysql中创建一个计数表 sphinx_cnt
create table sphinx_cnt ( id int unsigned primary key auto_increment,max_id int unsigned not null default 0);
在主数据源中的配置文件中添加一段
sql_query_pre replace into sphinx_cnt select 1,max(id) from tableName;
sql_query select * from tableName where id <= (select max_id from sphinx_cnt where id = 1)
在增量数据源中添加
sql_query select * from tableName where id > (select max_id from sphinx_cnt where id=1)
在linux的计划任务中添加
crontab -e
*/5 * * * * /usr/local/sphinx/bin/indexer delta --rotate
00 03 * * * /usr/local/sphinx/bin/indexer main --rotate
注:linux 每隔五分钟执行增量数据源,每天的凌晨三点执行主数据源的索引
中文索引的sphinx为 coreseek 安装如下:
