mirror of
https://github.com/flarum/core.git
synced 2025-08-17 22:01:44 +02:00
Really rough fulltext driver implementation
This commit is contained in:
@@ -89,6 +89,11 @@ class CoreServiceProvider extends ServiceProvider
|
||||
'Flarum\Core\Repositories\EloquentActivityRepository'
|
||||
);
|
||||
|
||||
$this->app->bind(
|
||||
'Flarum\Core\Search\Discussions\Fulltext\DriverInterface',
|
||||
'Flarum\Core\Search\Discussions\Fulltext\MySqlFulltextDriver'
|
||||
);
|
||||
|
||||
$avatarFilesystem = function (Container $app) {
|
||||
return $app->make('Illuminate\Contracts\Filesystem\Factory')->disk('flarum-avatars')->getDriver();
|
||||
};
|
||||
|
@@ -3,9 +3,17 @@
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Flarum\Core\Models\Post;
|
||||
use Flarum\Core\Models\User;
|
||||
use Flarum\Core\Search\Discussions\Fulltext\DriverInterface;
|
||||
|
||||
class EloquentPostRepository implements PostRepositoryInterface
|
||||
{
|
||||
protected $fulltext;
|
||||
|
||||
public function __construct(DriverInterface $fulltext)
|
||||
{
|
||||
$this->fulltext = $fulltext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a post by ID, optionally making sure it is visible to a certain
|
||||
* user, or throw an exception.
|
||||
@@ -72,10 +80,13 @@ class EloquentPostRepository implements PostRepositoryInterface
|
||||
*/
|
||||
public function findByContent($string, User $user = null)
|
||||
{
|
||||
$query = Post::select('id', 'discussion_id')
|
||||
->where('content', 'like', '%'.$string.'%');
|
||||
// ->whereRaw('MATCH (`content`) AGAINST (? IN BOOLEAN MODE)', [$string])
|
||||
// ->orderByRaw('MATCH (`content`) AGAINST (?) DESC', [$string])
|
||||
$ids = $this->fulltext->match($string);
|
||||
|
||||
$query = Post::select('id', 'discussion_id')->whereIn('id', $ids);
|
||||
|
||||
foreach ($ids as $id) {
|
||||
$query->orderByRaw('id != ?', [$id]);
|
||||
}
|
||||
|
||||
return $this->scopeVisibleForUser($query, $user)->get();
|
||||
}
|
||||
|
6
src/Core/Search/Discussions/Fulltext/DriverInterface.php
Normal file
6
src/Core/Search/Discussions/Fulltext/DriverInterface.php
Normal file
@@ -0,0 +1,6 @@
|
||||
<?php namespace Flarum\Core\Search\Discussions\Fulltext;
|
||||
|
||||
interface DriverInterface
|
||||
{
|
||||
public function match($string);
|
||||
}
|
13
src/Core/Search/Discussions/Fulltext/MySqlFulltextDriver.php
Normal file
13
src/Core/Search/Discussions/Fulltext/MySqlFulltextDriver.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php namespace Flarum\Core\Search\Discussions\Fulltext;
|
||||
|
||||
use Flarum\Core\Models\Post;
|
||||
|
||||
class MySqlFulltextDriver implements DriverInterface
|
||||
{
|
||||
public function match($string)
|
||||
{
|
||||
return Post::whereRaw('MATCH (`content`) AGAINST (? IN BOOLEAN MODE)', [$string])
|
||||
->orderByRaw('MATCH (`content`) AGAINST (?) DESC', [$string])
|
||||
->lists('id');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user