sphinx+mysql+mmseg

来源:互联网 发布:dz论坛数据库配置文件 编辑:程序博客网 时间:2024/06/08 01:27

from:

http://hi.baidu.com/liemeng/blog/item/bd60546679162428ab184ccd.html

http://www.cnblogs.com/chenzehe/archive/2010/11/04/1869388.html

关注:

http://blog.csdn.net/jucrazy/article/details/6739162

什么是Sphinx
Sphinx 是一个在GPLv2 下发布的一个全文检索引擎,商业授权(例如, 嵌入到其他程序中)
需要联系我们(Sphinxsearch.com)以获得商业授权。
一般而言,Sphinx是一个独立的搜索引擎,意图为其他应用提供高速、低空间占用、高结果
相关度的全文搜索功能。Sphinx可以非常容易的与SQL数据库和脚本语言集成。
当前系统内置MySQL和PostgreSQL 数据库数据源的支持,也支持从标准输入读取特定格式
的XML数据。通过修改源代码,用户可以自行增加新的数据源(例如:其他类型的DBMS
的原生支持)。
搜索API支持PHP、Python、Perl、Rudy和Java,并且也可以用作MySQL存储引擎。搜索
API非常简单,可以在若干个小时之内移植到新的语言上。
Sphinx 是SQL Phrase Index的缩写,但不幸的和CMU的Sphinx项目重名

Sphinx的特性

  • 高速的建立索引(在当代CPU上,峰值性能可达到10 MB/秒);

  • 高性能的搜索(在2 – 4GB 的文本数据上,平均每次检索响应时间小于0.1秒);

  • 可处理海量数据(目前已知可以处理超过100 GB的文本数据, 在单一CPU的系统上可
    处理100 M 文档);

  • 提供了优秀的相关度算法,基于短语相似度和统计(BM25)的复合Ranking方法;

  • 支持分布式搜索;

  • provides document exceprts generation;

  • 可作为MySQL的存储引擎提供搜索服务;

  • 支持布尔、短语、词语相似度等多种检索模式;

  • 文档支持多个全文检索字段(最大不超过32个);

  • 文档支持多个额外的属性信息(例如:分组信息,时间戳等);

  • 停止词查询;

  • 支持单一字节编码和UTF-8编码;

  • 原生的MySQL支持(同时支持MyISAM 和InnoDB );

  • 原生的PostgreSQL 支持.

一、安装所需文件
mmseg-0.7.3.tar.gz中文分词

mysql-5.1.26-rc.tar.gz mysql-5.1.26源代码

sphinx-0.9.9.tar.gz sphinx-0.9.9-release源代码

fix-crash-in-excerpts.patch sphinx支持分词补丁

sphinx-0.98rc2.zhcn-support.patch sphinx支持分词补丁


二、开始安装
1.安装libmmseg

tar -zxvf mmseg-0.7.3.tar.gzcd mmseg-0.7.3./configure --prefix=/usr/local/mmsegmakemake installcd ..

安装mmseg完成,测试一下

mmseg Coreseek COS(tm) MM Segment 1.0Copyright By Coreseek.com All Right Reserved.Usage: mmseg <option> <file>-u <unidict>           Unigram Dictionary-r           Combine with -u, used a plain text build Unigram Dictionary, default Off-b <Synonyms>           Synonyms Dictionary-h            print this help and exit


有问题可以尝试执行下面命令

echo '/usr/local/mmseg/lib' >> /etc/ld.so.confldconfig -vln -s /usr/local/mmseg/bin/mmseg /bin/mmseg

2.重新编译mysql


在安装之前先打两个补丁,这个是支持中文必须打的补丁

  1. tar -zxvf sphinx-0.9.8-rc2.tar.gz
  2. cd sphinx-0.9.8
  3. patch -p1 < ../sphinx-0.98rc2.zhcn-support.patch
  4. patch -p1 < ../fix-crash-in-excerpts.patch

之前我已安装了mysql5.1.26,这里略过具体安装步骤
mysql编译路径
/root/lemp/mysql-5.1.26-rc/
mysql安装路径
/opt/mysql

安装之前关闭mysql

  1. /opt/mysql/bin/mysql.server stop

接着将sphinx下的mysqlse 文件夹下的数据拷贝到mysql-5.1.26-rc/storage/sphinx 下面
(这样才能在编译mysql的时候把SphinxSE存储引擎编译进去)

  1. cp -rf mysqlse /root/lemp/mysql-5.1.26-rc/storage/sphinx
  2. cd /root/lemp/mysql-5.1.26-rc

  3. make clean
  4. sh BUILD/autorun.sh
  5. #这步是必须的,请勿遗漏

开始重新编译

  1. CFLAGS="-O3" CXX=gcc CXXFLAGS="-O3 -felide-constructors -fno-exceptions -fno-rtti" ./configure --prefix=/opt/mysql --localstatedir=/opt/mysql/var --sysconfdir=/opt/mysql --without-debug --with-unix-socket-path=/opt/mysql/mysql.sock --with-big-tables --with-charset=gbk --with-collation=gbk_chinese_ci --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --with-extra-charsets=gbk,gb2312,utf8 --with-pthread --enable-thread-safe-client --with-innodb --with-plugins=sphinx
  2. make
  3. make install
  1. configure: error: unknown plugin: sphinx

解决方法:
sudo yum install autoconf automake libtool

sh BUILD/autorun.sh
./configure -h
查看最低下有否包含了sphinx

  1. === Sphinx Storage Engine ===
  2. Plugin Name: sphinx
  3. Description: Sphinx Storage Engines
  4. Supports build: static and dynamic
  5. Configurations: max, max-no-ndb

然后再编译

make时错误

  1. ../libtool: line 466: CDPATH: command not found
  2. ../libtool: line 1144: func_opt_split: command not found
  3. libtool: Version mismatch error. This is libtool 2.2.6, but the
  4. libtool: definition of this LT_INIT comes from an older release.
  5. libtool: You should recreate aclocal.m4 with macros from libtool 2.2.6
  6. libtool: and run autoconf again.
  7. make[1]: *** [conf_to_src] 错误 63
  8. make[1]: Leaving directory `/home/andychu/lemp2/mysql-5.1.26-rc/strings'
  9. make: *** [all-recursive] 错误 1

libtool版本不一至产生错误,可以将已安装的libtool覆盖到编译目录
cp /usr/local/bin/libtool .
再重新编译

 

在fedora 下面可能会提示  libtinfo.so.5 缺少

cd client

vim Makefile

找到 LIBS 在后面添加 /lib/libtinfo.so.5

 

编译完成,启动mysql,检查SphinxSE存储引擎是否编译进去

/opt/mysql/bin/mysql.server start/opt/mysql/bin/mysql -uroot -p

mysql> show engines;+------------+---------+-----------------------------------------------------------+--------------+------+------------+| Engine     | Support | Comment                                                   | Transactions | XA   | Savepoints |+------------+---------+-----------------------------------------------------------+--------------+------+------------+| CSV        | YES     | CSV storage engine                                        | NO           | NO   | NO         || SPHINX     | YES     | Sphinx storage engine 0.9.9                               | NO           | NO   | NO         || MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables | NO           | NO   | NO         || MRG_MYISAM | YES     | Collection of identical MyISAM tables                     | NO           | NO   | NO         || MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance    | NO           | NO   | NO         |+------------+---------+-----------------------------------------------------------+--------------+------+------------+5 rows in set (0.00 sec)


此时我们可以看到已经包含sphinxSE 引擎

3.安装sphinx

./configure --prefix=/usr/local/sphinx --with-mysql=/usr/local/mysql/ --with-mysql-includes=/usr/local/mysql/include/mysql/ --with-mysql-libs=/usr/local/mysql/lib/mysql/ --with-mmseg-includes=/usr/local/mmseg/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg/lib --with-mmseg

头文件没找到:

  1. tokenizer_zhcn.cpp:1:30: SegmenterManager.h: 没有那个文件或目录
  2. tokenizer_zhcn.cpp:2:23: Segmenter.h: 没有那个文件或目录
  1. make clean
  2. ./configure --prefix=/usr/local/sphinx --with-mysql=/opt/mysql \
  3. --with-mysql-includes=/opt/mysql/include/mysql --with-mysql-libs=/opt/mysql/lib/mysql \
  4. --with-mmseg-includes=/usr/local/mmseg/include/mmseg --with-mmseg-libs=/usr/local/mmseg/lib --with-mmseg
  1. /root/sphinx/sphinx-0.9.8-rc2/src/tokenizer_zhcn.cpp:34: undefined reference to `libiconv_close'
  2. collect2: ld returned 1 exit status

官网解决办法:
In the meantime I've change the configuration file and set
#define USE_LIBICONV 0 in line 8179.
修改configure 文件把 #define USE_LIBICONV 0 最后的数值由1改为0
重新编译。

  1. make clean
  2. ./configure --prefix=/usr/local/sphinx --with-mysql=/opt/mysql \
  3. --with-mysql-includes=/opt/mysql/include/mysql --with-mysql-libs=/opt/mysql/lib/mysql \
  4. --with-mmseg-includes=/usr/local/mmseg/include/mmseg --with-mmseg-libs=/usr/local/mmseg/lib --with-mmseg

vi configure
输入/define USE_LIBICONV 找到目标行
按i键后将1改成0,按esc,输入:wq保存退出

  1. make
  2. make install

复制一份sphinx的配置

  1. cd /usr/local/sphinx/etc
  2. cp sphinx.conf.dist sphinx.conf

4.配置sphinx

修改/usr/local/sphinx/etc/sphinx.conf

 type = mysql # some straightforward parameters for SQL source types sql_host = localhost sql_user = root sql_pass = sql_db = test sql_port = 3306 # optional, default is 3306 address = 127.0.0.1 #安全点可以只监听本机



5.索引建立

装好sphinx后在sphinx的目录中有三个目录 分别为bin etc var
bin中 存有sphinx用到的一些执行文件 包括 indexer 索引建立 search 查询工具 searchd 查询服务器 等

为了下面测试方便,我们先将sphinx.conf自带的example.sql脚本倒入的mysql中
//创建了一个test库,并建立了documents测试表和数据


/opt/mysql/bin/mysql -uroot -p < /usr/local/sphinx/etc/example.sql

索引的建立方法为

/usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf test1


test1为资源名称 不写为默认所有都建立索引
附:
建立索引期间可能由于不同版本的数据库导致indexer找不到共享库libmysqlclient.so.16
需要把/opt/mysql/lib/mysql/libmysqlclient.so.16.0.0 这个文件复制到/usr/lib下 或者作软连接即可

6.查询服务器

/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf #为开启/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf --stop #为关闭

sphinx的查询 可以大致分为三种

1 数据库引擎中的查询

2 通过search工具查询
/usr/local/sphinx/bin/search --config /usr/local/sphinx/etc/sphinx.conf test

3 通过php的接口查询 详见sphinxapi.php.

三、在mysql中的使用SphinxSE方式调用Sphinx

1.在mysql中的使用SphinxSE方式调用Sphinx
首先建立一张索引专用表:

CREATETABLE`sphinx`(   `id`int(11)NOTNULL,   `weight`int(11)NOTNULL,   `query`varchar(255)NOTNULL,   `CATALOGID`INTNOTNULL,   `EDITUSERID`INTNOTNULL,   `HITS`INTNULL,   `ADDTIME`INTNOTNULL,  KEY `Query`(`Query`) ) ENGINE=SPHINXDEFAULTCHARSET=utf8CONNECTION='sphinx://localhost:3312/test1'

test1:索引的名称,可以在sphinx.conf中查找到
建立完索引专用表后,我们就可以在mysql中使用它了,例如,在mysql中输入

 SELECT doc.* FROM documents doc JOIN sphinx ON (doc.id =sphinx.id) WHERE query ='doc;mode= any'

运行后,结果记录中将会显示包含doc字符串的所用记录行

关于query的语法,还有sphinx的配置等信息你可以请参考:
http://www.sphinxsearch.com/doc.html

2.中文分词应用
生成词典
进入到mmseg的源代码目录后

  1. cd data
  2. mmseg -u unigram.txt

在data下边会产生一个文件unigram.txt.uni
这个就是生成的字典 然后把字典改名为uni.lib放在可以读取的目录中即可

  1. cp unigram.txt.uni /usr/local/sphinx/uni.lib

修改 sphinx.conf(/usr/local/sphinx/etc/sphinx.conf)配置文件
在索引中加入

  1. charset_type = zh_cn.utf-8
  2. charset_dictpath = /usr/local/sphinx/

数据库中增加一条中文数据

 INSERT INTO `test`.`documents` ( `id`, `group_id` , `group_id2` , `date_added` , `title` , `content` ) VALUES (NULL , '3', '9', NOW( ) , 'sphinx中文搜索', 'Sphinx是一个基于SQL的全文检索引擎,可以结合MySQL,PostgreSQL做全文搜索,它可以提供比数据库本身更专业的搜索功能,使得应用程序更容易实现专业化的全文检索。Sphinx特别为一些脚本语言设计搜索API接口,如PHP,Python,Perl,Ruby等,同时为 MySQL也设计了一个存储引擎插件。 ');


(如果searchd已经再运行,先kill 它再运行)
注意:在添加完数据后,需要重新加载索引,这样新的数据才能被缓存进去
重建索引,成功后,开启索引监听

  1. /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --all
  2. /usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf

在ide硬盘上建索引的速度

  1. indexing index 'test1'...
  2. collected 423228 docs, 637.2 MB
  3. sorted 125.5 Mhits, 100.0% done
  4. total 423228 docs, 637201412 bytes
  5. total 753.401 sec, 845766.13 bytes/sec, 561.76 docs/sec

这样,就可以在phpmyadmin中进行测试了。

  1. SELECT doc. * FROM documents doc JOIN sphinx ON ( doc.id = sphinx.id )
  2. WHERE query = '设计;mode= any '

好像没出来。。。。

修改 sphinx.conf
去掉下面的注释

  1. sql_query_pre = SET NAMES utf8

再重启searchd就可以搜出结果了

参考:
http://www.coreseek.com/uploads/pdf/sphinx_doc_zhcn_0.9.pdf
http://www.sphinxsearch.com/wiki/doku.php?id=sphinx_chinese_tutorial
http://www.cnblogs.com/hushixiu/articles/1295605.html
http://blog.xoyo.com/dcyhldcyhl/article/839863.shtml
http://blog.sina.com.cn/s/blog_5aefd9770100axf1.html
http://blog.s135.com/post/360/

2009-06-28更新
编码的解决方法(未经测试)
-------------------------------------------------------------------------------
转换现有表的数据
iconv -f GB18030 -t utf-8 -o dump.sql dump_utf8.sql

也可以不转换,直接使用现有的GBK的数据,但是需要设置连接方式
mysql_query("SET character_set_client = 'gbk'", $conn);
mysql_query("SET character_set_connection ='gbk'", $conn); //设置character_set_connection也就同时设置了collation_connection
//mysql_query("SET collation_connection = 'gbk'", $conn);
mysql_query("SET character_set_results ='utf8'", $conn);
这三个设置后,查询得到的结果,就是UTF8编码的结果了。合适给SPHINX使用。

mysql_query("SET SESSION query_cache_type=OFF", $conn);
//indexer建立索引时的查询,不需要缓存的
-------------------------------------------------------------------------------

2009-06-28更新
coreseek网站好像出问题不能下载咯。
提供安装部分文件下载,共3.6M,包含以下文件
build_delta_index.sh
build_main_index.sh
fix-crash-in-excerpts.patch
mmseg-0.7.3.tar.gz
sphinx-0.9.8-rc2.tar.gz
sphinx-0.98rc2.zhcn-support.patch
sphinx.conf
sphinxapi.php
test.php
test2.php

>>猛击这里下载sphinx安装文件

-------------------------------------------------------------------------------

2009-06-28更新
使用service 控制sphinx

先增加一sphinx用户并属于website组(事先已存在),并修改sphinx目录的属主

  1. useradd -d /usr/local/sphinx -g website -s /sbin/nologin sphinx
  2. chown -R sphinx:website /usr/local/sphinx

创建/etc/init.d/sphinx脚本

  1. #!/bin/sh
  2. # sphinx: Startup script for Sphinx search
  3. #
  4. # chkconfig: 345 86 14
  5. # description: This is a daemon for high performance full text \
  6. # search of MySQL and PostgreSQL databases. \
  7. # See http://www.sphinxsearch.com/ for more info.
  8. #
  9. # processname: searchd
  10. # pidfile: $sphinxlocation/var/log/searchd.pid

  11. # Source function library.
  12. . /etc/rc.d/init.d/functions

  13. processname=searchd
  14. servicename=sphinx
  15. username=sphinx
  16. sphinxlocation=/usr/local/sphinx
  17. pidfile=$sphinxlocation/var/log/searchd.pid
  18. searchd=$sphinxlocation/bin/searchd

  19. RETVAL=0

  20. PATH=$PATH:$sphinxlocation/bin

  21. start() {
  22.    echo -n $"Starting Sphinx daemon: "
  23.    daemon --user=$username --check $servicename $processname
  24.    RETVAL=$?
  25.    echo
  26.    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$servicename
  27. }

  28. stop() {
  29.    echo -n $"Stopping Sphinx daemon: "

  30.    $searchd --stop
  31.    #killproc -p $pidfile $servicename -TERM
  32.    RETVAL=$?
  33.    echo
  34.    if [ $RETVAL -eq 0 ]; then
  35.    rm -f /var/lock/subsys/$servicename
  36.    rm -f $pidfile
  37.    fi
  38. }

  39. # See how we were called.
  40. case "$1" in
  41.    start)
  42.    start
  43.    ;;
  44.    stop)
  45.    stop
  46.    ;;
  47.    status)
  48.    status $processname
  49.    RETVAL=$?
  50.    ;;
  51.    restart)
  52.    stop
  53. sleep 3
  54.    start
  55.    ;;
  56.    condrestart)
  57.    if [ -f /var/lock/subsys/$servicename ]; then
  58.    stop
  59. sleep 3
  60.    start
  61.    fi
  62.    ;;
  63.    *)
  64.    echo $"Usage: $0 {start|stop|status|restart|condrestart}"
  65.    ;;
  66. esac
  67. exit $RETVAL

修改权限并加入到service,机器启动时自动启动

  1. chmod 755 /etc/init.d/sphinx
  2. chkconfig --add sphinx
  3. chkconfig --level 345 sphinx on
  4. chkconfig --list|grep sphinx #检查下
  1. service sphinx start #运行
  2. service sphinx stop #停止,官方的脚本在我的as4上有点问题,所以粗鲁的改了下
  3. service sphinx restart #重启
  4. service sphinx status #查看是否运行

检查下已用sphinx用户运行

  1. ps aux |grep searchd
  2. sphinx 24612 0.0 0.3 11376 6256 pts/1 S 14:07 0:00 searchd

>>猛击这里下载sphinx启动脚本

参考:http://www.sphinxsearch.com/wiki/doku.php?id=for_centos_5
http://www.notsofaqs.com/catsdoc/doku.php?id=sphinx:install



原创粉丝点击