网站制作学习网PHP→正文:laravel 安装 xunsearch
字体:

laravel 安装 xunsearch

PHP 2022/9/24 15:14:20  点击:不统计

http://%77%77%77%2E%66%6F%72%61%73%70%2E%63%6E
 网络搜索了一下,xunsearch 已经有composer 安装扩展

laravel,可以直接通过composer 进行安装。这里不说xunsearch软件 的安装,一下都是xunsearch 开启服务时测试。
 
步骤如下:
1. 安装扩展
composer require shaozeming/xunsearch-laravel -v
安装成功后,进行下一步
2. 生成配置文件
php artisan vendor:publish --provider=ShaoZeMing\\LaravelXunsearch\\XunsearchServiceProvider 
运行后 在configure 中就会有 xunsearch.php 的配置文件
上面有默认的 学生,老师等 数据库配置案例。可以直接修改为自己的数据库定义的内容。
return [
    'default' => 'xiaoshuo',   //默认搜索库的名称
    'databases' => [
        'xiaoshuo' => [# 这里是搜索库名称
            'project.name' => 'xiaoshuo', #项目名称
            'project.default_charset' => 'utf-8', #默认编码
            'server.index' => '127.0.0.1:8383', #xunsearch 索引服务接口
            'server.search' => '127.0.0.1:8384',# xunsearch 搜索服务接口
            'id' => [# 主键 id ,或者说是返回,用于搜索返回
                'type' => 'id', # 类型id
            ],
            'title' => [# 搜索索引的-标题内容 用于搜索返回
                'index' => 'mixed', # 索引类型混合
            ],
            'author' => [# 搜索索引 作者名称 ,用于搜索返回
                'index' => 'mixed',
            ],
 
        ],
 
//        更多...
    ],

3. 修改配置文件完毕后,需要创建对应搜索索引,我们需要在laravel中创建命令模式
创建命令行对应文件 
php artisan make:command Buildsearchindex
这样我们就可以在 app/Console/Commands 中看到  Buildsearchindex.php 文件
打开文件,我们看php 代码如下:
<?PHP
namespace App\Console\Commands;
 
use Illuminate\Console\Command;
use App\Models\Db_book as Mhb; #引入数据库
use ShaoZeMing\LaravelXunsearch\Facade\Xunsearch; # 这里需要引入 xunsearch 
class Buildsearchindex extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'Build:searchindex';# 自定义对应的命令的名字
 
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';# 命令描述,自定义 
 
    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $search_name = "manhua"; #定义索引库名称
        $this->info('执行开始,清理历史');
        Xunsearch::setDatabase($search_name)->cleanIndex();# 清理历史
        $this->info('执行开始建立索引');
        $Book_obj = new Mhb();#打开数据库对象
        $i=0;
        do{
            $i++;
            $data = $Book_obj->get_search_index_page_data($i);# 查询数据库返回数据数组
# 数据数组,要与 第二步骤定义的数据格式一样,字段分别是 id ,title,author
            if(count($data)>0){
                $this->info('开始第'.$i."页,有".count($data)."条数据");
                Xunsearch::setDatabase($search_name)->addIndex($data);
            }else{
                break;
            }
        }while(true);
        $this->info('执行结束');
 
        return 0;
    }
}
最后执行 创建索引命令 php artisan build:searchindex ,创建完毕后就可以搜索了。
 
4.创建完毕索引后,我们进入项目中,进行搜索服务,直接看laravel 搜索代码
<?php
use App\Http\Controllers\Controller;
use ShaoZeMing\LaravelXunsearch\Facade\Xunsearch;
use Illuminate\Http\Request;
 
class Home extends Controller
class Home extends Controller
{
    // 获取首页信息
    public function search(){
$search_word = "搜索关键词";
$res = Xunsearch::setDatabase('manhua')//设置搜索搜索数据库
->setLimit(0,10)# 设置分页
->search($search_word);  # 搜索关键词
var_dump($res);
#结果结构都与 第二部搜索索引结构一致。可以直接根据结果再操作。
}
}

网站制作学习网Foasp.cn

·上一篇:网站搜索引擎-xunsearch 网站搜索 >>    ·下一篇:Api 接口返回标准参数 >>
推荐文章
最新文章