1、安装依赖包
yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel expat-devel
2、下载Sphinx
wget http://sphinxsearch.com/files/sphinx-2.2.11-release.tar.gz
或者直接去Sphinx官网去下载最新版本
3、安装Sphinx
tar zxvf sphinx-2.2.11-release.tar.gz
cd sphinx-2.2.11-release
./configure –prefix=/usr/local/sphinx –-with-mysql
make && make install
4、下载coreseek
wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz
##mmseg3是一个中文分词插件
tar xzvf coreseek-3.2.14.tar.gz
cd coreseek-3.2.14
cd mmseg-3.2.14/
./bootstrap
./configure –prefix=/usr/local/mmseg3
make
make install
cd ../csft-3.2.14/
sh buildconf.sh
./configure –prefix=/usr/local/coreseek –without-python –without-unixodbc –with-mmseg –with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ –with-mmseg-libs=/usr/local/mmseg3/lib/ –with-mysql –host=arm
vi src/sphinxexpr.cpp
然后将所有的T val = ExprEval ( this->m_pArg, tMatch )…..修改为
T val = this->ExprEval ( this->m_pArg, tMatch )
make
make install
cd /usr/local/coreseek/etc
进入配置目录通过命令ls可以看到3个文件
example.sql sphinx.conf.dist sphinx-min.conf.dist
其中example.sql是示例sql脚本我们将其导入到数据库中的test数据库中作为测试数据(会创建两张表 documents和tags)
cd /usr/local/sphinx/etc
cp sphinx.conf.dist sphinx.conf
vi sphinx.conf
mysql -u test < /usr/local/sphinx/etc/example.sql
=============================================================================
vi sphinx.conf
输入以下内容
source src1
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = 12345678
sql_db = test
sql_port = 3306 # optional, default is 3306
sql_sock = /tmp/mysql.sock
sql_query_pre = SET NAMES utf8
sql_query = \
SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
FROM documents
sql_attr_uint = group_id
sql_attr_timestamp = date_added
sql_query_info = SELECT * FROM documents WHERE id=$id
}
index test1
{
source = src1
path = /usr/local/coreseek/var/data/test1
docinfo = extern
charset_type = zh_cn.utf-8
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
charset_dictpath = /usr/local/mmseg3/etc/
ngram_len = 0
}
indexer
{
mem_limit = 32M
}
searchd
{
port = 9312
log = /usr/local/coreseek/var/log/searchd.log
query_log = /usr/local/coreseek/var/log/query.log
read_timeout = 5
max_children = 30
pid_file = /usr/local/coreseek/var/log/searchd.pid
max_matches = 1000
seamless_rotate = 1
preopen_indexes = 0
unlink_old = 1
}
说明:
代码段source src1{***} 代表数据源里面主要包含了数据库的配置信息,src1表示数据源名字,可以随便写。
代码段index test1{***} 代表为哪个数据源创建索引,与source *** 是成对出现的,其中的source参数的值必须是某一个数据源的名字。
其他参数可以查看手册,这里不再赘述。
生成索引
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/sphinx.conf –all
启动命令
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/sphinx.conf
其中参数–all表示生成所有索引
当然也可以是索引的名字例如:。
执行后可以在/usr/local/coreseek/var/data目录中看到多出一些文件,是以索引名为文件名的不同的扩展名的文件
在不启动sphinx的情况下即可测试命令:
/usr/local/coreseek/bin/search -c /usr/local/coreseek/etc/sphinx.conf 广东
上面的配置默认监听了,9312和9306端口,9312是给php程序链接的,9306是本地数据库调试端口,如果想要在服务器做测试,可以试试链接
mysql -h127.0.0.1 -P9306
SELECT * FROM test1 WHERE MATCH(‘my document’);
INSERT INTO rt VALUES (1, ‘this is’, ‘a sample text’, 11);
INSERT INTO rt VALUES (2, ‘some more’, ‘text here’, 22);
SELECT gid/11 FROM rt WHERE MATCH(‘text’) GROUP BY gid;
SELECT * FROM rt ORDER BY gid DESC;
SHOW TABLES;
SELECT *, WEIGHT() FROM test1 WHERE MATCH(‘”document one”/1’);SHOW META;
SET profiling=1;SELECT * FROM test1 WHERE id IN (1,2,4);SHOW PROFILE;
SELECT id, id%3 idd FROM test1 WHERE MATCH(‘this is | nothing’) GROUP BY idd;SHOW PROFILE;
SELECT id FROM test1 WHERE MATCH(‘is this a good plan?’);SHOW PLAN;
SELECT COUNT(*) c, id%3 idd FROM test1 GROUP BY idd HAVING COUNT(*)>1;
SELECT COUNT(*) FROM test1;
CALL KEYWORDS (‘one two three’, ‘test1’);
CALL KEYWORDS (‘one two three’, ‘test1’, 1);
windows下创建sphinx
1、进入coreseek文件夹下
2、创建索引 .\bin\indexer.exe -c .\etc\配置文件名字 –all
3、开启索引 .\bin\searchd.exe -c .\etc\配置文件
4、如果用docs操作进行搜索 : .\bin\search.exe -c .\etc\配置文件名字 搜索关键值
5.安装PHP的Sphinx模块
首先安装
ibsphinxclient 安装(PHP模块需要)
cd api/libsphinxclient
./configure –prefix=/usr/local/sphinx
make && make install
下载地址:http://pecl.php.net/package/sphinx
wget http://pecl.php.net/get/sphinx-1.3.0.tgz
tar zxf sphinx-1.3.3.tgz
cd sphinx-1.3.3
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config –with-sphinx=/usr/local/sphinx/
make && make install
添加php扩展库
编辑配置
vi /usr/local/php/etc/php.ini
:$ 跳至文件尾部
extension_dir=”/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/”
[sphinx]
extension=sphinx.so
php -m 或者 phpinfo() 查看是否已经加载扩展