From 7bcaa67aaba5b6c8bacb16dd87c7ca3728fb96d2 Mon Sep 17 00:00:00 2001 From: Daniel Klabbers Date: Wed, 13 Oct 2021 20:37:43 +0200 Subject: [PATCH] basic search --- extend.php | 2 +- src/Api/Controllers/SearchController.php | 35 +++++++++++++++++++++++- src/Schemas/DiscussionSchema.php | 6 ++++ src/Schemas/Schema.php | 2 ++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/extend.php b/extend.php index 47d6a8a..426080d 100644 --- a/extend.php +++ b/extend.php @@ -11,7 +11,7 @@ return [ ->js(__DIR__ . '/js/dist/forum.js'), (new Flarum\Routes('api')) - ->get('blomstra/search', 'blomstra.search', Api\Controllers\SearchController::class), + ->get('/blomstra/search/{index}', 'blomstra.search', Api\Controllers\SearchController::class), (new Flarum\Console) ->command(Commands\RebuildDocumentsCommand::class) diff --git a/src/Api/Controllers/SearchController.php b/src/Api/Controllers/SearchController.php index 5ada626..a866c88 100644 --- a/src/Api/Controllers/SearchController.php +++ b/src/Api/Controllers/SearchController.php @@ -2,14 +2,47 @@ namespace Blomstra\Search\Api\Controllers; +use Blomstra\Search\Schemas\Schema; use Flarum\Api\Controller\AbstractListController; +use Illuminate\Contracts\Container\Container; +use Illuminate\Support\Arr; +use Illuminate\Support\Collection; +use MeiliSearch\Client; use Psr\Http\Message\ServerRequestInterface; use Tobscure\JsonApi\Document; class SearchController extends AbstractListController { + public function __construct(protected Client $meili) + {} + protected function data(ServerRequestInterface $request, Document $document) { - // TODO: Implement data() method. + $index = Arr::get($request->getQueryParams(), 'index'); + $filters = $this->extractFilter($request); + + $schema = $this->getSchema($index); + + $result = $this->meili->index($index)->search($filters['q'], [ + 'offset' => $this->extractOffset($request), + 'limit' => $this->extractLimit($request), + 'sort' => $this->extractSort($request) + ]); + + $this->serializer = $schema::serializer(); + + return Collection::make($result->getHits()) + ->map(function ($hit) use ($schema) { + return $schema::model()::query()->find($hit['id']); + }); + } + + protected function getSchema(string $index): ?Schema + { + $mapping = resolve(Container::class)->tagged('blomstra.search.schemas'); + + return collect($mapping)->first(function (Schema $schema) use ($index) { + return $schema::index() === $index; + }); } } diff --git a/src/Schemas/DiscussionSchema.php b/src/Schemas/DiscussionSchema.php index 256923a..9ae05c0 100644 --- a/src/Schemas/DiscussionSchema.php +++ b/src/Schemas/DiscussionSchema.php @@ -2,6 +2,7 @@ namespace Blomstra\Search\Schemas; +use Flarum\Api\Serializer\DiscussionSerializer; use Flarum\Discussion\Discussion; use Flarum\Discussion\Event\Deleted; use Flarum\Discussion\Event\Hidden; @@ -39,6 +40,11 @@ class DiscussionSchema extends Schema return Discussion::class; } + public static function serializer(): string + { + return DiscussionSerializer::class; + } + public static function savingOn(Dispatcher $events, callable $callable) { $events->listen(Started::class, function (Started $event) use ($callable) { diff --git a/src/Schemas/Schema.php b/src/Schemas/Schema.php index d70a5b1..c0b5782 100644 --- a/src/Schemas/Schema.php +++ b/src/Schemas/Schema.php @@ -19,6 +19,8 @@ abstract class Schema abstract public static function model(): string; + abstract public static function serializer(): string; + abstract public static function savingOn(Dispatcher $events, callable $callable); abstract public static function deletingOn(Dispatcher $events, callable $callable); }