xunsearch 在 window 下测试实践(2)

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

<?php

namespace App\Http\Controllers;

use App\Post;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;

//加载XS.php
require_once "../vendor/xunsearch/lib/XS.php";

class XunsearchController extends Controller
{
    public function __construct()
    {

    }

    /** 
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    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);
        }
}

 

# 功能测试效果:

xunsearch 在 window 下测试实践(2)

 

xunsearch 在 window 下测试实践(2)

上一篇:C#中重写、重载和隐藏的概念


下一篇:Linux到底学什么?如何学?