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);
            }
    }
  • 相关阅读:
    loj#2049. 「HNOI2016」网络(set 树剖 暴力)
    创建多个Oracle数据库及相应的实例
    [置顶] lua 进阶3--lua文件中调用C++函数
    android 设置Button或者ImageButton的背景透明 半透明 透明
    struts2 18拦截器详解(七)
    《Linux命令行与shell脚本编程大全》 第二十三章 学习笔记
    ios7下不能录音问题解决
    360 2013校园招聘笔试题(含参考答案)
    【MFC三天一个游戏】之 局域网黑白棋
    Remove Nth Node From End of List
  • 原文地址:https://www.cnblogs.com/pyspang/p/12755473.html
Copyright © 2011-2022 走看看