Search the public site through the API
Full-text on Postgres, stemmed in the language searched; word matching elsewhere. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
41
app/Http/Controllers/Api/PublicApi/V1/SearchController.php
Normal file
41
app/Http/Controllers/Api/PublicApi/V1/SearchController.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?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()],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user