172 lines
4.6 KiB
PHP
172 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
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 {
|
|
try {
|
|
$this->client->collections['events']->delete();
|
|
} catch (\Exception $e) {
|
|
// ignore si la collection n'existe pas
|
|
}
|
|
|
|
$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);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function createUsersCollection()
|
|
{
|
|
try {
|
|
$collections = $this->client->collections->retrieve();
|
|
|
|
if (!in_array('users', array_column($collections, 'name'))) {
|
|
$this->client->collections->create([
|
|
'name' => 'users',
|
|
'fields' => [
|
|
['name' => 'name', 'type' => 'string'],
|
|
['name' => 'lastname', 'type' => 'string'],
|
|
['name' => 'validate', 'type' => 'bool'],
|
|
],
|
|
]);
|
|
}
|
|
} catch (Exception $e) {
|
|
throw new Exception("Error while creating the users collection : " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
private function indexUsers()
|
|
{
|
|
try {
|
|
$users = User::where('validate', 1)->get();
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$this->client->collections['users']->documents->upsert([
|
|
'id' => (string) $user->id,
|
|
'name' => $user->name,
|
|
'lastname' => $user->lastname,
|
|
'validate' => (bool) $user->validate,
|
|
]);
|
|
}
|
|
} catch (Exception $e) {
|
|
throw new Exception("Error while indexing users : " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function searchUsers(Request $request)
|
|
{
|
|
try {
|
|
$this->authorize('searchUsers');
|
|
try {
|
|
$this->client->collections['users']->delete();
|
|
} catch (\Exception $e) {
|
|
// ignore si la collection n'existe pas
|
|
}
|
|
|
|
$this->indexUsers();
|
|
$query = $request->input('query', '');
|
|
|
|
$searchResults = $this->client->collections['users']->documents->search([
|
|
'q' => $query,
|
|
'query_by' => 'name,lastname',
|
|
'filter_by' => 'validate:=true',
|
|
]);
|
|
|
|
return response()->json($searchResults);
|
|
} catch (Exception $e) {
|
|
return response()->json(['error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
}
|