PHP7的Sphinx扩展安装及使⽤⽅法
安装Sphinx扩展
# wget git.php/?p=pecl/search_engine/sphinx.git;a=snapshot;h=9a3d08c67af0cad216aa0d38d39be71362667738;sf=tgz
# tar zxvf
# cd sphinx-9a3d08c
# phpize
# ./configure
# make && make install
./configure 的时候可能出错,提⽰
checking for libsphinxclient headers in not found configure: error: Cannot find libsphinxclient headers ,解决⽅法如下:
进⼊coreseek-3.2.14/csft-3.2.14/api/libsphinxclient⽬录,编译安装libsphinxclient即可
# ./configure
# make && make install
然后再次编译安装sphinx扩展即可,安装完成后修改php.ini,在末尾添加如下内容
extension = sphinx.so
然后重启php-fpm即可,执⾏php -m,看到有sphinx扩展说明安装成功
PHP使⽤sphinx
<?php
$sphinx = new SphinxClient;
$sphinx->setServer("localhost", 9312);
$sphinx->setMatchMode(SPH_MATCH_ANY);  //匹配模式 ANY为关键词⾃动拆词,ALL为不拆词匹配(完全匹配)
$sphinx->SetArrayResult ( true ); //返回的结果集为数组
$result = $sphinx->query("test","*"); //星号为所有索引源
$count=$result['total'];  //查到的结果条数
$time=$result['time'];  //耗时
$arr=$result['matches'];  //结果集
php8兼容php7吗
$id='';
for($i=0;$i<$count;$i++){
$id.=$arr[$i]['id'].',';
}
$id=substr($id,0,-1);  //结果集的id字符串
注:执⾏php脚本之前要先启动sphinx服务,在shpinx安装⽬录的bin⽬录下执⾏./searchd 启动sphinx服务

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。