1、什么是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项目重名

2、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 中文分词
http://www.coreseek.com/uploads/sources/mmseg-0.7.3.tar.gz

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

sphinx-0.9.8-rc2.tar.gz sphinx-0.9.8-rc2源代码
http://www.sphinxsearch.com/downloads/sphinx-0.9.8-rc2.tar.gz

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

fix-crash-in-excerpts.patch sphinx支持分词补丁
http://www.coreseek.com/uploads/sources/fix-crash-in-excerpts.patch

sphinx-0.98rc2.zhcn-support.patch sphinx支持分词补丁
http://www.coreseek.com/uploads/sources/sphinx-0.98rc2.zhcn-support.patch

二、开始安装
1.安装libmmseg

安装mmseg完成,测试一下

 

  1. tar -zxvf mmseg-0.7.3.tar.gz   
  2. cd mmseg-0.7.3   
  3. ./configure --prefix=/usr/local/mmseg   
  4. make   
  5. make install   
  6. cd ..  

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

  1. echo '/usr/local/mmseg/lib' >> /etc/ld.so.conf
  2. ldconfig -v
  3. ln -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

3.安装sphinx

  1. cd /root/lemp/sphinx-0.9.8-rc2   
  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 --with-mmseg-libs=/usr/local/mmseg/lib --with-mmseg   
  5. make  
  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=/usr/local/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
  5. /root/sphinx/sphinx-0.9.8-rc2/src/tokenizer_zhcn.cpp:34: undefined reference to `libiconv_close'
  6. 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=/usr/local/mysql/include/mysql --with-mysql-libs=/usr/local/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
  1. cd /usr/local/sphinx/etc
  2. cp sphinx.conf.dist sphinx.conf

4.配置sphinx

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

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

5.索引建立

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

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

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

索引的建立方法为

  1. /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
首先建立一张索引专用表:
  1. CREATE TABLE `sphinx` (
  2. `id` int(11) NOT NULL,
  3. `weight` int(11) NOT NULL,
  4. `query` varchar(255) NOT NULL,
  5. `CATALOGID` INT NOT NULL,
  6. `EDITUSERID` INT NOT NULL,
  7. `HITS` INT NULL,
  8. `ADDTIME` INT NOT NULL,   KEY
  9. `Query` (`Query`)
  10. ) ENGINE=SPHINX DEFAULT CHARSET=utf8 CONNECTION='sphinx://localhost:3312/test1'
 

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

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

(如果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就可以搜出结果了

使用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 #检查下
  5. service sphinx start #运行
  6. service sphinx stop  #停止,官方的脚本在我的as4上有点问题,所以粗鲁的改了下
  7. service sphinx restart #重启
  8. service sphinx status #查看是否运行
  9. #检查下已用sphinx用户运行
  10. ps aux |grep searchd
  11. sphinx   24612  0.0  0.3 11376 6256 pts/1    S    14:07   0:00 searchd

转自:http://blog.csdn.net/lgm252008/article/details/5373436

sphinx的更多相关文章

  1. 利用sphinx为python项目生成API文档

    sphinx可以根据python的注释生成可以查找的api文档,简单记录了下步骤 1:安装 pip install -U Sphinx 2:在需要生成文档的.py文件目录下执行sphinx-apido ...

  2. 安装PHP sphinx扩展 sphinx-1.1.0/sphinx.c:105:2: error: too few arguments 错误

    表现: /home/sphinx-1.1.0/sphinx.c: In function 'php_sphinx_client_read_property':/home/sphinx-1.1.0/sp ...

  3. 【整理】Linux下中文检索引擎coreseek4安装,以及PHP使用sphinx的三种方式(sphinxapi,sphinx的php扩展,SphinxSe作为mysql存储引擎)

          一,软件准备 coreseek4.1 (包含coreseek测试版和mmseg最新版本,以及测试数据包[内置中文分词与搜索.单字切分.mysql数据源.python数据源.RT实时索引等测 ...

  4. coreseek+sphinx+mysql+thinkphp整合

    1.安装coreseek 1.1首先升级或安装系统依赖库 yum install make gcc g++ automake libtool mysql-client libmysqlclient15 ...

  5. 全文检索解决方案(lucene工具类以及sphinx相关资料)

    介绍两种全文检索的技术. 1.  lucene+ 中文分词(IK) 关于lucene的原理,在这里可以得到很好的学习. http://www.blogjava.net/zhyiwww/archive/ ...

  6. 如何将Sphinx生成的html文档集成进入Django

    参考 http://stackoverflow.com/questions/10594618/django-and-sphinx-how-to-view-the-html-sphinx-generat ...

  7. Linux下搭建coreseek(sphinx+mmseg3)全文检索

    测试平台:Center OS 1.设置环境,升级/安装系统基础依赖包:m4.autoconf.automake.libtool #设置路径和中文环境: $ export PATH=/usr/local ...

  8. debian下使用Sphinx异常“Could not import extension sphinx.builders.linkcheck (exception: cannot import name SSLError)”的解决

    最近使用到Sphinx编译文档,出现如下异常: Extension error:Could not import extension sphinx.builders.linkcheck (except ...

  9. [搜索引擎]Sphinx的介绍和原理探索

    What/Sphinx是什么 定义 Sphinx是一个全文检索引擎. 特性 索引和性能优异 易于集成SQL和XML数据源,并可使用SphinxAPI.SphinxQL或者SphinxSE搜索接口 易于 ...

  10. Sphinx安装配置应用

    Sphinx 是由俄罗斯人Andrew Aksyonoff开发的一个全文搜索引擎.意图为其他应用提供高速.地空间占用.高结果相关度的全文搜索功能.Sphinx可以非常容易的与SQL数据库和脚本语言集成 ...

随机推荐

  1. node相关--socket.io

    使用Socket.IO可以避免webSocket产生的问题: 传输: Socket.IO中消息的传递是基于传输的,而非全部依靠WebSocket; 它总会尝试选择对用户来说速度最快.对服务器性能来说最 ...

  2. stack UVA 442 Matrix Chain Multiplication

    题目传送门 题意:给出每个矩阵的行列,计算矩阵的表达式,如果错误输出error,否则输出答案 分析:表达式求值,stack 容器的应用:矩阵的表达式求值A 矩阵是a * b,B 矩阵是b * c,则A ...

  3. kernel里面出现了类似isegfault at xxx ip 错误

    今天同时碰到了错误如下,但是没有core,当时通过代码审查解决了问题,后续再查的时候发现可以使用addr2line 查找代码行, segfault at 10000 ip b7f0f48a sp bf ...

  4. FireFox火狐浏览器与IE兼容问题 - 透明滤镜 DIV滚动条

    问题一:最简单的鼠标移过手变型的css要改了 cursor:pointer;/*FireFox(火狐)不支持cursor:hand*/ dw8下面自动出来的也没有hand这个属性了,标准的是point ...

  5. winform学习之----打开文件对话框并将文件内容放入文本框

    OpenFileDialog ofg = new OpenFileDialog(); ofg.Title = "ddd";//设置对话框标题 ofg.Multiselect = t ...

  6. OpenCV show two cameras 同时显示两个摄像头

    用OpenCV同时显示两个摄像头的内容的代码如下: #include <iostream> #include <stdio.h> #include <tchar.h> ...

  7. Programming with gtkmm 3

      https://developer.gnome.org/gtkmm-tutorial/unstable/index.html.zh_CN 1. 序言 1.1. 本书 1.2. gtkmm 2. 安 ...

  8. vs2005中删除最近打开的项目和文件的记录

    vs2005中总是保留最近打开的项目和文件的记录,甚至是以删除的它也不删,-_-!下面介绍几种删除的方法: 第一种:建立一个bat文件,以后双击即可清除,内置代码如下: @echo off@REG D ...

  9. Nodejs - windows的系统变量(环境变量)

    我的电脑-属性-高级-环境变量-系统变量(s)-Path 将Node.exe所在的路径插入Path的变量值(V)中 如 ;E:\nodejs\ 最终效果 C:\Windows\system32;C:\ ...

  10. T-SQL查询进阶--深入浅出视图

    视图可以看作定义在SQL Server上的虚拟表.视图正如其名字的含义一样,是另一种查看数据的入口.常规视图本身并不存储实际的数据,而仅仅存储一个Select语句和所涉及表的metadata. 视图简 ...