php使用coreseek进行中文分词搜索

方法一

使用coreseek源码自带testpack/api/test_coreseek.php代码,
进行稍微修改就可以使用了,只不过需要引入”spinxapi.php“类

方法二--制作php扩展

1.安装sphinx扩展

1 下载依赖 http://pecl.php.net/package/sphinx 
2 解压并执行以下代码:
3 /usr/local/php7/bin/phpize
4 ./configure --prefix=/usr/local/libsphinxclient
5 make && make install

2.安装sphinx扩展(这里需要根据php版本进行分别安装)

php5版本

 1 wget http://pecl.php.net/get/sphinx-1.3.3.tgz 
 2 tar zxvf sphinx-1.3.3.tgz 
 3 cd sphinx-1.3.3
 4  ./configure 
 5  --with-php-config=/usr/local/php/bin/php-config 
 6  --with-sphinx=/usr/local/libsphinxclient
7 上面有可能会报错: 8 php_sphinx_client_handlers.read_property = php_sphinx_client_read_property; ^ 9 make: *** [sphinx.lo] Error 1 10 这个报错,修改 sphinx.c 第105行为: 11 retval = std_hnd->read_property(object, member, type TSRMLS_CC, NULL); 12 然后编译即可通过

php7版本

 1 下载地址:
 2 http://git.php.net/?p=pecl/search_engine/sphinx.git;a=snapshot;h=339e123acb0ce7beb2d9d4f9094d6f8bcf15fb54;sf=tgz ---可能下载下来为index.html文件,那就下载到本地然后rz上去。
 3 
 4 接下来接着安装:
 5 
 6 这里现在的版本为:sphinx-339e123.tar.gz
 8 tar -zxf sphinx-339e123.tar.gz
10 cd sphinx-339e123
12 ./configure 
13 --prefix=/usr/local/sphinxextend 
14 --with-php-config=/usr/local/php-fpm/bin/php-config 
15 --with-sphinx=/usr/local/libsphinxclient/
16 
17 make && make install

以上两个版本的步骤执行完成后,找到php.ini,在里面添加extension=spninx.so

然后使用php-m 或者看下phpinfo();
能看到sphinx即可。

注:执行php脚本之前要先启动sphinx服务,在shpinx安装目录的bin目录下执行./searchd 启动sphinx服务

例子:

 1 $cl = new SphinxClient ();
 2 $cl->SetServer ( '127.0.0.1', 9312);
 3 $cl->SetConnectTimeout ( 3 );
 4 $cl->SetArrayResult ( true );
 5 $cl->SetMatchMode ( SPH_MATCH_ANY);
 6 $res = $cl->Query ( '祖国', "*" );
 7 
 8 $ids = '';
 9 foreach ($res['matches'] as $key => $val) {
10 $ids .= $val['id'].',';
11 }
12 $ids = rtrim($ids,',');
13 
14 $conn = mysqli_connect("localhost","root","root","post");
15 mysqli_query($conn,"set names utf8");
16 $sql = "select * from post_article where id in($ids)";
17 $res = mysqli_query($conn,$sql);
18 $list = array();
19 while($row = mysqli_fetch_assoc($res)){
20 
21 $list[] = $row;
22 
23 }
24 
25 mysqli_free_result($res);
26 
27 mysqli_close($conn);
28 
29 foreach($list as $v){
30 
31 echo $v['title'].'<br/>'.$v['content'].'<hr>';
32 
33 }
原文地址:https://www.cnblogs.com/baikaishui-liang/p/10156131.html