basic search
This commit is contained in:
parent
a5411efe73
commit
7bcaa67aab
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue