Home > Backend Development > PHP Tutorial > Laravel xunsearch full text search

Laravel xunsearch full text search

高洛峰
Release: 2023-03-05 15:50:01
Original
2780 people have browsed it

Before starting, it is highly recommended to browse the official documentation of xunsearch

Introduction to Xunsearch

Xunsearch is a high-performance, full-featured full-text search solution.
Xunsearch is designed to help general developers quickly and easily build their own full-text search engines for existing massive data.

Click here for details

Test environment:

Linux server 2:

##CodenameFunctionIPSystem##Server AServer Bxunsearch backend server configuration:
Provides web services 192.168.56.2 centos7
Provides xunsearch back-end service 192.168.56.3 centos7

PHP version 7.0.13 does not have a cache installed. It is recommended to install a cache in the production environment

PHP 7.0.13 (cli) (built: Dec 21 2016 13:53:31) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Xdebug v2.5.0, Copyright (c) 2002-2016, by Derick Rethans
Copy after login
Copy after login

Connect to server B. Run the following instructions to install and decompress the xunsearch installation package.

wget //m.sbmmt.com/
tar -xjf xunsearch-full-latest.tar.bz2
Copy after login
Copy after login

Execute the installation script and follow the prompts. The main thing is to enter the installation directory of the xunsearch software package. It is strongly recommended to plan a separate directory instead of mixing it. to other software directories.

cd xunsearch-full-1.4.10/
sh setup.sh
Copy after login
Copy after login

The installation interactive interface will be displayed. Then press Enter directly to install to the default path.

Then drink a cup of coffee and wait for a while. The following installation success prompt will appear.

+==========================================+
| Welcome to setup xunsearch(full)         |
| 欢迎使用 xunsearch (完整版) 安装程序       |
+------------------------------------------+
| Follow the on-screen instructions please |
| 请按照屏幕上的提示操作以完成安装            |
+==========================================+

Please specify the installation directory
请指定安装目录 (默认为中括号内的值)
[/usr/local/xunsearch]:

Confirm the installation directory
请确认安装目录:/usr/local/xunsearch [Y/n]Y
Checking scws ... no
Installing scws (1.2.3) ... 
Extracting scws package ...
Configuring scws ...
Compiling & installing scws ...
Checking scws dict ... no
Extracting scws dict file ... 
Checking libuuid ... no, try to install it
Extracting libuuid package ...
Configuring libuuid ...
Compiling & installing libuuid ...
Checking xapian-core-scws ... no
Installing xapian-core-scws (1.2.22) ... 
Extracting xapian-core-scws package ...
Configuring xapian-core-scws ...
Compiling & installing xapian-core-scws ...
Checking libevent ... no
Installing libevent (2.0.21-stable) ... 
Extracting libevent package ...
Configuring libevent ...
Compiling & installing libevent ...
Extracting xunsearch package (1.4.10) ...
Configuring xunsearch ...
Compiling & installing xunsearch ...
Cleaning ... done

+=================================================+
| Installation completed successfully, Thanks you |
| 安装成功,感谢选择和使用 xunsearch                |
+-------------------------------------------------+
| 说明和注意事项:                                  |
| 1. 开启/重新开启 xunsearch 服务程序,命令如下:    |
|    /usr/local/xunsearch/bin/xs-ctl.sh restart   |
|    强烈建议将此命令写入服务器开机脚本中             |
|                                                 |
| 2. 所有的索引数据将被保存在下面这个目录中:         |
|    /usr/local/xunsearch/data                    |
|    如需要转移到其它目录,请使用软链接。             |
|                                                 |
| 3. 您现在就可以在我们提供的开发包(SDK)基础上       |
|    开发您自己的搜索了。                           |
|    目前只支持 PHP 语言,参见下面文档:             |
|    /usr/local/xunsearch/sdk/php/README          |
+=================================================+
Copy after login
Copy after login

Note: If it is a production environment, iptables may need to be configured strictly. This article only configures iptables for testing

. If you have not enabled it, ignore this operation

vi /etc/sysconfig/iptables

加入, 下面两行

-A INPUT -p tcp -m state --state NEW -m tcp --dport 8383 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8384 -j ACCEPT

重启iptables
service iptables restart
Copy after login
Copy after login

Start service

/usr/local/xunsearch/bin/xs-ctl.sh -b inet start
Copy after login
Copy after login

Startup output content:

INFO: starting server[xs-indexd] ... (BIND:8383)
INFO: starting server[xs-searchd] ... (BIND:8384)
Copy after login
Copy after login

Web server configuration

Install xunsearch PHP SDK

cd project/root/path
composer require --prefer-dist hightman/xunsearch
Copy after login
Copy after login

Create project configuration file, a The project corresponds to a configuration file, and the project name cannot be repeated

cp app\vendor\hightman\xunsearch\app\demo.ini app\config\search-demo.ini
Copy after login
Copy after login

Modify the content of the file just created, where the IP addresses of server.index and server.search are the IP addresses of the server where the xunsearch service is installed

;搜索配置文件示例
project.name = demo
project.default_charset = utf-8
server.index = 192.168.56.4:8383
server.search = 192.168.56.4:8384

[pid]
type = id

[subject]
type = title

[message]
type = body

[chrono]
type = numeric
Copy after login
Copy after login

Modify the app/routes/web.php file and write some client search test code

###################### 测试模块 ####################
/*
 * 全文搜索测试部分
 */
Route::get('/search/{key}', function ($key){
    $xs = new XS(config_path('search-demo.ini'));
    $search = $xs->search; // 获取 搜索对象
    $query = $key;
    $search->setQuery($query)
        ->setSort('chrono', true) //按照chrono 正序排列
        ->setLimit(20,0) // 设置搜索语句, 分页, 偏移量
    ;

    $docs = $search->search(); // 执行搜索,将搜索结果文档保存在 $docs 数组中
    $count = $search->count(); // 获取搜索结果的匹配总数估算值
    foreach ($docs as $doc){
        $subject = $search->highlight($doc->subject); // 高亮处理 subject 字段
        $message = $search->highlight($doc->message); // 高亮处理 message 字段
        echo $doc->rank() . '. ' . $subject . " [" . $doc->percent() . "%] - ";
        echo date("Y-m-d", $doc->chrono) . "<br>" . $message . "<br>";
        echo '<br>========<br>';
    }
    echo  '总数:'. $count;
});

Route::get('/makedoc/{title}/{message}', function ($title, $message){
    $xs = new XS('demo');
    $doc = new XSDocument;
    $doc->setFields([
        'pid' => 1,
        'subject' => $title,
        'message' => $message,
        'chrono' => time(),
    ]); // 用数组进行批量赋值
    $xs->index->add($doc);
});
Copy after login
Copy after login

Use a browser to access the above two routes to test the function

Access:

http://

Your domain name/makedoc/title/content Test generated indexTest example:

http://你的域名/makedoc/关于 xunsearch 的 DEMO 项目测试/项目测试是一个很有意思的行为!

http://你的域名/makedoc/测试第二篇/这里是第二篇文章的内容

http://你的域名/makedoc/项目测试第三篇/俗话说,无三不成礼,所以就有了第三篇
Copy after login
Copy after login

Note: The index does not take effect immediately and the delay is about 10 seconds About

Test the search function by visiting:

http://

your domain name/search/keywordsuch as search keyworditem
<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">1. 关于 xunsearch 的 DEMO 项目测试 [99%] - 2011-08-26 项目测试是一个很有意思的行为! ======== 2. 项目测试第三篇 [100%] - 2011-08-26 俗话说,无三不成礼,所以就有了第三篇 ======== 总数:2</pre><div class="contentsignin">Copy after login</div></div><div class="contentsignin">Copy after login</div></div>Through the above steps, we have completed the simplest full-text search example.

If you want to know more details, please read the official documentation of xunsearch.


Reference content

    xunsearch official documentation


It is highly recommended to browse the official documentation of xunsearch before starting

Xunsearch Introduction

Xunsearch is a high-performance, full-featured full-text search solution.
Xunsearch is designed to help general developers quickly and easily build their own full-text search engines for existing massive data.


Click here for details

Test environment:

Linux server 2:

##CodenameFunctionIPSystem##Server AProvides web servicesProvides xunsearch back-end service
192.168.56.2 centos7 Server B
192.168.56.3 centos7

xunsearch back-end server configuration:

PHP version 7.0.13 does not have a cache installed. It is recommended to install a cache in the production environment

PHP 7.0.13 (cli) (built: Dec 21 2016 13:53:31) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Xdebug v2.5.0, Copyright (c) 2002-2016, by Derick Rethans
Copy after login
Copy after login

Connect to server B and run the following instructions to install and unzip the xunsearch installation package

wget //m.sbmmt.com/
tar -xjf xunsearch-full-latest.tar.bz2
Copy after login
Copy after login

Execute the installation script and follow the prompts. Mainly enter the installation directory of the xunsearch software package. It is strongly recommended to plan a separate directory instead of mixing it into other software directories.

cd xunsearch-full-1.4.10/
sh setup.sh
Copy after login
Copy after login

The installation interactive interface will be displayed. Then press Enter directly to install to the default path.
Then drink a cup of coffee and wait for a while. The following installation success prompt will appear.

+==========================================+
| Welcome to setup xunsearch(full)         |
| 欢迎使用 xunsearch (完整版) 安装程序       |
+------------------------------------------+
| Follow the on-screen instructions please |
| 请按照屏幕上的提示操作以完成安装            |
+==========================================+

Please specify the installation directory
请指定安装目录 (默认为中括号内的值)
[/usr/local/xunsearch]:

Confirm the installation directory
请确认安装目录:/usr/local/xunsearch [Y/n]Y
Checking scws ... no
Installing scws (1.2.3) ... 
Extracting scws package ...
Configuring scws ...
Compiling & installing scws ...
Checking scws dict ... no
Extracting scws dict file ... 
Checking libuuid ... no, try to install it
Extracting libuuid package ...
Configuring libuuid ...
Compiling & installing libuuid ...
Checking xapian-core-scws ... no
Installing xapian-core-scws (1.2.22) ... 
Extracting xapian-core-scws package ...
Configuring xapian-core-scws ...
Compiling & installing xapian-core-scws ...
Checking libevent ... no
Installing libevent (2.0.21-stable) ... 
Extracting libevent package ...
Configuring libevent ...
Compiling & installing libevent ...
Extracting xunsearch package (1.4.10) ...
Configuring xunsearch ...
Compiling & installing xunsearch ...
Cleaning ... done

+=================================================+
| Installation completed successfully, Thanks you |
| 安装成功,感谢选择和使用 xunsearch                |
+-------------------------------------------------+
| 说明和注意事项:                                  |
| 1. 开启/重新开启 xunsearch 服务程序,命令如下:    |
|    /usr/local/xunsearch/bin/xs-ctl.sh restart   |
|    强烈建议将此命令写入服务器开机脚本中             |
|                                                 |
| 2. 所有的索引数据将被保存在下面这个目录中:         |
|    /usr/local/xunsearch/data                    |
|    如需要转移到其它目录,请使用软链接。             |
|                                                 |
| 3. 您现在就可以在我们提供的开发包(SDK)基础上       |
|    开发您自己的搜索了。                           |
|    目前只支持 PHP 语言,参见下面文档:             |
|    /usr/local/xunsearch/sdk/php/README          |
+=================================================+
Copy after login
Copy after login

Note: If it is a production environment, iptables may need to be configured strictly. This article only configures iptables for testing

. If you have not enabled it, ignore this operation

vi /etc/sysconfig/iptables

加入, 下面两行

-A INPUT -p tcp -m state --state NEW -m tcp --dport 8383 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8384 -j ACCEPT

重启iptables
service iptables restart
Copy after login
Copy after login

Start service

/usr/local/xunsearch/bin/xs-ctl.sh -b inet start
Copy after login
Copy after login

Startup output content:

INFO: starting server[xs-indexd] ... (BIND:8383)
INFO: starting server[xs-searchd] ... (BIND:8384)
Copy after login
Copy after login

Web server configuration

Install xunsearch PHP SDK

cd project/root/path
composer require --prefer-dist hightman/xunsearch
Copy after login
Copy after login

Create project configuration file, a The project corresponds to a configuration file, and the project name cannot be repeated

cp app\vendor\hightman\xunsearch\app\demo.ini app\config\search-demo.ini
Copy after login
Copy after login

Modify the content of the file just created, where the IP addresses of server.index and server.search are the IP addresses of the server where the xunsearch service is installed

;搜索配置文件示例
project.name = demo
project.default_charset = utf-8
server.index = 192.168.56.4:8383
server.search = 192.168.56.4:8384

[pid]
type = id

[subject]
type = title

[message]
type = body

[chrono]
type = numeric
Copy after login
Copy after login

Modify the app/routes/web.php file and write some client search test code

###################### 测试模块 ####################
/*
 * 全文搜索测试部分
 */
Route::get('/search/{key}', function ($key){
    $xs = new XS(config_path('search-demo.ini'));
    $search = $xs->search; // 获取 搜索对象
    $query = $key;
    $search->setQuery($query)
        ->setSort('chrono', true) //按照chrono 正序排列
        ->setLimit(20,0) // 设置搜索语句, 分页, 偏移量
    ;

    $docs = $search->search(); // 执行搜索,将搜索结果文档保存在 $docs 数组中
    $count = $search->count(); // 获取搜索结果的匹配总数估算值
    foreach ($docs as $doc){
        $subject = $search->highlight($doc->subject); // 高亮处理 subject 字段
        $message = $search->highlight($doc->message); // 高亮处理 message 字段
        echo $doc->rank() . '. ' . $subject . " [" . $doc->percent() . "%] - ";
        echo date("Y-m-d", $doc->chrono) . "<br>" . $message . "<br>";
        echo '<br>========<br>';
    }
    echo  '总数:'. $count;
});

Route::get('/makedoc/{title}/{message}', function ($title, $message){
    $xs = new XS('demo');
    $doc = new XSDocument;
    $doc->setFields([
        'pid' => 1,
        'subject' => $title,
        'message' => $message,
        'chrono' => time(),
    ]); // 用数组进行批量赋值
    $xs->index->add($doc);
});
Copy after login
Copy after login

Use a browser to access the above two routes to test the function

Access: http://Your domain name/makedoc/title/content Test generated index

Test example:

http://你的域名/makedoc/关于 xunsearch 的 DEMO 项目测试/项目测试是一个很有意思的行为!

http://你的域名/makedoc/测试第二篇/这里是第二篇文章的内容

http://你的域名/makedoc/项目测试第三篇/俗话说,无三不成礼,所以就有了第三篇
Copy after login
Copy after login

Note: The index does not take effect immediately and the delay is about 10 seconds About

Test the search function by visiting: http://your domain name/search/keyword
such as search keyworditem

<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">1. 关于 xunsearch 的 DEMO 项目测试 [99%] - 2011-08-26 项目测试是一个很有意思的行为! ======== 2. 项目测试第三篇 [100%] - 2011-08-26 俗话说,无三不成礼,所以就有了第三篇 ======== 总数:2</pre><div class="contentsignin">Copy after login</div></div><div class="contentsignin">Copy after login</div></div>

Through the above steps, we have completed the simplest full-text search example.
If you want to know more details, please read the official documentation of xunsearch.

More Laravel xunsearch full-text search For related articles, please pay attention to the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template