update SearchController
This commit is contained in:
@@ -2,12 +2,9 @@
|
||||
|
||||
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
|
||||
{
|
||||
@@ -27,147 +24,42 @@ class SearchController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
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([
|
||||
return response()->json(
|
||||
$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);
|
||||
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 {
|
||||
try {
|
||||
$this->client->collections['users']->delete();
|
||||
} catch (\Exception $e) {
|
||||
// ignore si la collection n'existe pas
|
||||
}
|
||||
|
||||
|
||||
$this->createUsersCollection();
|
||||
|
||||
$this->indexUsers();
|
||||
$query = $request->input('query', '');
|
||||
|
||||
$searchResults = $this->client->collections['users']->documents->search([
|
||||
return response()->json(
|
||||
$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);
|
||||
return response()->json([
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user