Full-text on Postgres, stemmed in the language searched; word matching elsewhere. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api\PublicApi\V1;
|
|
|
|
use App\Support\Search\SiteSearch;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
/**
|
|
* GET /public/v1/search?q=…
|
|
*
|
|
* What the site shows that matches some words, in the request's language,
|
|
* best match first.
|
|
*/
|
|
class SearchController extends PublicController
|
|
{
|
|
public function __invoke(Request $request, SiteSearch $search): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'q' => ['required', 'string', 'min:2', 'max:120'],
|
|
'type' => ['sometimes', 'array'],
|
|
'type.*' => [Rule::in(SiteSearch::TYPES)],
|
|
'limit' => ['sometimes', 'integer', 'min:1', 'max:50'],
|
|
]);
|
|
|
|
$results = $search->search(
|
|
$data['q'],
|
|
app()->getLocale(),
|
|
$data['type'] ?? SiteSearch::TYPES,
|
|
(int) ($data['limit'] ?? 20),
|
|
);
|
|
|
|
return response()->json([
|
|
'data' => $results,
|
|
'meta' => ['query' => $data['q'], 'locale' => app()->getLocale(), 'count' => $results->count()],
|
|
]);
|
|
}
|
|
}
|