zoukankan      html  css  js  c++  java
  • xunsearch 在 window 下测试实践(2)

    # 先上测试效果:

    # Xunsearch 搜索骨架代码

    # 《XUNSEARCH 10分钟入门》

    # 在 Laravel框架 下,还有 Xunsearch 生成的骨架代码基础上,测试 Xunsearch 基础用法;代码导入数据、更新、删除、搜索,基本功能就有了;

    <?php
    
    namespace AppHttpControllers;
    
    use AppPost;
    use CarbonCarbon;
    use IlluminateHttpRequest;
    use IlluminatePaginationLengthAwarePaginator;
    use IlluminatePaginationPaginator;
    
    //加载XS.php
    require_once "../vendor/xunsearch/lib/XS.php";
    
    class XunsearchController extends Controller
    {
        public function __construct()
        {
    
        }
    
        /** 
         * Display a listing of the resource.
         *
         * @return IlluminateHttpResponse
         */
        public function index(Request $request)
        {
            $keyword = !empty($request->all()['wd']) ? $request->all()['wd'] : '';
            $page = !empty($request->all()['page']) ? $request->all()['page'] : 1;
    
            // TODO xunsearch 迅搜测试
            if($keyword){
                $count = $total = $search_cost = 0;
                $docs = $related = $corrected = $hot = array();
                $error = $pager = '';
    
                try {
                    $xs = new XS('laravel'); //// 创建 XS 对象
                    $index = $xs->index; // 获取索引对象
                    $search = $xs->search; // 获取搜索对象
    
                    // TODO 清空索引
                    $index->clean(); die;
    
                    $arr = Post::all()->toArray();
                    foreach ($arr as $v){
                        $document = new XSDocument($v);
    
                        // TODO 添加数据,不管是否已经存在相同主键数据
                        $index->add($document);
    
                        // TODO 更新数据,同主键则更新数据,不同则添加(推荐)
                        $index->update($document);
    
                        // TODO 删除数据,传入主键,单个可直接传,多个放数组中,例如 array(1, 3, 5)
                        if(in_array($v['id'], [2, 4])){
                            // 删除掉主键为 2,4的数据
                            $index->del($v['id']);
                        }
                    }
    
                    // 同步索引数据
                    $index->flushIndex();
    
                } catch (XSException $e) {
                    echo $e->getMessage() . PHP_EOL;
                    exit;
                }
    
    
                $listRows = 3;
                $search->setLimit($listRows, $listRows*($page-1)); // 设置偏移量
    
                $search_begin = microtime(true);
                $docs = $search->setQuery($keyword)->search(); // 查询
                $search_cost = microtime(true) - $search_begin;
    
    //            $count = $search->count($keyword); // // 直接检索关键词的数量
                $count = $search->getLastCount(); // 获取搜索总数,这个和count()的不一样是不用传入搜索词
                $total = $search->getDbTotal();
    
                // TODO 返回纠正后的关键词组成的数组
                if ($count < 1 || $count < ceil(0.001 * $total)) {
                    $corrected = $search->getCorrectedQuery();
                }
    
                $hot = $search->getHotQuery();
                $related = $search->getRelatedQuery($keyword, 5);
    
                $post = [];
                foreach ($docs as $key=>$doc){
                    $post[$key]->id = $doc->id; // 高亮处理标题
                    $post[$key]->title = $search->highlight($doc->title); // 高亮处理标题
                    $post[$key]->body = $search->highlight($doc->body); // 高亮处理标题
                }
    
                $paginator = new LengthAwarePaginator($post, $count, $listRows, $page, [
                    'path' => Paginator::resolveCurrentPath(),
                    'pageName' => 'page',
                ]);
    
                // TODO 添加参数到分页链接
                $paginator->appends(['wd'=>$keyword]);
    
                $data = array(
                    'post'=>$post,
                    'paginator'=>$paginator,
                    'count' => $count,
                    'total' => $total,
                    'search_cost' => $search_cost,
                    'corrected' => $corrected,
                    'hot' => $hot,
                    'related' => $related,
                );
                return view('post.index', $data);
            }
    }
  • 相关阅读:
    机器任务——最小点覆盖
    树的统计
    农夫约翰
    关押罪犯
    题单
    加分二叉树(递归,区间DP)
    [动态规划] 斜率优化DP
    [树形DP] 换根DP
    [期望DP][SCOI2008] 奖励关
    [数位DP][AHOI2009] Luogu P4127 同类分布
  • 原文地址:https://www.cnblogs.com/pyspang/p/12755473.html
Copyright © 2011-2022 走看看