Home >Backend Development >PHP Tutorial >Laravel xunsearch full text search
Before starting, it is highly recommended to browse the official documentation of 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
Function | IP | System | |
---|---|---|---|
Provides web services | 192.168.56.2 | centos7 | |
Provides xunsearch back-end service | 192.168.56.3 | centos7 |
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
Connect to server B. Run the following instructions to install and decompress the xunsearch installation package.
wget http://www.php.cn/ tar -xjf xunsearch-full-latest.tar.bz2
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
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 | +=================================================+
. 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
Start service
/usr/local/xunsearch/bin/xs-ctl.sh -b inet start
Startup output content:
INFO: starting server[xs-indexd] ... (BIND:8383) INFO: starting server[xs-searchd] ... (BIND:8384)
Web server configuration
cd project/root/path composer require --prefer-dist hightman/xunsearch
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
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
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); });
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/项目测试第三篇/俗话说,无三不成礼,所以就有了第三篇Note: The index does not take effect immediately and the delay is about 10 seconds About
Test the search function by visiting:
your domain name/search/keywordsuch as search keyworditem<pre class="brush:php;toolbar:false">1. 关于 xunsearch 的 DEMO 项目测试 [99%] - 2011-08-26
项目测试是一个很有意思的行为!
========
2. 项目测试第三篇 [100%] - 2011-08-26
俗话说,无三不成礼,所以就有了第三篇
========
总数:2</pre>
Through the above steps, we have completed the simplest full-text search example.
Reference content
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:
IP | System | ||
---|---|---|---|
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 Connect to server B and run the following instructions to install and unzip the xunsearch installation package wget http://www.php.cn/ tar -xjf xunsearch-full-latest.tar.bz2 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 The installation interactive interface will be displayed. Then press Enter directly to install to the default path. +==========================================+ | 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 | +=================================================+
. 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 Start service /usr/local/xunsearch/bin/xs-ctl.sh -b inet start Startup output content: INFO: starting server[xs-indexd] ... (BIND:8383) INFO: starting server[xs-searchd] ... (BIND:8384) Web server configurationInstall xunsearch PHP SDK cd project/root/path composer require --prefer-dist hightman/xunsearch 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 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 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); }); 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/项目测试第三篇/俗话说,无三不成礼,所以就有了第三篇
Test the search function by visiting: http://your domain name/search/keyword Through the above steps, we have completed the simplest full-text search example. More Laravel xunsearch full-text search For related articles, please pay attention to the PHP Chinese website! |