#Sphinx是一个全文检索引擎。
索引和性能优异
- 易于集成SQL和XML数据源,并可使用SphinxAPI、SphinxQL或者SphinxSE搜索接口
- 易于通过分布式搜索进行扩展
- 高速的索引建立(在当代CPU上,峰值性能可达到10 ~ 15MB/秒)
- 高性能的搜索 (在1.2G文本,100万条文档上进行搜索,支持高达每秒150~250次查询)
官网下载地址http://sphinxsearch.com/downloads/current/
中文文档
下载完成后如下图
ps : 然后我们在新建一个data用来存放索引目录 log用来存放查询日志
1.新建sphinx.conf配置文件 放在bin目录 下
(配置项的信息可以参考etc/sphinx.conf.dist 已经写的非常全面了)
#
# Minimal Sphinx configuration sample (clean, simple, functional)
#
source src1
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = root
sql_db = test
sql_port = 3306 # optional, default is 3306
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
}
index test1
{
source = src1
path = H:/soft/sphinx/data/test1
}
index testrt
{
type = rt
rt_mem_limit = 128M
path = H:/soft/sphinx/data/testrt
rt_field = title
rt_field = content
rt_attr_uint = gid
}
indexer
{
mem_limit = 128M
}
searchd
{
listen = 9312
listen = 9306:mysql41
log = H:/soft/sphinx/log/searchd.log
query_log = H:/soft/sphinx/log/query.log
read_timeout = 5
max_children = 30
pid_file = H:/soft/sphinx/log/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
workers = threads # for RT to work
binlog_path = H:/soft/sphinx/data
}
2.然后用cmd命令进入H:\soft\sphinx\bin indexer test1生成索引
成功后会在data目录下生成文件
#3.启动searchd服务。
4.建立一个php文件
<?php
require ( "sphinxapi.php" );
$cl = new SphinxClient ();
$q = "test"; //模拟关键字
$sql = "";
$mode = SPH_MATCH_ALL;
$host = "127.0.0.1";
$port = 9312;
$index = "*";
$cl->SetServer ( $host, $port );
$cl->SetConnectTimeout(10);
$cl->SetArrayResult(true);
$res = $cl->Query ( $q, $index );
var_dump($res['matches']);
sphinx搜索只是查询sphinx索引,并不查询mysql,从sphinx服务中查询出的id就是mysql 数据库中的主键。根据主键查询mysql。当数据库中的内容发生改变后,要重新生成索引,或者在数据表中添加一个字段用来标记该条数据是否生成了索引,重建索引时,只生成没有生成索引的记录,然后合并到索引中去即可.
5. 下面介绍一些常用的语句
indexer.exe test1 <后面是索引名称> #建立索引
indexer.exe test1 --rotate #重建索引
searchd #启动服务
searchd --stop 停止服务
一个案例sphinx_runtime.conf
source common_src
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = root
sql_db = qiwangguanjia
sql_port = 3306
sql_query_pre = SET CHARACTER SET 'utf8'
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
}
index common_idx
{
charset_table = U+FF10..U+FF19->0..9, 0..9, U+FF41..U+FF5A->a..z, U+FF21..U+FF3A->a..z,\
A..Z->a..z, a..z, U+0149, U+017F, U+0138, U+00DF, U+00FF, U+00C0..U+00D6->U+00E0..U+00F6,\
U+00E0..U+00F6, U+00D8..U+00DE->U+00F8..U+00FE, U+00F8..U+00FE, U+0100->U+0101, U+0101,\
U+0102->U+0103, U+0103, U+0104->U+0105, U+0105, U+0106->U+0107, U+0107, U+0108->U+0109,\
U+0109, U+010A->U+010B, U+010B, U+010C->U+010D, U+010D, U+010E->U+010F, U+010F,\
U+0110->U+0111, U+0111, U+0112->U+0113, U+0113, U+0114->U+0115, U+0115, \
U+0116->U+0117,U+0117, U+0118->U+0119, U+0119, U+011A->U+011B, U+011B, U+011C->U+011D,\
U+011D,U+011E->U+011F, U+011F, U+0130->U+0131, U+0131, U+0132->U+0133, U+0133, \
U+0134->U+0135,U+0135, U+0136->U+0137, U+0137, U+0139->U+013A, U+013A, U+013B->U+013C, \
U+013C,U+013D->U+013E, U+013E, U+013F->U+0140, U+0140, U+0141->U+0142, U+0142, \
U+0143->U+0144,U+0144, U+0145->U+0146, U+0146, U+0147->U+0148, U+0148, U+014A->U+014B, \
U+014B,U+014C->U+014D, U+014D, U+014E->U+014F, U+014F, U+0150->U+0151, U+0151, \
U+0152->U+0153,U+0153, U+0154->U+0155, U+0155, U+0156->U+0157, U+0157, U+0158->U+0159,\
U+0159,U+015A->U+015B, U+015B, U+015C->U+015D, U+015D, U+015E->U+015F, U+015F, \
U+0160->U+0161,U+0161, U+0162->U+0163, U+0163, U+0164->U+0165, U+0165, U+0166->U+0167, \
U+0167,U+0168->U+0169, U+0169, U+016A->U+016B, U+016B, U+016C->U+016D, U+016D, \
U+016E->U+016F,U+016F, U+0170->U+0171, U+0171, U+0172->U+0173, U+0173, U+0174->U+0175,\
U+0175,U+0176->U+0177, U+0177, U+0178->U+00FF, U+00FF, U+0179->U+017A, U+017A, \
U+017B->U+017C,U+017C, U+017D->U+017E, U+017E, U+0410..U+042F->U+0430..U+044F, \
U+0430..U+044F,U+05D0..U+05EA, U+0531..U+0556->U+0561..U+0586, U+0561..U+0587, \
U+0621..U+063A, U+01B9,U+01BF, U+0640..U+064A, U+0660..U+0669, U+066E, U+066F, \
U+0671..U+06D3, U+06F0..U+06FF,U+0904..U+0939, U+0958..U+095F, U+0960..U+0963, \
U+0966..U+096F, U+097B..U+097F,U+0985..U+09B9, U+09CE, U+09DC..U+09E3, U+09E6..U+09EF, \
U+0A05..U+0A39, U+0A59..U+0A5E,U+0A66..U+0A6F, U+0A85..U+0AB9, U+0AE0..U+0AE3, \
U+0AE6..U+0AEF, U+0B05..U+0B39,U+0B5C..U+0B61, U+0B66..U+0B6F, U+0B71, U+0B85..U+0BB9, \
U+0BE6..U+0BF2, U+0C05..U+0C39,U+0C66..U+0C6F, U+0C85..U+0CB9, U+0CDE..U+0CE3, \
U+0CE6..U+0CEF, U+0D05..U+0D39, U+0D60,U+0D61, U+0D66..U+0D6F, U+0D85..U+0DC6, \
U+1900..U+1938, U+1946..U+194F, U+A800..U+A805,U+A807..U+A822, U+0386->U+03B1, \
U+03AC->U+03B1, U+0388->U+03B5, U+03AD->U+03B5,U+0389->U+03B7, U+03AE->U+03B7, \
U+038A->U+03B9, U+0390->U+03B9, U+03AA->U+03B9,U+03AF->U+03B9, U+03CA->U+03B9, \
U+038C->U+03BF, U+03CC->U+03BF, U+038E->U+03C5,U+03AB->U+03C5, U+03B0->U+03C5, \
U+03CB->U+03C5, U+03CD->U+03C5, U+038F->U+03C9,U+03CE->U+03C9, U+03C2->U+03C3, \
U+0391..U+03A1->U+03B1..U+03C1,U+03A3..U+03A9->U+03C3..U+03C9, U+03B1..U+03C1, \
U+03C3..U+03C9, U+0E01..U+0E2E,U+0E30..U+0E3A, U+0E40..U+0E45, U+0E47, U+0E50..U+0E59, \
U+A000..U+A48F, U+4E00..U+9FBF,U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF, \
U+2F800..U+2FA1F, U+2E80..U+2EFF,U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF, \
U+3040..U+309F, U+30A0..U+30FF,U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF, \
U+3130..U+318F, U+A000..U+A48F,U+A490..U+A4CF
}
indexer
{
mem_limit = 128M
}
searchd
{
listen = 9312
listen = 9306:mysql41
log = E:/soft/sphinx/sphinx-3.0.3/bin/sphinxdata/logs/searchd.log
query_log = E:/soft/sphinx/sphinx-3.0.3/bin/sphinxdata/logs/query.log
read_timeout = 5
client_timeout = 300
max_children = 30
pid_file = E:/soft/sphinx/sphinx-3.0.3/bin/sphinxdata/logs/searchd.pid
preopen_indexes = 1
unlink_old = 1
mva_updates_pool = 1M
max_packet_size = 8M
max_filters = 256
max_filter_values = 4096
workers = threads
}
source user_reginfo_src:common_src
{
sql_query_pre = SET NAMES utf8
sql_query_post = update qw_sphinx set counter = (select MAX(uid) from qw_user_reginfo) where from_who='user_reginfo'
sql_query = select uid,tid,mobile from qw_user_reginfo
sql_attr_uint = tid
}
index user_reginfo_idx:common_idx
{
source = user_reginfo_src
path =E:/soft/sphinx/sphinx-3.0.3/bin/sphinxdata/user_reginfo
}
index user_reginfo_rtx
{
# 实时索引类型
type = rt
# 默认UTF8
charset_table = 0..9, A..Z->a..z, _, a..z,U+410..U+42F->U+430..U+44F, U+430..U+44F
#对于非字母型数据的长度切割(默认已字符和数字切割,设置1为按没个字母切割)
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
# 索引保存路径,平时都是保存在内存内,数据量超过内存量的时候会保存在文件内,这里随便存了下没放到data目录下
path =E:/soft/sphinx/sphinx-3.0.3/bin/sphinxdata/user_reginfo_rtx
# 内存保存大小限制,超过这个就会保存到硬盘中
rt_mem_limit = 512M
# 全文检索字段声明,这里把实时索引的索引字段都声明出来
rt_field = mobile
#他属性字段,可以用来查询
rt_attr_uint = tid
}
index fans_mapping_rtx
{
# 实时索引类型
type = rt
# 默认UTF8
charset_table = 0..9, A..Z->a..z, _, a..z,U+410..U+42F->U+430..U+44F, U+430..U+44F
#对于非字母型数据的长度切割(默认已字符和数字切割,设置1为按没个字母切割)
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
# 索引保存路径,平时都是保存在内存内,数据量超过内存量的时候会保存在文件内,这里随便存了下没放到data目录下
path =E:/soft/sphinx/sphinx-3.0.3/bin/sphinxdata/fans_mapping_rtx
# 内存保存大小限制,超过这个就会保存到硬盘中
rt_mem_limit = 512M
# 全文检索字段声明,这里把实时索引的索引字段都声明出来
rt_field = openid
#他属性字段,可以用来查询
rt_attr_uint = tid
rt_attr_uint = uid
rt_attr_uint = regtid
}
index order_rtx
{
# 实时索引类型
type = rt
# 默认UTF8
charset_table = 0..9, A..Z->a..z, _, a..z,U+410..U+42F->U+430..U+44F, U+430..U+44F
#对于非字母型数据的长度切割(默认已字符和数字切割,设置1为按没个字母切割)
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
# 索引保存路径,平时都是保存在内存内,数据量超过内存量的时候会保存在文件内,这里随便存了下没放到data目录下
path =E:/soft/sphinx/sphinx-3.0.3/bin/sphinxdata/order_rtx
# 内存保存大小限制,超过这个就会保存到硬盘中
rt_mem_limit = 512M
# 全文检索字段声明,这里把实时索引的索引字段都声明出来
rt_field = title
rt_field = ordersn
rt_field = mobile
#他属性字段,可以用来查询
rt_attr_uint = utid
rt_attr_uint = atid
rt_attr_uint = aoid
rt_attr_uint = uid
rt_attr_uint = uoid
}
重建索引
#!/bin/bash
/usr/bin/indexer -c /etc/sphinx/sphinx.conf user_reginfo_idx --rotate >> /etc/sphinx/log/index_log.log
mysql -P 9306 -h 127.0.0.1 -e"TRUNCATE RTINDEX user_reginfo_rtx;ATTACH INDEX user_reginfo_idx TO RTINDEX user_reginfo_rtx;"
/usr/bin/indexer -c /etc/sphinx/sphinx.conf fans_mapping_idx --rotate >> /etc/sphinx/log/index_log.log
mysql -P 9306 -h 127.0.0.1 -e"TRUNCATE RTINDEX fans_mapping_rtx;ATTACH INDEX fans_mapping_idx TO RTINDEX fans_mapping_rtx;"
/usr/bin/indexer -c /etc/sphinx/sphinx.conf fans_unionids_idx --rotate >> /etc/sphinx/log/index_log.log
mysql -P 9306 -h 127.0.0.1 -e"TRUNCATE RTINDEX fans_unionids_rtx;ATTACH INDEX fans_unionids_idx TO RTINDEX fans_unionids_rtx;"
#!/bin/bash
/usr/bin/indexer -c /etc/sphinx/sphinx.conf user_reginfo_idx --rotate >> /etc/sphinx/log/index_log.log
mysql -P 9306 -h 127.0.0.1 -e"TRUNCATE RTINDEX user_reginfo_rtx;ATTACH INDEX user_reginfo_idx TO RTINDEX user_reginfo_rtx;"
/usr/bin/indexer -c /etc/sphinx/sphinx.conf fans_mapping_idx --rotate >> /etc/sphinx/log/index_log.log
mysql -P 9306 -h 127.0.0.1 -e"TRUNCATE RTINDEX fans_mapping_rtx;ATTACH INDEX fans_mapping_idx TO RTINDEX fans_mapping_rtx;"
/usr/bin/indexer -c /etc/sphinx/sphinx.conf fans_unionids_idx --rotate >> /etc/sphinx/log/index_log.log
mysql -P 9306 -h 127.0.0.1 -e"TRUNCATE RTINDEX fans_unionids_rtx;ATTACH INDEX fans_unionids_idx TO RTINDEX fans_unionids_rtx;"
启动服务
#gedit /etc/init.d/sphinx
#!/bin/sh
# chkconfig: 2345 10 90
sphinx_pidfile=/etc/sphinx/log/searchd.pid
if [ -e "${sphinx_pidfile}" ] ; then
sphinx_active=1 #runing
#echo ${sphinx_active}
else
sphinx_active=0 #close
#echo ${sphinx_active}
fi
stop(){
if [ "${sphinx_active}" -eq 0 ];then
echo -e '\e[31m SPHINX Has been turned off \e[m' #红色
exit 1
else
/usr/local/sphinx302/bin/searchd --config /etc/sphinx/sphinx.conf --stop > /dev/null 2>&1
resu=$?
if [ "${resu}" -eq 0 ]; then
echo -e '\e[32m******************************************************************** \e[m' #绿色
echo -e '\e[32m( ^_^ ) Sphinx stop SUCESS( ^_^ ) \e[m' #绿色
echo -e '\e[32m******************************************************************** \e[m' #绿色
else
echo -e '\e[31m******************************************************************** \e[m' #红色
echo -e '\e[31m !o(︶︿︶)o!Sphinx stop FAIL! ~~~~(>_<)~~~~ \e[m' #红色
echo -e '\e[31m******************************************************************** \e[m' #红色
fi
return ${resu}
fi
}
start(){
if [ "${sphinx_active}" -eq 1 ];then
echo -e '\e[31m SPHINX Is already running \e[m' #红色
exit 1
else
/usr/local/sphinx302/bin/searchd --config /etc/sphinx/sphinx.conf > /dev/null 2>&1
resu=$?
if [ "${resu}" -eq 0 ]; then
echo -e '\e[32m******************************************************************** \e[m' #绿色
echo -e '\e[32m ( ^_^ )Sphinx start SUCESS( ^_^ ) \e[m' #绿色
echo -e '\e[32m******************************************************************** \e[m' #绿色
else
echo -e '\e[31m******************************************************************** \e[m' #红色
echo -e '\e[31m !o(︶︿︶)o!Sphinx start FAIL! ~~~~(>_<)~~~~ \e[m' #红色
echo -e '\e[31m******************************************************************** \e[m' #红色
fi
return ${resu}
fi
}
indexer_all(){
if [ "${sphinx_active}" -eq 0 ];then
echo -e '\e[31m SPHINX Has been turned off \e[m' #红色
exit 1
else
./usr/local/sphinx/bin/indexer --config /etc/sphinx/sphinx.conf --all > /dev/null 2>&1
resu=$?
if [ "${resu}" -eq 0 ]; then
echo -e '\e[32m******************************************************************** \e[m' #绿色
echo -e '\e[32m ( ^_^ )Sphinx indexer_all SUCESS( ^_^ ) \e[m' #绿色
echo -e '\e[32m******************************************************************** \e[m' #绿色
else
echo -e '\e[31m******************************************************************** \e[m' #红色
echo -e '\e[31m !o(︶︿︶)o!Sphinx indexer_all FAIL! ~~~~(>_<)~~~~ \e[m' #红色
echo -e '\e[31m******************************************************************** \e[m' #红色
fi
return ${resu}
fi
}
indexer_online(){
if [ "${sphinx_active}" -eq 0 ];then
echo -e '\e[31m SPHINX Has been turned off \e[m' #红色
exit 1
else
/usr/local/sphinx/bin/indexer --config /etc/sphinx/sphinx.conf --rotate --all > /dev/null 2>&1
resu=$?
if [ "${resu}" -eq 0 ]; then
echo -e '\e[32m******************************************************************** \e[m' #绿色
echo -e '\e[32m ( ^_^ )Sphinx indexer_online SUCESS( ^_^ ) \e[m' #绿色
echo -e '\e[32m******************************************************************** \e[m' #绿色
else
echo -e '\e[31m******************************************************************** \e[m' #红色
echo -e '\e[31m !o(︶︿︶)o!Sphinx indexer_online FAIL! ~~~~(>_<)~~~~ \e[m' #红色
echo -e '\e[31m******************************************************************** \e[m' #红色
fi
return ${resu}
fi
}
status(){
if [ "${sphinx_active}" -eq 0 ];then
echo -e '\e[31m SPHINX Has been turned off \e[m' #红色
exit 1
else
/usr/local/sphinx302/bin/searchd --config /etc/sphinx/sphinx.conf --status
resu=$?
echo -e '\e[32m******************************************************************** \e[m' #绿色
echo -e '\e[32m ( ^_^ )If you see output status that mean sphinx is work( ^_^ ) \e[m' #绿色
echo -e '\e[32m******************************************************************** \e[m' #绿色
return ${resu}
fi
}
case $1 in
restart)
stop
start
;;
stop)
stop
;;
start)
start
;;
indexer_all)
indexer_all
;;
indexer_online)
indexer_online
;;
status)
status
;;
esac
exit 0