此配置本人经过测试
CentOS 6.0 64bit 上搭建Sphinx环境
一、获取相关开源程序
1. 利用 CentOS 系统自带的yum命令安装、升级所需的程序库
1 |
sudo -s |
2 |
LANG=C |
3 |
yum install make gcc
g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel expat-devel |
2. 下载所需的开源文件以及组件
使用下述命令创建文件夹以保存下载的文件
1 |
cd / |
2 |
mkdir software |
此处仅列出所需要的文件以及官方网址
coreseek 【http://www.coreseek.cn】
二、安装 coreseek-3.2.14
1、解压缩 coreseek-3.2.14.tar.gz
1 |
tar zxvf
coreseek-3.2.14. tar .gz |
2 |
cd coreseek-3.2.14 |
2、安装mmseg
1 |
cd mmseg-3.2.14/ |
2 |
./bootstrap |
3 |
./configure
--prefix=/usr/ local /webserver/sphinx/mmseg |
4 |
make && make install |
5 |
cd .. |
3、安装 csft-3.2.14
指定–enable-id64选项会打开64位文档ID和词ID的支
1 |
cd csft-3.2.14/ |
2 |
sh
buildconf.sh |
3 |
export LIBS= "-liconv" |
4 |
./configure
--prefix=/usr/ local /webserver/sphinx/coreseek
--without-unixodbc --with-mmseg --with-mmseg-includes=/usr/ local /webserver/sphinx/mmseg/include/mmseg/
--with-mmseg-libs=/usr/ local /webserver/sphinx/mmseg/lib/
--with-mysql --with-iconv |
5 |
make && make install |
6 |
cd .. |
我们在编译软件的时候,是不是经常遇到下面的错误信息呢?
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking for gcc... gcc
checking for C compiler default output file name...
configure: error: C compiler cannot create executables
See `config.log' for more details.
有很多人建议重装GCC,但是确无济于事。
这个错误产生的原因其实很简单: 由于我们在编译软件之前,进行了export操作,改变了CFLAGS和LIBS的值。
这个时候只要讲这个值清空就可以了。
sh export LIBS=
sh export CFLAGS=
如果出现undefined reference to `libiconv’的类似错误,可以按照如下方法处理:
##方法一:(Linux使用)
## 直接执行:export LIBS=”-liconv”
##然后再次configure后,进行编译安装make && make install
## 方法二:
## 首先configure,然后vim src/makefile
## 在其中搜索lexpat,在其后加上 -liconv
## 修改后该行应该为:-lexpat -liconv -L/usr/local/lib
## 然后再次make && make install
## 方法三:
## 首先configure,然后vim config/config.h
## 在其中搜索USE_LIBICONV,将其后的1修改为0
## 然后再次make && make install
如果出现错误提示:“ERROR: cannot find MySQL include files…….To disable MySQL support, use –without-mysql option.“,可按照如下方法处理:
##请找到头文件mysql.h所在的目录,一般是/usr/local/mysql/include,请替换为实际的
##请找到库文件libmysqlclient.a所在的目录,一般是/usr/local/mysql/lib,请替换为实际的
##configure参数加上:–with-mysql-includes=/usr/local/mysql/include –with-mysql-libs=/usr/local/mysql/lib,执行后,重新编译安装
4、测试安装
1 |
/usr/ local /webserver/sphinx/coreseek/bin/indexer
-c /usr/ local /webserver/sphinx/coreseek/etc/sphinx-min.conf.dist |
如果显示下述信息,则安装正常
1 |
Coreseek
Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)] |
2 |
Copyright
(c) 2007-2011, |
3 |
Beijing
Choice Software Technologies Inc (http://www.coreseek.com) |
4 |
5 |
using
config file '/usr/local/webserver/sphinx/coreseek/etc/sphinx-min.conf.dist'... |
6 |
total
0 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg |
7 |
total
0 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg |
三、具体的使用和配置请查看官方文档
/usr/local/webserver/sphinx/coreseek/bin/searchd -c /usr/local/webserver/sphinx/coreseek/etc/sphinx-min.conf.dist
/usr/local/webserver/sphinx/coreseek/bin/indexer -c /usr/local/webserver/sphinx/coreseek/etc/sphinx-min.conf.dist --all --rotate
/usr/local/webserver/sphinx/coreseek/bin/search -c /usr/local/webserver/sphinx/coreseek/etc/sphinx-min.conf.dist 程序
/usr/local/webserver/sphinx/coreseek/etc/sphinx-min.conf.dist
source src1{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = 123456
sql_db = test
sql_port = 3306
sql_query_pre = SET NAMES utf8
sql_query = SELECT id,group_id,author_id,UNIX_TIMESTAMP(date_added) as date_added,title,content FROM documents
sql_attr_uint = author_id
sql_attr_uint = group_id
sql_attr_timestamp = date_added
sql_query_info_pre = SET NAMES utf8
}
index src1{
source = src1
path = /usr/local/coreseek/var/data/src1/
docinfo = extern
mlock =0
morphology = none
min_word_len =1
html_strip =0
charset_type = zh_cn.utf-8
charset_dictpath = /usr/local/webserver/sphinx/mmseg/etc/
#charset-table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
#ngram_len = 1
#ngram_chars = U+3000..U+2FA1F
}
searchd{
listen = 9312
read_timeout =5
max_children = 30
max_matches = 1000
seamless_rotate = 0
preopen_indexes = 0
unlink_old = 1
pid_file = /usr/local/coreseek/var/log/searchd_rtindex.pid
log = /usr/local/coreseek/var/log/searchd_rindex.log
query_log = /usr/local/coreseek/var/log/query_rtindex.log
}
a. source是配置数据源,按照提示输入MYSQL的主机、帐号、密码和数据库即可,我的MYSQL就安装在本机上(MYSQL的安装可自行百度)
b. sql_query_pre是在执行查询之前执行的SQL语句。(注意:在coreseek只能识别utf8字符集编码,所以我们要执行转换一下)
c. sql_query是要查询进行索引的SQL语句,sql_attr_unit和sql_attr_timestamp是设置属性的,属性在全文检索中可以用来设置过滤和排序。
d. index和source应该是成对出现,index就是配置索引的功能(我们还可以配置多个索引 主索引+增量索引的功能)
e. searchd是常驻进程的全文检索服务,默认监控本机的9312端口
f. charset_type和charset_dictpath是中文分词配置
4. 创建索引和测试全文检索
上面我们配置了MYSQL数据源,我们要做到是test库中的documents表进行全文检索,为了测试全文检索的效率,我批量插入了该表30W的数据,以下我们就对该表进行索引测试和全文检索测试。
a. 测试indexer索引
# /usr/local/webserver/sphinx/coreseek/bin/indexer -c /usr/local/webserver/sphinx/coreseek/etc/sphinx-min.conf.dist
Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)
using config file '/usr/local/webserver/sphinx/coreseek/etc/sphinx-min.conf.dist'...
total 0 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
total 0 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/cal
b. 创建sphinx索引文件(30W的数据我创建花了2分钟左右,索引文件大概200MB)
/usr/local/webserver/sphinx/coreseek/bin/indexer -c /usr/local/webserver/sphinx/coreseek/etc/sphinx-min.conf.dist --all --rotate
Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)
using config file '/usr/local/webserver/sphinx/coreseek/etc/sphinx-min.conf.dist'...
indexing index 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 230 bytes
total 0.007 sec, 32158 bytes/sec, 559.28 docs/sec
total 2 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
total 7 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
rotating indices: succesfully sent SIGHUP to searchd (pid=3626)
c. 使用search程序测试全文检索(注意是search不是searchd)
/usr/local/webserver/sphinx/coreseek/bin/search -c /usr/local/webserver/sphinx/coreseek/etc/sphinx-min.conf.dist -a 我们在编译软件的时候
Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)
using config file '/usr/local/webserver/sphinx/coreseek/etc/sphinx-min.conf.dist'...
index 'test1': query '我们在编译软件的时候 ': returned 1 matches of 1 total in 0.003 sec
displaying matches:
1. document=2, weight=14, group_id=1, date_added=Tue Mar 26 13:37:41 2013
id=2
group_id=1
group_id2=6
date_added=2013-03-26 13:37:41
title=test two 编译软件
content=this is my test document number two
words:
1. '我们': 0 documents, 0 hits
2. '在': 0 documents, 0 hits
3. '编译': 1 documents, 1 hits
4. '软件': 1 documents, 1 hits
5. '的': 0 documents, 0 hits
6. '时候': 0 documents, 0 hits
d. 开启searchd服务
开启searchd服务,使能用API程序进行调用(注意:你需要配置你的防火墙或者关闭,和关闭selinux)
- service iptables stop #暂时关闭防火墙
- setenforce 0 # 暂时关闭selinux
$ /usr/local/coreseek/bin/searchd -c etc/csft.conf
##以下为正常开启搜索服务时的提示信息:(csft-4.0版类似)
Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
Copyright (c) 2007-2010,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)
using config file 'etc/csft.conf'...
listening on all interfaces, port=9312
##如要停止搜索服务,请使用/usr/local/coreseek/bin/searchd -c etc/csft.conf --stop
##如要已启动服务,要更新索引,请使用/usr/local/coreseek/bin/indexer -c etc/csft.conf --all --rotate
5. 使用PHP API调用coreseek
复制api/sphinxapi.php文件到你的项目,以下是我的一个PHP测试脚本程序
<?php
error_reporting(E_ALL ^ E_NOTICE);
header("Content-type: text/html; charset=utf-8");
require ( "sphinxapi.php" );
define(INDEX_SRC1, 'src1');
$cl = new SphinxClient();
$cl->SetServer("10.2.4.15",9312);
$cl->SetArrayResult(true);
$keyword = "媒体";
$result = $cl->Query($keyword, INDEX_SRC1);
//处理$result
$matches = isset($result['matches']) ? $result['matches'] : '';
if(is_array($matches)){
foreach ($matches as $v){
$ids[] = $v['id'];
}
}else{
print("<pre>");
print $cl->GetLastError();
print $cl->GetLastWarning();
//print "没找到了亲~";
print("<pre>");
return;
}
$ids = implode(',', $ids);
//拿着id ,拉库
$link = mysql_connect('localhost','root','123456') or die('mysql link fail!');
mysql_select_db('test',$link);
mysql_query("SET NAMES UTF8");
$sql = "select * from documents where id in({$ids})";
$result = mysql_query($sql,$link);
$data = array();
while ($row = mysql_fetch_assoc($result))
{
$data[] = $row;
}
mysql_close($link);
// 关键字高亮
$p_titles = array();
$p_contents = array();
$build_opts= array(//查询结果集设置
'before_match'=> '<font color="red">',
'after_match'=> '</font>',
'limit'=> '130',
//'exact_phrase'=> true,
'single_passage'=> true,
'chunk_separator' => '',
);
foreach($data as $key=>$value)
{
$p_titles[$key] = $value['title'];
$p_contents[$key] = $value['content'];
}
$p_titles = $cl->BuildExcerpts($p_titles, INDEX_SRC1, $keyword, $build_opts);
$p_contents = $cl->BuildExcerpts($p_contents, INDEX_SRC1, $keyword, $build_opts);
foreach($data as $key=>$post)
{
$data[$key]['title'] = $p_titles[$key];
$data[$key]['content'] = $p_contents[$key];
}
var_dump($data);
?>
6. 设置coreseek自动启动
在/etc/rc.d/rc.local文件中加入以下行
/usr/local/webserver/sphinx/coreseek/bin/searchd -c /usr/local/webserver/sphinx/coreseek/etc/sphinx-min.conf.dist
程序启动你的服务器,使用命令 ps -aux查看是否已经启动成功了。
7. 中文分词mmseg的一些用法
官方文档:www.coreseek.cn/opensource/mmseg/#mmseg_ini
LibMMSeg 是Coreseek.com为 Sphinx 全文搜索引擎设计的中文分词软件包,其在GPL协议下发行的中文分词法,采用Chih-Hao Tsai的MMSEG算法
词典的构造
mmseg -u unigram.txt
该命令执行后,将会在unigram.txt所在目录中产生一个名为unigram.txt.uni的文件,将该文件改名为uni.lib,完成词典的构造。需要注意的是,unigram.txt需要预先准备,并且编码格式必须为UTF-8编码
mmseg -d <dict_dir> tobe_segment.txt
其中,命令使用‘-d’开关指定词库文件所在的位置,参数dict_dir为词库文件(uni.lib )所在的目录;tobe_segment.txt 为待切分的文本文件,必须为UTF-8编码。如果一切正确,mmseg会将切分结果以及所花费的时间显示到标准输出上。
对特殊短语的支持
由于LibMMSeg是为Sphinx全文搜索引擎设计的,因此其内置了部分搜索引擎切分算法的特性,主要表现在对特殊短语的支持上。
在搜索引擎中,需要处理C++时,如果分词器中没有词组C++,则将被切分为C/x +/x +/x,在进一步的检索中,可能每个词会由于出现的过于频繁而被过滤掉,导致搜索的结果与 C++相关度不高不说,也严重影响的全文搜索的速度。在LibMMSeg中,内置对特殊短语的支持。
其输入文件格式如下
// test commit
.net => dotnet
c# => csharp
c++ => cplusplus
其中左侧是待支持的特殊短语,右侧是左侧的特殊短语需要被转换为的短语。这一转换在分词前进行。
可以在行的开头加入'//'作为注释符号,发现符号'//'后,整行将被忽略。
特殊短语词库构造命令:
mmseg -b exceptions.txt
其中, 开关'-b'指示mmseg是要构造特殊短语词库;exceptions.txt是用户编辑的特殊短语转换规则。
该命令执行后,将在当前目录下产生一个名为"synonyms.dat"的文件,将该文件放在"uni.lib"同一目录下,分词系统将自动启动特殊短语转换功能。
分词
mmseg -d tobe_segment.txt
其中,命令使用‘-d’开关指定词库文件所在的位置,参数dict_dir为词库文件(uni.lib )所在的目录;tobe_segment.txt 为待切分的文本文件,必须为UTF-8编码。如果一切正确, mmseg会将切分结果以及所花费的时间显示到标准输出上。
注意:分词中的相似词组,比如要在词库里有存在的配置才会有效!
总结:当然以上都是一些安装的菜鸟级别应用,coreseek是一个强大系统官方也有很全的文档,还有查询表达式、增量索引、实时索引、中文词库、分布式索引等高级的应用。