FLARUM 全文檢索 -- Day4

Flarum 全站搜尋功能(含中文)

1.MySqlFulltextLikeModeDriver.php

首先在 \vendor\flarum\core\src\Core\Search\Discussion\Fulltext\ 資料夾中,建立新檔案為 MySqlFulltextLikeModeDriver.php 。

MySqlFulltextLikeModeDriver.php

<?php
namespace Flarum\Core\Search\Discussion\Fulltext;
 
use Flarum\Core\Post;
use Flarum\Core\Discussion;
 
class MySqlFulltextLikeModeDriver implements DriverInterface
{
    /**
     * {@inheritdoc}
     */
    public function match($string)
    {
        $discussionTitleIds = Discussion::where('title', 'like', "%$string%")
            ->lists('id');
        $discussionIds = Post::where('type', 'comment')
            ->where('content', 'like', "%$string%")
            ->lists('discussion_id', 'id');
 
        $relevantPostIds = [];
        foreach ($discussionTitleIds as $discussionId) {
            $relevantPostIds[$discussionId][] = 0;
        }
 
        foreach ($discussionIds as $postId => $discussionId) {
            $relevantPostIds[$discussionId][] = $postId;
        }
 
        return $relevantPostIds;
    }
}


2. SearchServiceProvider.php

打開 \vendor\flarum\core\src\Core\Search\SearchServiceProvider.php

LINE 30
'Flarum\Core\Search\Discussion\Fulltext\MySqlFulltextDriver'

改成 👇👇👇

'Flarum\Core\Search\Discussion\Fulltext\MySqlFulltextLikeModeDriver'

參考網址:https://gaobukai.com/flarum/86.html


留言

張貼留言