Thinkphp6结合Xunsearch做百万级数据处理

浏览:2001 发布日期:2020/09/28 分类:ThinkPHP6专区 关键字: 搜索引擎 thinkphp6 百万级数据
Xunsearch (中文名:迅搜)是一套免费开源的专业中文全文检索解决方案,简单易用而且 功能强大、性能卓越能轻松处理海量数据的全文检索。它包含后端索引、搜索服务程序和前端 脚本语言编写的开发工具包(称之为 SDK) 。
之前发布过xunsearch安装教程
https://www.thinkphp.cn/code/6847.html
在控制器更新和添加xunsearch都会有延时
使用命令行导入工具是实时更新的vendor/bin/xs index直接导入mysql--source=mysql://[user[:passwd]@]host/dbname[/table]可以在thinkphp6创建自定义指令php think make:command Xunsearch xunsearch代码我们修改内容如下:protected function configure()
    {
        // 指令配置
        $this->setName('xunsearch')
            ->addArgument('id', Argument::OPTIONAL, "your id")
            ->setDescription('the xunsearch command');        
    }

    protected function execute(Input $input, Output $output)
    {
        $id=$input->getArgument('id');
        if($id){
            exec('cd ../; vendor/bin/xs index --project=demo--source=mysql://root:root@127.0.0.1/dbname/tbname --sql="select * from tbname where id='.$id.'"',$data);
            for ($i=0; $i <count($data); $i++) { 
                $output->writeln($data[$i]);
            }            
            $output->writeln('添加ID='.$id.'数据到Xunsearch');
        }else{            
            exec('cd ../; vendor/bin/xs index --project=demo --source=mysql://root:root@127.0.0.1/dbname/tbname',$data);
            for ($i=0; $i <count($data); $i++) { 
                $output->writeln($data[$i]);
            }
            $output->writeln('添加所有数据到Xunsearch');
        }
    }
第二步,配置config/console.php文件<?php
return [
    'commands' => [
        'xunsearch' => 'app\command\Xunsearch',
    ]
];
在控制器摸着模型中$id=['1'];
$output = Console::call('xunsearch', $id);
return $output->fetch();
最佳答案
评论( 相关
后面还有条评论,点击查看>>