xunsearch搜索文章可以搜索出来,搜索用户却搜索不出来

发布于 2022-09-07 04:08:45 字数 7344 浏览 26 评论 0

1. 我使用xunsearch做搜索功能,为两张表建立了文档索引,一个是文章表,一个是用户表
2. 我的配置文件有两个user.ini article.ini 内容如下

project.name = user
project.default_charset = utf-8
server.index = xxx:8383
server.search = xxx:8384

[id]
type = id

[nickname]
index = self
phrase = yes

[avatar]

[article_numbers]
type = numeric

[follow_numbers]
type = numeric

[fans_numbers]
type = numeric

[createtime]
type = numeric
project.name = article
project.default_charset = utf-8
server.index = xxx:8383
server.search = xxx:8384

[id]
type = id

[author]
type = string

[user_id]

[category_id]
index = self
tokenizer = full

[category]
type = both

[title]
type = title

[cover_url]
type = both

[content]
type = body

[reply_numbers]
type = numeric

[star_numbers]
type = numeric

[read_numbers]
type = numeric

[createtime]
type = numeric

3. 建立索引文档

    /**
     * 根据文章生成或者更新索引文档
     * @param $type string 根据参数判断是更新(update)还是重新生成文章文档(add)
     * @param $subject string 根据参数判断是生成文章索引(article)还是生成用户索引(user)
     * @return bool
     * @throws \XSException
     * @throws \think\exception\DbException
     */
    public function makedoc()
    {
        $request = Request::instance();
        $type = $request->param('type');
        $subject = $request->param('subject');
        switch ($subject){
            case 'article':
                $map = [
                    'status' => ExtraConst::NORMAL
                ];
                $order = 'collect_numbers desc,reply_numbers desc,star_numbers desc,read_numbers desc,id desc';
                $articles = Article::all(function ($query) use ($map, $order) {
                    $query->where($map)->order($order);
                });
                if (!$articles) {
                    return false;
                }
                $xs = new \XS(file_get_contents(ROOT_PATH . 'docs' . DIRECTORY_SEPARATOR . 'article.ini'));
                $doc = new \XSDocument();
                foreach ($articles as $article) {
                    $doc->setFields([
                        'id'            => $article->id,
                        'author'        => $article->author,
                        'user_id'       => $article->user_id,
                        'category_id'   => $article->category_id,
                        'category'      => $article->category,
                        'title'         => $article->title,
                        'content'       => $article->content,
                        'cover_url'     => $article->cover_url,
                        'reply_numbers' => $article->reply_numbers,
                        'star_numbers'  => $article->star_numbers,
                        'read_numbers'  => $article->read_numbers,
                        'createtime'    => $article->createtime,
                    ]);
                    if ($type === 'add') {
                        $result = $xs->index->add($doc);
                    } elseif ($type === 'update') {
                        $result = $xs->index->update($doc);
                    }
                }
                break;
            case 'user':
                $map = [
                    'status' => 'normal'
                ];
                $order = 'id desc';
                $users = User::all(function ($query) use ($map, $order){
                    $query->where($map)->order($order);
                });
                if (!$users) {
                    return false;
                }
                $xs = new \XS(file_get_contents(ROOT_PATH . 'docs' . DIRECTORY_SEPARATOR . 'user.ini'));
                $doc = new \XSDocument();
                foreach ($users as $user){
                    $doc->setFields([
                        'id'                => $user->id,
                        'nickname'          => $user->nickname,
                        'avatar'            => $user->avatar,
                        'article_numbers'   => $user->article_numbers,
                        'follow_numbers'    => $user->follow_numbers,
                        'fans_numbers'      => $user->fans_numbers,
                        'createtime'        => $user->createtime,
                    ]);
                    if ($type === 'add') {
                        $result = $xs->index->add($doc);
                    } elseif ($type === 'update') {
                        $result = $xs->index->update($doc);
                    }
                }
                break;
        }

        if ($result) {
            echo '--------success  ----------';
        }
    }

4. 根据关键词搜索内容

    /**
     * 按照关键词搜索内容
     * @return array
     * @throws \XSException
     */
    public function search()
    {
        $request = Request::instance();
        $query = $request->post('key');
        $article_ini = file_get_contents(ROOT_PATH . 'docs' . DIRECTORY_SEPARATOR . 'article.ini');
        $user_ini = file_get_contents(ROOT_PATH . 'docs' . DIRECTORY_SEPARATOR . 'user.ini');
        // 加载文章配置 初始化文章索引
        $a_xs = new \XS($article_ini);
        $a_search = $a_xs->search;
        $a_search->setQuery($query)->setSort('createtime', true)->setLimit(20, 0);
        $a_docs = $a_search->search();
        $a_count = $a_search->count();
        // 加载用户配置 初始化用户索引
        $u_xs = new \XS($user_ini);
        $u_search = $u_xs->search;
        $u_search->setQuery($query)->setSort('createtime', true)->setLimit(20, 0);
        $u_docs = $u_search->search();
        $u_count = $u_search->count();
        dump($a_docs);
        dump($u_docs);die;
        // 初始化变量

5. 使用postman测试接口

array(3) {
  ["code"] => int(1024)
  ["users"] => array(0) {
  }
  ["articles"] => array(4) {
    [1] => array(10) {
      ["id"] => string(10) "jo1gn7l9b8"
      ["title"] => string(36) "啦啦啦这也是一篇测试文章"
      ["author"] => string(22) "区块链学院-文杰"
      ["user_id"] => string(10) "xenp87gyvk"
      ["read_numbers"] => string(3) "123"
      ["reply_numbers"] => string(4) "3445"
      ["star_numbers"] => string(4) "1322"
      ["cover_url"] => string(41) "/uploads/cover/20180423/5add3c3664db9.jpg"
      ["content"] => string(57) "这是一篇测试文章啊哈哈哈哈啊哈哈哈哈哈"
      ["createtime"] => string(11) "04-11 18:52"
    }
    [2] => array(10) {
      ["id"] => string(10) "jo1gn7l9b8"
      ["title"] => string(36) "啦啦啦这也是一篇测试文章"
      ["author"] => string(22) "区块链学院-文杰"
      ["user_id"] => string(10) "xenp87gyvk"
      ["read_numbers"] => string(3) "123"
      ["reply_numbers"] => string(4) "3445"
      ["star_numbers"] => string(4) "1322"
      ["cover_url"] => string(41) "/uploads/cover/20180423/5add3c3664db9.jpg"
      ["content"] => string(57) "这是一篇测试文章啊哈哈哈哈啊哈哈哈哈哈"
      ["createtime"] => string(11) "04-11 18:52"
    }

6.我测试了很多次
文章的关键词很轻松的就可以搜索出来,而用户表的得nickname数据,怎么样都搜不出来,
我尝试到linux的/usr/local/xunsearch/data/db中去找索引文档,是有user和article文档的,请问我的代码到底是哪里出了问题?我的UserModel中我断点调试过,是有数据的,nickname关键词也是存在的。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花之痕靓丽 2022-09-14 04:08:45

兄弟,你这写的这两个表,是查询两个表的内容吗?最后效果实现了吗?我目前在写这个,向你请教下呗

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文