linux安装sphinx全文搜索,并php使用案例

8 篇文章 0 订阅
3 篇文章 0 订阅

请先装好lnmp环境,如没有装,参考

https://blog.csdn.net/hgb24660/article/details/108938963

1,去官网下载包

http://sphinxsearch.com/

这里我通过 wget 方式下载软件包,命令如下:

wget http://sphinxsearch.com/files/sphinx-2.1.6-release.tar.gz

2.加压该压缩包,命令如下:

tar zxvf sphinx-2.1.6-release.tar.gz

源码包方式安装,如果没有安装其他支持的相关的类库文件,所以先安装其他类库包文件,如果以前安装过相关的类库文,跳过这步

yum -y install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel expat-devel

3.cd进入目录执行

./configure --prefix=/usr/local/sphinx

4.使用 make && make install编译

make && make install

5.编辑配置文件

cd /usr/local/sphinx/etc/

复制默认配置文件,重新创建一个配置文件,sphinx.conf.dist 完整版默认配置一,有很多内容,在这里选择复制的是 sphinx-min.conf.dist 迷你版

cp /usr/local/sphinx/etc/sphinx-min.conf.dist /usr/local/sphinx/etc/sphinx.conf
vim sphinx.con

先了解一配置下几个概念:

-source:数据源,数据是从什么地方来的
-index:索引,当有数据源之后,从数据源处构建索引,索引实际上就是相当于一个字典检索。有了整本字典内容以后,才会有字典检索。
-searchd:提供搜索查询服务,启动 sphinx 服务一般使用 /usr/local/sphinx/bin/searchd -c /usr/local/etc/sphinx.conf
-indexer:构建索引,当需要构建索引的时候就调用indexer这个命令:/usr/local/bin/indexer -c /usr/local/etc/sphinx.conf --all --rotate
-attr:属性,属性是存在索引中的,它不进行全文索引,但是可以进行过滤和排序

配置数据库
在这里插入图片描述

6.把 /usr/local/sphinx/etc/ 目录下的 example.sql 导入数据库中(用来全文搜索的测试数据,使用其它表也可以,但要有数据)

进入mysql
mysql -u xxx -p
show databases;		//查看数据库
create database test;	//创建数据库
source /usr/local/sphinx/etc/example.sql  //导入sql文件

在这里插入图片描述

退出mysql
6.5 修改mysql.ini配置文件socket
将/tmp下的mysql.sock移动到/var/lib/mysql/下
在这里插入图片描述

mkdir /var/lib/mysql
chmod 777 /var/lib/mysql
cd /tmp/
mv mysql.sock /var/lib/mysql/

并修改 socket,重启MySQL服务
在这里插入图片描述

8.启动 sphinx 服务

/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf

在这里插入图片描述

7.建立 sphinx 的索引文件

/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf --all --rotate
如果创建的索引文件比较多而又不需要全部重新生成索引,可以单独生成
/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf test1
/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf testrt

如果出现
WARNING: failed to open pid_file ‘/usr/local/sphinx/var/log/searchd.pid’.
WARNING: indices NOT rotated.

FATAL: stop: pid file ‘/usr/local/sphinx/var/log/searchd.pid’ does not exist or is not readable
解决:
启动 sphinx 服务 在进行操作
在这里插入图片描述
请使用启动 sphinx 服务,让后再次执行

如需停止服务使用:

/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf --stop

linux安装sphinx完成。

php使用sphinx案例

1.PHP 安装 sphinx 扩展模块

  • 需要 libsphinxclient支持

libsphinxclient 文件存在于解压后的源码文件的 api 文件夹内

cd sphinx-2.1.6-release/api/libsphinxclient/
./configure -prefix=/usr/local/sphinx/
make && make install

下载 sphinx扩展 , 下载地址:https://pecl.php.net/package/sphinx
在这里插入图片描述
在这里插入图片描述

解压,进入目录执行:

phpize
./configure --with-php-config=/www/server/php/56/bin/php-config --with-sphinx=/usr/local/sphinx/
make && make install

注:

–with-php-config=/www/server/php/56/bin/php-config :是你安装php环境文件目录
最后会出现sphinx扩展:
在这里插入图片描述
然后配置 php.ini 文件,加载 sphinx 扩展
在php.ini里添加

[sphinx]
extension=/www/server/php/56/lib/php/extensions/no-debug-non-zts-20131226/sphinx.so

在这里插入图片描述

重启服务 ,查看 phpinfo,验证 sphinx 是否成功安装
在这里插入图片描述

2.php 代码测试 sphinx

<?php
    $sphinx = new SphinxClient;
    $sphinx->setServer("127.0.0.1", 9312);
    $sphinx->setMatchMode(SPH_MATCH_ANY);   //匹配模式 ANY为关键词自动拆词,ALL为不拆词匹配(完全匹配)
    // $sphinx->SetArrayResult ( true );    //返回的结果集为数组
    $keyword = 'is';
    // $result = $sphinx->query($keyword,"*");     //星号为所有索引源
    $result = $sphinx->query($keyword,"test1");     
    $err = $sphinx->GetLastError();                 //错误信息
    if(!empty($err)) {
        print_r($result);
    }
    //使用mysql
    $ids = implode(',',array_keys($result['matches']));
    $conn = mysqli_connect('127.0.0.1','root','root');
    mysqli_query($conn,'set names utf8');
    mysqli_select_db($conn,'test');
    
    $sql = "select * from documents where id in (".$ids.")";
    
    $rst = mysqli_query($conn,$sql);
    
    //给匹配关键字添加样式
    $opts = array(
        'before_match'=>'<font style="font-weight:bold;color:#f00;">',
        'after_match'=>'</font>'
    );
    echo '<pre>';
    while($row = mysqli_fetch_assoc($rst)){
        $row2 = $sphinx->buildExcerpts($row,'test1',$keyword,$opts);//test1 配置文件中的主数据源索引
        print_r($row2);  
    }
    echo '</pre>';

结果:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值