美文网首页
讯搜引擎搜索

讯搜引擎搜索

作者: 安晓生 | 来源:发表于2020-03-30 17:29 被阅读0次

如何安装 Xunsearch我只说一个大概,详情请参照官方文档。 首先要安装 Xunsearch,安装命令如下: wget
如何安装 Xunsearch
我只说一个大概,详情请参照官方文档。
首先要安装 Xunsearch,安装命令如下:

wget http://www.xunsearch.com/download/xunsearch-full-latest.tar.bz2
tar -jxvf xunsearch-full-latest.tar.bz2
cd xunsearch-full-1.4.9 # 注意此处的文件夹名,可能有不一样
sh ./setup.sh安装完记得要启动 xunsearch 服务程序,示例如下(安装完之后会提示你如何启动):
你的安装目录/xunsearch/bin/xs-ctl.sh start

Yii2 如何使用 Xunsearch
首先要在配置文件 frontend/config/main.php 添加 xunsearch 组件:

'components' => [
    ...
    'xunsearch' => [
        'class' => 'hightman\xunsearch\Connection', // 此行必须
        'iniDirectory' => '@common/config',    // 搜索 ini 文件目录,默认:@vendor/hightman/xunsearch/app
        'charset' => 'utf-8',   // 指定项目使用的默认编码,默认即时 utf-8,可不指定
    ],
    ...
],

在 common/config 目录下添加 xunsearch 配置文件 search.ini, 下面是 yii2cmf 配置:
下面这个根据自己字段配置。

project.name = yii2cmf
project.default_charset = utf-8
server.index = 8383
server.search = 8384
[id]
type = id
[title]
type = title
[content]
type = body
[status]
index = self
tokenizer = full
[published_at]
type = numeric

新建一个 Model 文件,yii2cmf 的 Model 是 common/models/Search.php 里面的代码就跟写 Yii2 一样的:

<?php
namespace common\models;
use yii\data\ActiveDataProvider;
class Search extends \hightman\xunsearch\ActiveRecord
{
    public function search($keyword)
    {
        $query = self::find()->where($keyword)->andWhere(['status' => 1]);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'published_at' => SORT_DESC,
                ]
            ]
        ]);
        return $dataProvider;
    }
}

添加新文章的时候实现自动入库
创建XsBehavior并绑定到ArticleData模型里

<?php
namespace common\behaviors;
use common\models\Article;
use common\models\Search;
use yii\base\Behavior;
use yii\db\ActiveRecord;
class XsBehavior extends Behavior
{
    public function events()
    {
        return [
            ActiveRecord::EVENT_AFTER_INSERT => [$this, 'afterSaveInternal'],
            ActiveRecord::EVENT_AFTER_UPDATE => [$this, 'afterSaveInternal']
        ];
    }
    public function afterSaveInternal($event)
    {
        $article = Article::findOne(['id' => $event->sender->id]);
        if (!empty($article)) {
            if ($event->name == 'afterInsert') {
                $search = new Search();
                $search->id = $event->sender->id;
            } else {
                $search = Search::findOne($event->sender->id);
            }
            $search->status = $article->status;
            $search->title = $article->title;
            $search->content = $event->sender->content;
            $search->published_at = $article->published_at;
            $search->save();
        }
    }
}

剩下的就是搜索的实现了,更详情代码请参照 yii2cmf 源码
如何导入以前的数据到 Xunsearch
下面是 yii2cmf 导入数据的具体实现(记得数据库用户名和密码,库名要改成自己的):

php vendor/hightman/xunsearch/util/Indexer.php --source=mysql://root:root@localhost/yii --sql="SELECT a.id,a.title,d.content,a.status,a.published_at FROM pop_article a INNER JOIN pop_article_data d ON d.id=a.id" --project=common/config/search.ini

查看 Xunsearch 搜索当前导入的词库
在 yii2cmf 根目录运行:

php vendor/hightman/xunsearch/util/Quest.php -p common/config/search.ini 'keyword'

相关文章

网友评论

      本文标题:讯搜引擎搜索

      本文链接:https://www.haomeiwen.com/subject/hrnvuhtx.html