php sphinx中文检索实例

来源:互联网 发布:家有n多宝贝 淘宝 编辑:程序博客网 时间:2024/04/30 06:53

首先你的web服务器必须安装支持中文检索的sphinx服务 (我的blog有详细的讲解)

其次要准备要测试的数据 提供数据如下:

DROP TABLE IF EXISTS `post`;CREATE TABLE `post` (  `id` int(10) unsigned NOT NULL auto_increment,  `title` varchar(200) NOT NULL,  `content` text NOT NULL,  PRIMARY KEY  (`id`)) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COMMENT='sphinx测试表';INSERT INTO `post` VALUES (1,'Linux','Linux is centos 6.4 , this is for lamp '),(2,'php ','php is web sricpt for user ,this is smple script, server sricpt.'),(3,'mysql','mysql is database .this is test for sphinx server.'),(4,'我的中文检索','我在这里测试sphinx的中文检索,也是coreseek的中文索引。'),(5,' JavaScript语言基础4','cript 中使用变量的好处:变量保存在计算机的内存中,变量非常适合于保存临时性的数据 ,变量只具有有限的生存期,当\r\n用户关闭了页面或者打开一个新的页面,变量就会被释放。\r\n  在JavaScript 代码中,变量名是大小写敏感的,另外,某些特定的单词 或字符不能作为变量名,这些特定的单词就是JavaScript 中\r\n的保留字,例如:var、 with等,某些特殊字符'),(6,'我的Linux文章的列表','我进行许多的Linux文章在csnd博客中 其中包括 sphinx php MySQL redis 数据库的笔记'),(7,'如何为你的项目挑选适宜的Web开发框架?','我进行许多的Linux文章在csnd博客中 其中包括 sphinx php MySQL redis 数据库的笔记\r\n古语云工欲善其事,必先利其器,对于工程师而言,选择一款合适而强大的开发框架对开发工作是大有裨益的。那么我们该从什么角度来进行衡量和挑选呢?\r\n\r\n首先对于一家创业公司而言,这更像是一个商业抉择而不仅仅是技术选择,时间人力物力等开销都必须要考虑周全。一旦公司规模上来了资金不成问题了,可选择的空间就更大了甚至可以进行迁移或重'),(8,'文章','只是一片测试文章 关于sphinx'),(10,'新增测试数据','这是新增的测试增量数据'),(11,'这是测试数据','这是测试数据这是测试数据这是测试数据这是测试数据Linux php Java'),(12,'特殊的数据索引','特殊的数据索引特殊的数据索引特殊的数据索引特殊的数据索引特殊的数据索引特殊的数据索引特殊的数据索引特殊的数据索引特殊的数据索引特殊的数据索引特殊的数据索引特殊的数据索引特殊的数据索引'),(13,'my title','my content');DROP TABLE IF EXISTS `sph_coner`;CREATE TABLE `sph_coner` (  `s_id` int(10) unsigned NOT NULL auto_increment,  `p_id` int(11) NOT NULL,  PRIMARY KEY  (`s_id`)) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
sphinx中文检索测试:

<html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><head></head><body><table width='800' align="center" border="1"   >        <tr><td colspan="3" align="center" ><h2>php sphinx 检索</h2><br /><form action="find.php" method="post" ><input type="text" name="sphinxwprd" value="" /> <input type="submit" name="sub" value="搜 索" />   </form></td></tr><tr width='800'><td colspan="3" align="center">检索结果</td></tr><tr><td align="center">文章id</td><td align="center">标题</td><td align="center">内容</td></tr><?php if(isset($_POST['sub'])){$keyword = $_POST['sphinxwprd'];$sphinx = new SphinxClient();$sphinx->SetServer("192.168.1.124", 9312);$sphinx->SetMatchMode(SPH_MATCH_ANY);$sr = $sphinx->query($keyword,"*");$whereid = join(',',array_keys($sr['matches']));$mysqli = new mysqli('localhost', 'root', 'root', 'test');$sql = "select * from post where id in (".$whereid.")";$mysqli->query("set names utf8");$opt = array();$reult = $mysqli->query($sql);while ($row = $reult->fetch_assoc()) {//var_dump($row);$opt = array("before_match"=>"<font style='color:red'>","after_match"=>"</font>");$r2 = $sphinx->buildExcerpts($row,'main',$keyword,$opt);//var_dump($r2);echo "<tr><td>".$r2[0]."</td><td>".$r2[1]."</td><td>".$r2[2]."</td></tr>";}//foreach($r)}?>         <tr>      <td colspan="3" style="color:red"><strong>根据下面的文章进行检索测试 sphinx</strong></td></tr>        <tr><td colspan="3" ><img src="jiansuo.jpg" /></td></tr></table></body></html>

测试的结果:



0 0