fixed search again
This commit is contained in:
parent
281df80691
commit
cfa3bae1f7
|
|
@ -14,6 +14,8 @@ use Illuminate\Contracts\Container\Container;
|
|||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Spatie\ElasticsearchQueryBuilder\Aggregations\TermsAggregation;
|
||||
use Spatie\ElasticsearchQueryBuilder\Aggregations\TopHitsAggregation;
|
||||
use Spatie\ElasticsearchQueryBuilder\Builder;
|
||||
use Spatie\ElasticsearchQueryBuilder\Queries\BoolQuery;
|
||||
use Spatie\ElasticsearchQueryBuilder\Queries\MultiMatchQuery;
|
||||
|
|
@ -50,11 +52,13 @@ class SearchController extends AbstractListController
|
|||
->addQuery(
|
||||
$this->addFilters($filterQuery, $actor)
|
||||
)
|
||||
->addAggregation(
|
||||
TermsAggregation::create('discussions', 'discussion_id')
|
||||
->aggregation(TopHitsAggregation::create('hits', 1))
|
||||
)
|
||||
->search();
|
||||
|
||||
$ids = Collection::make(Arr::get($result, 'hits.hits'))->pluck('_id')->toArray();
|
||||
|
||||
return $schema::query()->findMany($ids);
|
||||
return $schema::results(Arr::get($result, 'hits.hits'));
|
||||
}
|
||||
|
||||
protected function getSchema(string $index): ?Schema
|
||||
|
|
|
|||
|
|
@ -2,12 +2,14 @@
|
|||
|
||||
namespace Blomstra\Search\Schemas;
|
||||
|
||||
use Flarum\Api\Serializer\PostSerializer;
|
||||
use Flarum\Api\Serializer\DiscussionSerializer;
|
||||
use Flarum\Discussion\Discussion;
|
||||
use Flarum\Post\CommentPost;
|
||||
use Flarum\Post\Event\Deleted;
|
||||
use Flarum\Post\Event\Posted;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class CommentPostSchema extends Schema
|
||||
{
|
||||
|
|
@ -17,7 +19,8 @@ class CommentPostSchema extends Schema
|
|||
'author' => $post->user_id,
|
||||
'created_at' => $post->created_at->toAtomString(),
|
||||
'private' => $post->is_private,
|
||||
'groups' => $this->groupsForDiscussion($post->discussion)
|
||||
'groups' => $this->groupsForDiscussion($post->discussion),
|
||||
'discussion_id' => $post->discussion?->id
|
||||
];
|
||||
|
||||
|
||||
|
|
@ -37,8 +40,8 @@ class CommentPostSchema extends Schema
|
|||
public function fulltext(CommentPost $post): array
|
||||
{
|
||||
return [
|
||||
'title' => $post->discussion->title,
|
||||
'content' => $post->content
|
||||
'title' => $post->discussion?->title,
|
||||
'content' => $post->exists ? $post->content : null,
|
||||
];
|
||||
}
|
||||
|
||||
|
|
@ -55,12 +58,25 @@ class CommentPostSchema extends Schema
|
|||
public static function query(): Builder
|
||||
{
|
||||
return CommentPost::query()
|
||||
->where('type', CommentPost::$type);
|
||||
->where('type', CommentPost::$type)
|
||||
->with('discussion');
|
||||
}
|
||||
|
||||
public static function results(array $hits): \Illuminate\Database\Eloquent\Collection
|
||||
{
|
||||
$postIds = Collection::make($hits)->keyBy('_source.discussion_id')->pluck('_id');
|
||||
$discussionIds = Collection::make($hits)->pluck('_source.discussion_id');
|
||||
|
||||
return Discussion::query()->findMany($discussionIds)->map(function (Discussion $discussion) use ($postIds) {
|
||||
$discussion->most_relevant_post_id = $postIds->get($discussion->id);
|
||||
|
||||
return $discussion;
|
||||
})->load('mostRelevantPost');
|
||||
}
|
||||
|
||||
public static function serializer(): string
|
||||
{
|
||||
return PostSerializer::class;
|
||||
return DiscussionSerializer::class;
|
||||
}
|
||||
|
||||
public static function savingOn(Dispatcher $events, callable $callable)
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use Flarum\Group\Permission;
|
|||
use Flarum\Tags\Tag;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
abstract class Schema
|
||||
{
|
||||
|
|
@ -24,6 +25,7 @@ abstract class Schema
|
|||
|
||||
abstract public static function model(): string;
|
||||
abstract public static function query(): Builder;
|
||||
abstract public static function results(array $hits): Collection;
|
||||
|
||||
abstract public static function serializer(): string;
|
||||
|
||||
|
|
@ -39,7 +41,7 @@ abstract class Schema
|
|||
->pluck('group_id');
|
||||
|
||||
if ($this->extensionEnabled('flarum-tags')) {
|
||||
/** @var \Illuminate\Database\Eloquent\Collection $tags */
|
||||
/** @var Collection $tags */
|
||||
$tags = $discussion->tags;
|
||||
|
||||
$filters['tags'] = $tags->pluck('id')->toArray();
|
||||
|
|
|
|||
Loading…
Reference in New Issue