linux install sphinx full-text search, and php use case

Please install the lnmp environment first, if not installed, please refer to

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

1. Go to the official website to download the package

http://sphinxsearch.com/

Here I download the software package through wget, the command is as follows:

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

2. Pressurize the compressed package, the command is as follows:

tar zxvf sphinx-2.1.6-release.tar.gz

Source code package installation. If other supported related class library files are not installed, install other class library package files first. If you have installed related class library files before, skip this step

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

3.cd into the directory to execute

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

4. Use make && make install to compile

make && make install

5. Edit the configuration file

cd /usr/local/sphinx/etc/

Copy the default configuration file and recreate a configuration file. The default configuration of the full version of sphinx.conf.dist has a lot of content. Here I choose to copy the mini version of sphinx-min.conf.dist

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

First understand the following concepts in a configuration:

-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:属性,属性是存在索引中的,它不进行全文索引,但是可以进行过滤和排序

Configuration database
Insert picture description here

6. Import example.sql in the /usr/local/sphinx/etc/ directory into the database (test data used for full-text search, other tables can be used, but data is required)

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

Insert picture description here

Exit mysql
6.5 Modify mysql.ini configuration file socket
to move mysql.sock under /tmp to /var/lib/mysql/
Insert picture description here

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

And modify the socket, restart the MySQL service
Insert picture description here

8. Start the sphinx service

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

Insert picture description here

7. Create sphinx index file

/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

If
WARNING: failed to open pid_file '/usr/local/sphinx/var/log/ searchd.pid'.
WARNING: indices NOT rotated.
or
FATAL: stop: pid file'/usr/local/sphinx/var/log/ searchd.pid' does not exist or is not readable
solution:
start sphinx service is in operation
Insert picture description here
, please use start sphinx service, and then execute it again

To stop using the service:

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

The linux installation of sphinx is complete.

php use sphinx case

1. PHP install sphinx extension module

  • Requires libsphinxclient support

The libsphinxclient file exists in the api folder of the decompressed source file

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

Download sphinx extension, download link: https://pecl.php.net/package/sphinx
Insert picture description here
Insert picture description here

Unzip and enter the directory to execute:

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

Note:

--With-php-config=/www/server/php/56/bin/php-config: is the directory where you install the php environment file.
Finally, the sphinx extension will appear:
Insert picture description here
then configure the php.ini file, load the sphinx extension
in php.ini Add to

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

Insert picture description here

Restart the service, check phpinfo, verify whether sphinx is successfully installed
Insert picture description here

2.php code test 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>';

result:
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/hgb24660/article/details/108941506