Sphinx讲解

来自技术开发小组内部wiki
跳转至: 导航搜索

Sphinx简介 

1.什么是全文检索 
全文检索是指以文档的全部文本信息作为检索对象的一种信息检索技术 。检索的对象有可能是文章的标题,也有可能是文章的作者,也有可能是文章摘要或内容。

2. 介绍 
Sphinx是一个基于SQL的全文检索引擎,可以结合MySQL做全文搜索,它可以提供比数据库本身更专业的搜索功能 ,使得应用程序更容易实现专业化的全文检索。Sphinx特别为一些脚本语言设计搜索API接口 ,如PHP ,Python,Perl,Ruby等,同时为MySQL也设计了一个存储引擎插件。

3. 现在我们先了解一下sphinx原理

http://wiki.fumubang.net/images/1/18/Sphinx_1.png

http://wiki.fumubang.net/images/e/eb/Sphinx_2.png

http://wiki.fumubang.net/images/2/2e/Sphinx_3.png

4. 具体操作

首先创建数据
mysql  #进入mysql
show databases;# 查看到有下面这些库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| mysql              | 
| test               | 
+--------------------+
3 rows in set (0.00 sec)

#进入test库,查看到有下面这些表,其中documents表是自动导进来的:
mysql> use test
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| documents      | 
| eht_articles   | 
| tags           | 
+----------------+
3 rows in set (0.01 sec)

查询documents表就能看到下面记录:
mysql> SELECT * FROM documents;
+----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+
| id | group_id | group_id2 | date_added          | title           | content                                                                   |
+----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+
|  1 |        1 |         5 | 2010-11-04 19:22:13 | test one        | this is my test document number one. also checking search within phrases. | 
|  2 |        1 |         6 | 2010-11-04 19:22:13 | test two        | this is my test document number two                                       | 
|  3 |        2 |         7 | 2010-11-04 19:22:13 | another doc     | this is another group                                                     | 
|  4 |        2 |         8 | 2010-11-04 19:22:13 | doc number four | this is to test groups                                                    | 
+----+----------+-----------+---------------------+-----------------+---------------------------------------------------------------------------+
4 rows in set (0.00 sec)


一、安装

 #确保又mysql库 没有的话预编译的时候会报错的,那就要先安mysql库,不过我这里就不讲了!

查看–如何在没安装MySQL的服务器上安装Sphinx(远程连接)?

官网:http://sphinxsearch.com/

wget  http://sphinxsearch.com/files/sphinx-1.10-beta.tar.gz

tar   zxvf     sphinx-1.10-beta.tar.gz

cd   sphinx-1.10-beta

./configure  –prefix=/usr/local/sphinx –with-mysql=/usr/local/mysql  

make &&make install

二、修改配置文件

vi /usr/local/sphinx/etc/sphinx.conf

具体实例配置文件:

​source article_src
{
type = mysql #####数据源类型
sql_host = 192.168.1.10 ######mysql主机
sql_user = root ########mysql用户名
sql_pass = pwd############mysql密码
sql_db = test #########mysql数据库名
sql_port= 3306 ###########mysql端口
sql_query_pre = SET NAMES UTF8 ###mysql检索编码,特别要注意这点,很多人中文检索不到是数据库的编码是GBK或其他非UTF8
sql_query = SELECT id,content FROM documents ####### 获取数据的sql

以下是用来过滤或条件查询的属性############

sql_attr_uint = cat_id ######## 无符号整数属性
sql_attr_uint = member_id
sql_attr_timestamp = created ############ UNIX时间戳属性

sql_query_info = select * from documents where id=$id ######### 用于命令界面端(CLI)调用的测试

}

index article
{
source = article_src ####声明索引源
path = /usr/local/sphinx/var/data/article #######索引文件存放路径及索引的文件名
docinfo = extern ##### 文档信息存储方式
mlock = 0 ###缓存数据内存锁定
morphology = none #### 形态学(对中文无效)
min_word_len = 1 #### 索引的词最小长度
charset_type = utf-8 #####数据编码


}​

索引器配置
indexer
{
mem_limit = 256M ####### 内存限制
}

sphinx 服务进程
searchd
{
    #listen = 9312 ### 监听端口,在此版本开始,官方已在IANA获得正式授权的9312端口,以前版本默认的是3312

    log = /usr/local/sphinx/var/log/searchd.log #### 服务进程日志 ,一旦sphinx出现异常,基本上可以从这里查询有效信息,轮换(rotate)出的问题一般可在此寻到答案
    query_log = /usr/local/sphinx/var/log/query.log ### 客户端查询日志,笔者注:若欲对一些关键词进行统计,可以分析此日志文件
    read_timeout = 5 ## 请求超时
    max_children = 30 ### 同时可执行的最大searchd 进程数
    pid_file = /usr/local/sphinx/var/log/searchd.pid #######进程ID文件
    max_matches = 1000 ### 查询结果的最大返回数
    seamless_rotate = 1 ### 是否支持无缝切换,做增量索引时通常需要
}

sphinx的配置文件创建完了,数据也导进去了,接下来就用下面命令来创建索引:

/usr/local/sphinx/bin/indexer --all
      创建索引是报了一个这样的错误:/usr/local/sphinx/bin/indexer: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory,主要原因是在上一篇中安装完mysql后没有设置环境变量,可以用下面方法解决:

locate libmysqlclient    #运行该命令找到关于libmysqlclient.so.16的文件
cp /usr/local/mysql/lib/mysql/libmysqlclient.so.16 /usr/lib/libmysqlclient.so.16    #然后把该文件的一个连接复制到在环境变量的文件夹/usr/lib/下

再次运行创建索引命令就能完成索引的创建了。 

然后用下面命令进行搜索测试:

/usr/local/sphinx/bin/search test

复制代码
代码

using config file '/usr/local/sphinx/etc/sphinx.conf'...
index 'test1': query 'test ': returned 3 matches of 3 total in 0.001 sec

displaying matches:
1. document=1, weight=2, group_id=1, date_added=Thu Nov  4 19:22:13 2010
        id=1
        group_id=1
        group_id2=5
        date_added=2010-11-04 19:22:13
        title=test one
        content=this is my test document number one. also checking search within phrases.
2. document=2, weight=2, group_id=1, date_added=Thu Nov  4 19:22:13 2010
        id=2
        group_id=1
        group_id2=6
        date_added=2010-11-04 19:22:13
        title=test two
        content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Thu Nov  4 19:22:13 2010
        id=4
        group_id=2
        group_id2=8
        date_added=2010-11-04 19:22:13
        title=doc number four
        content=this is to test groups

words:
1. 'test': 3 documents, 5 hits


安装coreseek
tar -zxvf coreseek-3.2.14.tar.gz
cd coreseek-3.2.14


cd mmseg-3.2.14/
安装报错
./configure --prefix=/alidata/server/mmseg --with-python --with-mysql --with-mmseg-includes=/usr/local/mmseg/include/mmseg --with-mmseg-libs=/usr/local/mmseg/lib/
解决
aclocal
libtoolize --force
automake --add-missing
autoconf
autoheader
make clean
./configure --prefix=/alidata/server/mmseg --with-python --with-mysql --with-mmseg-includes=/usr/local/mmseg/include/mmseg --with-mmseg-libs=/usr/local/mmseg/lib/
make && make install

安装csft
cd csft-3.2.14/
./configure --prefix=/alidata/server/coreseek --with-mysql=/alidata/server/mysql --with-mmseg=/alidata/server/mmseg 
    --with-mmseg-includes=/alidata/server/mmseg/include/mmseg/ --with-mmseg-libs=/alidata/server/mmseg/lib/
make && make install


修改配置
cd coreseek
cp sphinx.conf csft.conf


cd /alidata/server/coreseek/bin
./searchd 
netstat -tunpl | grep 9312


PHP模块
tar -xzvf sphinx-1.2.0.tgz
cd sphinx-1.2.0
/alidata/server/php/bin/phpize

cd csft-3.2.14/api/libsphinxclient/
./configure
make && make install

cd sphinx-1.2.0
./configure /alidata/server/php/bin/php-config 
./configure --with-php-config=/alidata/server/php/bin/php-config --with-sphinx
make && make install
cd /alidata/server/php/lib/php/extensions/no-debug-non-zts-20121212/
vi /alidata/server/php/etc/
nginx -s reload

这样的话,PHP就可以加载sphinx模块了,将可以调试搜索了。。。