Sphinx与Coreseek中文全文检索
2013-11-19 17:57 hduhans 阅读(366) 评论(0) 收藏 举报Sphinx是一个基于SQL的全文检索引擎,可以结合MySQL,PostgreSQL做全文搜索,它可以提供比数据库本身更专业的搜索功能,使得应用程序更容易实现专业化的全文检索。
shinx优点:1) 高速索引。建立索引速度可达10M/s;
2) 高速搜索。2-4G的文本量平均查询速度不到0.1秒;
3) 高可用性。单CPU最大支持100GB文本,100M文档;
一、sphinx配置及使用
1、下载
1) 官方:http://sphinxsearch.com/downloads/release/
2) 网盘:sphinx-2.1.3-release-win32.zip sphinx-2.1.3-release-win64.zip
2、配置步骤(本实例配置环境:Win7系统64位CPU)
1) 下载后解压放在D:\sphinx目录下;
2) 在D:\sphinx目录下新建索引文件夹data和日志文件夹log,复制D:\sphinx\sphinx.conf.in到D:\sphinx\bin\sphinx.conf(注意修改文件名);
3) 修改配置文件D:\sphinx\bin\sphinx.conf。参考配置sphinx.conf
配置说明如下:
主数据源 source src1{}
增量数据源 source src1throttled:src1{}
主数据索引 index test1{}
增量数据索引 index test1stemmed:test1{}
分布式索引 index dist1{}
实时索引 inedx rt{}
索引器 indexer{}
服务进程 searchd{}
主要修改如下:
source src1 #主数据源 { ... ql_host = localhost # 数据库服务器 sql_user = root # 数据库用户名 sql_pass = '' # 数据库密码 sql_db = test # 数据库 sql_port = 3306 # 数据库端口 sql_query_pre = SET NAMES utf8 # 去掉此行前面的注释,如果你的数据库是uft8编码的,两个sql_query_pre都打开 ... sql_query = select id,title,content from post #查询数据sql ... # sql_attr_uint #关闭 ... # sql_attr_timestamp #关闭 ... sql_query_info = SELECT * FROM post WHERE id=$id #修改表名 ... } #source src1throttled : src1 # 关闭增量数据源 #{ ... #} index test1 # 主数据索引 { ... source = src1 #这里与主数据源名一致 ... path = D:/sphinx/data/ #索引存放目录 ... charset_type = utf-8 #编码 ... charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F #打开utf8的编码 ... } #index test1stemmed : test1 #关闭增量数据索引 #{ ... #} #index dist1 # 关闭分布式索引 #{ ... #} #index rt # 关闭实时索引 #{ ... #} indexer # 索引器 { mem_limit = 256M #这里可以根据自己的需要及主机内容大小修改 }
4) 导入数据
DROP TABLE IF EXISTS `post`; CREATE TABLE `post` ( `id` int(11) NOT NULL auto_increment, `title` varchar(200) default NULL, `content` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of post -- ---------------------------- INSERT INTO `post` VALUES ('1', 'test', '我是一名学生,正在学习php程序开发和信息审计'); INSERT INTO `post` VALUES ('2', 'java', 'java and php is very good!'); INSERT INTO `post` VALUES ('14', 'sphinx', 'sphinx is bery good for search!!!');
5) 进入bin建立索引
命令:indexer.exe --all

6) 搜索
命令:search.exe java

7) 调用程序api。在api文件夹中有php、python和java等调用api。
a、开启searchd进程。
命令:searchd.exe

b、程序调用,拷贝sphinxapi.php至程序根目录。
① index.php
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
<h1>简单php搜索测试</h1>
<form action="find.php" method="post">
请输入搜索关键字:<input type="text" name="word"><br>
<input type="submit" name="submit"><br>
</form>
</body>
</html>
② find.php
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Find</title>
</head>
<body>
<h1 id='top'>查询输出页面</h1>
<?php
require 'sphinxapi.php';
$keyword=$_POST['word'];
echo "receive key word: ".$keyword."<br/>";
$sphinx=new SphinxClient();
$sphinx->SetServer("127.0.0.1",9312);
$sphinx->SetMatchMode(SPH_MATCH_ANY); //两种主要模式 SPH_MATCH_ANY 和 SPH_MATCH_ALL
$sphinx->setLimits(0,100);
$result=$sphinx->query("$keyword","*");
if(count($result['matches'])==0){
echo "<h2>找不到数据</h2>";
return;
}
echo "*******************************************************************************************<br>";
echo "<b style='font-size:20px;'>查询时间:</b>".$result['time']."<br>";
echo "<b style='font-size:20px;'>共搜到数据:</b>".$result['total_found']."<br>";
echo "<b style='font-size:20px;'>显示数据:</b>".count($result['matches'])."<br>";
echo "*******************************************************************************************<br>";
$ids=join(",",array_keys($result['matches']));
mysql_connect("127.0.0.1","root","123456");
mysql_select_db("test");
mysql_query("set names utf8");
$sql="select * from FS_Company where id in({$ids})";
$rst=mysql_query($sql);
$opts=array("before_match"=>"<font color=red>","after_match"=>"</font>");
$index=1;
$title=array();
$content=array();
while($row=mysql_fetch_assoc($rst)){
$rst2=$sphinx->buildExcerpts($row,"mysql",$keyword,$opts);
echo "<pre>";
echo "<b style='font-size:20px;'>第{$index}篇帖子</b><br>";
//echo "编号:".$row['id']."<br>";
//echo "标题:".$row['title']."<br>";
//echo "内容:".$row['content']."<br>";
print_r($rst2);
echo "<hr></pre>";
$index++;
}
?>
</body>
</html>
二、coreseek中文检索配置及使用
1、下载
1) 官方:http://www.coreseek.cn/news/7/52/
2) 网盘:coreseek-3.2.14-win32.zip
2、使用与sphinx类似,详细见文档;
浙公网安备 33010602011771号