1
0
mirror of https://github.com/flarum/core.git synced 2025-08-04 23:47:32 +02:00

Improve fulltext search API and interface

This commit is contained in:
Toby Zerner
2015-07-07 20:35:18 +09:30
parent 38c2ff0306
commit 42f1fa1272
12 changed files with 62 additions and 81 deletions

View File

@@ -4,7 +4,7 @@ use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Flarum\Core\Users\User;
use Flarum\Core\Discussions\Discussion;
use Flarum\Core\Discussions\Search\Fulltext\DriverInterface;
use Flarum\Core\Discussions\Search\Fulltext\Driver;
// TODO: In some cases, the use of a post repository incurs extra query expense,
// because for every post retrieved we need to check if the discussion it's in
@@ -28,13 +28,6 @@ use Flarum\Core\Discussions\Search\Fulltext\DriverInterface;
class PostRepository
{
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.
@@ -99,31 +92,6 @@ class PostRepository
return $this->filterVisibleTo($posts, $actor);
}
/**
* Find posts by matching a string of words against their content,
* optionally making sure they are visible to a certain user.
*
* @param string $string
* @param \Flarum\Core\Users\User|null $actor
* @return \Illuminate\Database\Eloquent\Collection
*/
public function findByContent($string, User $actor = null)
{
$ids = $this->fulltext->match($string);
$ids = $this->filterDiscussionVisibleTo($ids, $actor);
$query = Post::select('id', 'discussion_id')->whereIn('id', $ids);
foreach ($ids as $id) {
$query->orderByRaw('id != ?', [$id]);
}
$posts = $query->get();
return $this->filterVisibleTo($posts, $actor);
}
/**
* Get the position within a discussion where a post with a certain number
* is. If the post with that number does not exist, the index of the