add of typesense container, searchEvents route and SearchController and method
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Typesense\Client;
|
||||
use Exception;
|
||||
use App\Models\Events;
|
||||
|
||||
class SearchController extends Controller
|
||||
{
|
||||
protected Client $client;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new Client([
|
||||
'api_key' => 'xyz',
|
||||
'nodes' => [
|
||||
[
|
||||
'host' => env('TYPESENSE_CONNECTION'),
|
||||
'port' => '8108',
|
||||
'protocol' => 'http',
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
private function createEventsCollection()
|
||||
{
|
||||
try {
|
||||
$collections = $this->client->collections->retrieve();
|
||||
|
||||
if (!in_array('events', array_column($collections, 'name'))) {
|
||||
$this->client->collections->create([
|
||||
'name' => 'events',
|
||||
'fields' => [
|
||||
['name' => 'name', 'type' => 'string'],
|
||||
['name' => 'description', 'type' => 'string'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
throw new Exception("Error while creating the events collection : " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function indexEvents()
|
||||
{
|
||||
try {
|
||||
$events = Events::all();
|
||||
|
||||
foreach ($events as $event) {
|
||||
|
||||
$this->client->collections['events']->documents->upsert([
|
||||
'id' => (string) $event->id,
|
||||
'name' => $event->name,
|
||||
'description' => $event->description,
|
||||
]);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
throw new Exception("Error while indexing events : " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function searchEvents(Request $request)
|
||||
{
|
||||
try {
|
||||
$this->createEventsCollection();
|
||||
$this->indexEvents();
|
||||
$query = $request->input('query', '');
|
||||
|
||||
$searchResults = $this->client->collections['events']->documents->search([
|
||||
'q' => $query,
|
||||
'query_by' => 'name,description',
|
||||
]);
|
||||
|
||||
return response()->json($searchResults);
|
||||
} catch (Exception $e) {
|
||||
return response()->json(['error' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -12,7 +12,8 @@
|
||||
"php": "^8.2",
|
||||
"laravel/framework": "^12.0",
|
||||
"laravel/sanctum": "^4.0",
|
||||
"laravel/tinker": "^2.10.1"
|
||||
"laravel/tinker": "^2.10.1",
|
||||
"typesense/typesense-php": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"fakerphp/faker": "^1.23",
|
||||
|
||||
Generated
+1128
-481
File diff suppressed because it is too large
Load Diff
@@ -36,6 +36,16 @@ services:
|
||||
volumes:
|
||||
- dbdata:/var/lib/mysql
|
||||
|
||||
|
||||
typesense:
|
||||
image: typesense/typesense:29.0
|
||||
restart: on-failure
|
||||
ports:
|
||||
- "8108:8108"
|
||||
volumes:
|
||||
- ./typesense-data:/data
|
||||
command: '--data-dir /data --api-key=xyz --enable-cors'
|
||||
|
||||
volumes:
|
||||
dbdata:
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\EventsController;
|
||||
use App\Http\Controllers\SearchController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Http\Controllers\TasksController;
|
||||
|
||||
@@ -23,3 +24,6 @@ Route::middleware(['web', 'auth:sanctum'])->post('/events/tasks/{id}/update', [T
|
||||
Route::middleware(['web', 'auth:sanctum'])->get('/events', [EventsController::class, "getEvents"]);
|
||||
|
||||
Route::middleware(['web', 'auth:sanctum'])->delete('/events/{id}', [EventsController::class, "delete"]);
|
||||
|
||||
Route::middleware(['web', 'auth:sanctum'])->get('/events/search', [SearchController::class, "searchEvents"]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user