PHP+Mysql+Sphinx高效的站内搜索引擎搭建详释.doc_第1页
PHP+Mysql+Sphinx高效的站内搜索引擎搭建详释.doc_第2页
PHP+Mysql+Sphinx高效的站内搜索引擎搭建详释.doc_第3页
PHP+Mysql+Sphinx高效的站内搜索引擎搭建详释.doc_第4页
PHP+Mysql+Sphinx高效的站内搜索引擎搭建详释.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PHP+Mysql+Sphinx高效的站内搜索引擎搭建详释l 为什么要使用Sphinx 假设你现在运营着一个论坛,论坛数据已经超过100W,很多用户都反映论坛搜索的速度非常慢,那么这时你就可以考虑使用Sphinx了(当然其他的全文检索程序或方法也行)。 l Sphinx是什么 Sphinx由俄罗斯人Andrew Aksyonoff 开发的高性能全文搜索软件包,在GPL与商业协议双许可协议下发行。 全文检索是指以文档的全部文本信息作为检索对象的一种信息检索技术。检索的对象有可能是文章的标题,也有可能是文章的作者,也有可能是文章摘要或内容。 l Sphinx的特性 高速索引 (在新款CPU上,近10 MB/秒);高速搜索 (2-4G的文本量中平均查询速度不到0.1秒);高可用性 (单CPU上最大可支持100 GB的文本,100M文档); 提供良好的相关性排名;支持分布式搜索; 提供文档摘要生成; 提供从MySQL内部的插件式存储引擎上搜索 ; 支持布尔,短语, 和近义词查询; 支持每个文档多个全文检索域(默认最大32个); 支持每个文档多属性; 支持断词; 支持单字节编码与UTF-8编码; l 下载并安装Sphinx 打开网址/news/7/52/ 找到适合自己的操作系统的版本,比如我是Windows那么我就可以下载Coreseek Win32通用版本,Linux下可以下载源码包,自己编译安装。这里解释下为什么我们下载的程序叫Coreseek,Coreseek是基于Sphinx开发的一款软件,对Sphinx做了一些改动,在中文方面支持得比Sphinx好,所以我们使用之。下载完成后,将程序解压到你想解压的地方,比如我就想解压到E盘根目录,之后修改目录名为Coreseek,大功告成Coreseek安装完成了,安装的目录是在E:coreseek。 l 使用Sphinx 我要使用Sphinx需要做以下几件事 1) 首先得有数据 2) 建立Sphinx配置文件3) 生成索引4) 启动Sphinx5) 使用之(调用api或search.exe程序进行查询)第1步:(导入数据)我们建立测试所需要用到得数据库、表以及数据,篇幅有限,这些在附件中都有,下载后导入MySQL即可。 第2步:(建立配置文件) 接下来我们需要建立一个Sphinx的配置文件 E:coreseeketcmysql.conf,将其内容改为下面这些:source mysql type = mysql sql_host = localhost sql_user = root sql_pass = sql_db = test sql_port = 3306 sql_query_pre = SET NAMES utf8 sql_query = SELECT id,addtime,title,content FROM post sql_attr_timestamp = addtime index mysql source = mysql path = E:/coreseek/var/data/mysql charset_dictpath = E:/coreseek/etc/ charset_type = zh_cn.utf-8 searchd listen = 9312 max_matches = 1000 pid_file = E:/coreseek/var/log/searchd_mysql.pid log = E:/coreseek/var/log/searchd_mysql.log query_log = E:/coreseek/var/log/query_mysql.log 先讲下这个配置文件中每项的含义。source mysql 定义源名称为mysql,也可以叫其他的,比如:source xxx type 数据源类型 。sql_* 数据相关的配置,比如sql_host,sql_pass什么的,这些不解释。sql_query 建立索引时的查询命令,在这里尽可能不使用where或group by,将where与groupby的内容交给sphinx,由sphinx进行条件过滤与groupby效率会更高,注意:select 的字段必须包括一个唯一主键以及要全文检索的字段,where中要用到的字段也要select出来 sql_query_pre 在执行sql_query前执行的sql命令, 可以有多条。sql_attr 以这个开头的配置项,表示属性字段,在where,orderby,groupby中出现的字段要分别定义一个属性,定义不同类型的字段要用不同的属性名,比如上面的sql_attr_timestamp就是时间戳类型。 index mysql 定义索引名称为mysql,也可以叫其他的,比如:index xxxsource 关联源,就是source xxx定义的。 path 索引文件存放路径,比如:E:/coreseek/var/data/mysql 实际存放在E:/coreseek/var/data/目录,然后创建多个名称为mysql后缀却不同的索引文件 charset_dictpath 指明分词法读取词典文件的位置,当启用分词法时,为必填项。在使用LibMMSeg作为分词 库时,需要确保词典文件uni.lib在指定的目录下 charset_type 字符集,比如charset_type = zh_cn.gbk searchd sphinx守护进程配置listen 监听端口max_matches最大匹配数,也就是查找的数据再多也只返回这里设置的1000条 pid_file pid文件路径log全文检索日志query_log查询日志好了,配置文件就这样,配置的参数还有很多,大家可以自己查文档。第3步:(生成索引) 开始 - 运行 - 输入cmd回车,打开命令行工具 e:coreseekbinindexer -config e:coreseeketcmysql.conf -all 这一串东西其实就是调用indexer程序来生成所有索引如果只想对某个数据源进行索引,则可以这样:e:coreseekbinindexer -config e:coreseeketcmysql.conf 索引名称(索引名称指配置文件中所定义的) -config,-all这些都是indexer程序的参数,想了解更多参数的朋友可以查看文档 运行命令后如果你没看到FATAL,ERROR这些东西,那么索引文件就算生成成功了,比如我看到得就是省略 using config file e:coreseeketcmysql.conf. indexing index mysql. collected 4 docs, 0.0 MB 省略 第4步:(启动Sphinx)同样命令行下 :e:coreseekbinsearchd -config e:coreseeketcmysql.conf 运行后提示了一大堆东西 省略using config file e:coreseeketcmysql.conf. listening on all interfaces, port=9312 accepting connections 不用管这些鸟文是啥意思,反正Sphinx是启动好了。 现在有一串鸟文的这个命令行是不能关的,因为关了Sphinx也就关了,如果觉得这样不爽,可以将Sphinx安装成系统服务,在后台运行,安装系统服务只需在命令行中输入以下命令:e:coreseekbinsearchd -config e:coreseeketcmysql.conf -install 安装之后记得启动这个服务,不会启动那我没法,自己google。第5步:(使用Sphinx)在web根目录下建立一个search目录(当然不在根目录也行,同样目录名也可以随取),复制E:coreseekapi sphinxapi.php文件到search目录(sphinxapi.php这个是sphinx官方提供的api),开始php程序的编写。 在search目录建立一个文件,名字叫啥都行,我管它叫index.php,其内容如下setServer(localhost, 9312); / 设置服务端,第一个参数sphinx服务器地址,第二个sphinx监听端口 $res = $sc-query(sphinx, mysql); / 执行查询,第一个参数查询的关键字,第二个查询的索引名称,mysql索引名称(这个也是在配置文件中定义的),多个索引名称以,分开,也可以用*表示所有索引。print_r($res); 打印结果: Array ( 省略 matches = Array ( 2 = Array ( weight = 2 attrs = Array ( addtime = 1282622004 ) ) 4 = Array ( weight = 2 attrs = Array ( addtime = 1282622079 ) ) ) 省略 ) Matches中就是查询的结果了,但是仿佛不是我们想要的数据,比如titile,content字段的内容就没有查询出来,根据官方的说明是Sphinx并没有连接到MySQL去取数据,只是根据它自己的索引内容进行计算,因此如果想用Sphinx提供的API去取得我们想要的数据,还必须以查询的结果为依据,再次查询MySQL从而得到我们想要的数据。 查询结果中键值分别表示 2唯一主键 weight权重 attrs sql_attr_*中配置 至此,搜索引擎算是完成一大半了,剩下的大家可以自行完成。 比如: a.z, _, a.z, U+410.U+42F-U+430.U+44F, U+430.U+44F # 简单分词,只支持0和1,如果要搜索中文,请指定为1 ngram_len = 1# 需要分词的字符,如果要搜索中文,去掉前面的注释 ngram_chars = U+3000.U+2FA1F# index test1stemmed : test1# # path = CONFDIR/data/test1stemmed # morphology = stem_en# # 如果没有分布式索引,注释掉下面的内容# index dist1# # distributed index type MUST be specified # type = distributed# local index to be searched # there can be many local indexes configured # local = test1 # local = test1stemmed# remote agent # multiple remote agents may be specified # syntax is hostname:port:index1,index2,. # agent = localhost:3313:remote1 # agent = localhost:3314:remote2,remote3# remote agent connection timeout, milliseconds # optional, default is 1000 ms, ie. 1 sec # agent_connect_timeout = 1000# remote agent query timeout, milliseconds # optional, default is 3000 ms, ie. 3 sec # agent_query_timeout = 3000# # 搜索服务需要修改的部分searchd # 日志 log = D:/sphinx/log/searchd.log# PID file, searchd process ID file name pid_file = D:/sphinx/log/searchd.pid# windows下启动searchd服务一定要注释掉这个 # seamless_rotate = 14.导入测试数据sql 文件在D:/sphinx/example.sqlC:Program FilesMySQLMySQL Server 5.0binmysql -uroot testindexer.exe test1 (备注:test1为 sphinx.conf 的 index test1() )Sphinx 0.9.8-release (r1533)Copyright (c) 2001-2008, Andrew Aksyonoffusing config file ./sphinx.confindexing index test1collected 4 docs, 0.0 MBsorted 0.0 Mhits, 100.0% donetotal 4 docs, 193 bytestotal 0.101 sec, 1916.30 bytes/sec, 39.72 docs/secD:sphinxbin6.搜索test试试D:sphinxbinsearch.exe test1Sphinx 0.9.8-release (r1533)Copyright (c) 2001-2008, Andrew Aksyonoffusing config file ./sphinx.confindex test1: query test : returned 3 matches of 3 total in 0.000 secdisplaying matches:1. document=1, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008id=1group_id=1group_id2=5date_added=2008-11-26 14:58:59title=test onecontent=this is my test document number one. also checking search withinphrases.2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008id=2group_id=1group_id2=6date_added=2008-11-26 14:58:59title=test twocontent=this is my test document number two3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008id=4group_id=2group_id2=8date_added=2008-11-26 14:58:59title=doc number fourcontent=this is to test groupswords:1. test: 3 documents, 5 hitsD:sphinxbin都所出来了吧。6.测试中文搜索修改test数据库中documents数据表,UPDATE test.documents SET title = 测试中文, content = this is my test document number two,应该搜的到吧 WHERE documents.id = 2;重建索引:D:sphinxbinindexer.exe test1搜索中文试试:D:sphinxbinsearch.exe 中文Sphinx 0.9.8-release (r1533)Copyright (c) 2001-2008, Andrew Aksyonoffusing config file ./sphinx.confindex test1: query 中文 : returned 0 matches of 0 total in 0.000 secwords:D:sphinxbin貌似没有搜到,这是因为windows命令行中的编码是gbk,当然搜不出来。我们可以用程序试试,在D:sphinxapi下新建一个 foo.php的文件,注意utf-8编码SetServer(localhost,9312);$result = $s-Query(中文);var_dump($result);?启动Sphinx searchd服务D:sphinxbinsearchd.exeSphinx 0.9.8-release (r1533)Copyright (c) 2001-2008, Andrew AksyonoffWARNING: forcing console mode on Windowsusing config file ./sphinx.confcreating server socket on :9312accepting connections已经表示服务器已经开启 不要关闭 命令行 (目前本人无法将此服务安装成系统服务器 所以每次PHP测试前注意开启 searchd 服务)执行PHP查询:访问 /sphinx/api/foo.php (自己配置的虚拟主机)结果是不是出来?剩下的工作就是去看手册,慢慢摸索高阶的配置。Windows下的Sphinx+MySQL简单配置及调试2010年01月15日 星期五 02:44 P.M.首先我们要从 Sphinx 官网上 /downloads.html 下载 mysql-5.0.45-sphinxse-0.9.8-win32.zip 和 sphinx--win32.zip,假设你已经安装好了 MySQL先将 mysql 服务停掉 解压 mysql-5.0.45-sphinxse-0.9.8-win32.zip 将 bin 和 share 覆盖掉 mysql 目录中的 bin 和 share 解压 sphinx--win32.zip 到独立的目录,如:d:/www/sphinx/中接着开启 mysql 服务,建立 test 数据库,并导入 sql 语句,如下:-CREATE TABLE documents (id int(11) NOT NULL auto_increment,group_id int(11) NOT NULL,group_id2 int(11) NOT NULL,date_added datetime NOT NULL,title varchar(255) NOT NULL,content text NOT NULL,PRIMARY KEY (id) ENGINE=InnoDB AUTO_INCREMENT=5;INSERT INTO documents VALUES (1, 1, 5, 2008-09-13 21:37:47, test one, this is my test document number one. also checking search within phrases.);INSERT INTO documents VALUES (2, 1, 6, 2008-09-13 21:37:47, test two, this is my test document number two);INSERT INTO documents VALUES (3, 2, 7, 2008-09-13 21:37:47, another doc, this is another group);INSERT INTO documents VALUES (4, 2, 8, 2008-09-13 21:37:47, doc number four, this is to test groups);-实际上,这个新建立的表就是 Sphinx 中的 example.sql我们的测试表已经建立完成,接下来我们要配置 sphinx-doc.conf 文件(重要)先将 sphinx 下的 sphinx-min.conf 复制一份改名为 sphinx-doc.conf,接着 修改它:# Minimal Sphinx configuration sample (clean, simple, functional)# type-数据库类型,目前支持 mysql 与 pgsql# strip_html-是否去掉html 标签# sql_host-数据库主机地址# sql_user-数据库用户名# sql_pass-数据库密码# sql_db-数据库名称# sql_port-数据库采用的端口# sql_query_pre-执行sql前要设置的字符集,用utf8必须SET NAMES utf8# sql_query-全文检索要显示的内容,在这里尽可能不使用where或 group by,将 where 与 groupby 的内容交给 sphinx,由 sphinx 进行条件过滤与 groupby 效率会更高# 注意: select 出来的字段必须至少包括一个唯一主键 (ARTICLESID) 以及要全文检索的字段,你计划原本在 where 中要用到的字段也要 select 出来# 这里不用使用orderby# sql_attr_ 开头的表示一些属性字段,你原计划要用在 where, orderby, groupby 中的字段要在这里定义(# 为自己添加的注释内容)#source 数据源名:source documentstype = mysqlsql_host = localhostsql_user = rootsql_pass = yourpasswordsql_db = testsql_port = 3306 # optional, default is 3306sql_query_pre = SET NAMES utf8sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documentssql_attr_uint = group_idsql_attr_timestamp = date_addedsql_query_info = SELECT * FROM documents WHERE id=$idindex documentssource = documents#path 索引记录存放目录,如 d:/sphinx/data/cgfinal ,实际存放时会存放在 d:/sphinx/data 目录,然后创建多个 cgfinal 名称,不同扩展名的索引文件。path = d:/www/sphinx/data/docdocinfo = externenable_star = 1min_word_len = 3min_prefix_len = 0min_infix_len = 3charset_type = sbcs# 其他的配置如 min_word_len, charset_type, charset_table, ngrams_chars, ngram_len 这些则是支持中文检索需要设置的内容。# 如果检索的不是中文,则 charset_table, ngrams_chars, min_word_len 就要设置不同的内容,具体官方网站的论坛中有很多,大家可以去搜索看看。# mem_limit 索引使用内存最大限制,根据机器情况而定,默认是32M,太小的会影响索引的性能。indexermem_limit = 32M# 搜索的守护进程配置# 在进行全文检索过程中,searchd要先开启,mysql在全文检索时才能连接到sphinx,由sphinx进行全文检索,再将结果返回给mysql# address 侦听请求的地址,不设置则侦听所有地址# port 侦听端口searchdport = 3312log =d:/www/sphinx/logs/searched_doc.logquery_log = d:/www/sphinx/logs/query_doc.logread_timeout = 5max_children = 30pid_file = d:/www/sphinx/logs/searched-doc.pidmax_matches = 1000seamless_rotate = 0preopen_indexes = 0unlink_old = 1为了测试,我们的 Sphinx 配置文件已经写好,确保我们的 Mysql 数据库已经启动,如果没有启动则在 cmd 中键入 net start mysql 接下来,我们的测试正式开始:1,生成数据索引或重建索引:(最好再复制一个 sphinx-doc.conf 配置文件,并把它放入 bin 文件夹中,下面的举例 假设我们已经这样做):在 cmd 模式下:输入:d:/www/sphinx/bin/indexer.exe -config d:/www/sphinx/bin/sphinx-doc.conf documents2,运行检索守护进程 searchd.exe: d:/www/sphinx/bin/searchd.exe -config d:/www/sphinx/bin/sphinx-doc.conf如过这两步没有报错的话,说明我们的 Sphinx 已经正常运行了!可以通过 netstat -an 查看是否 3312 端口是否处如监听状态。3,现在来用 sphinx 自带的工具 search.exe 来测试一下:测试:索引关键字: this is mD:wwwsphinxbinsearch.exe -c d:/www/sphinx/bin/sphinx-doc.conf this is m结果:Sphinx 0.9.8-release (r1371)Copyright (c) 2001-2008, Andrew Aksyonoffusing config file d:/www/sphinx/bin/sphinx-doc.conf.WARNING: index documents: invalid morphology option extern - IGNOREDindex documents: query this is m : returned 4 matches of 4 total in 0.000 scdisplaying matches:1. document=1, weight=1, group_id=1, date_added=Sat Sep 13 21:37:47 2008 id=1 group_id=1 group_id2=5 date_added=2008-09-13 21:37:47 title=test one content=this is my test document number one. also checking search withiphrases.2. document=2, weight=1, group_id=1, date_added=Sat Sep 13 21:37:47 2008 id=2 group_id=1 group_id2=6 date_added=2008-

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论