From 0fe6650be1760dd0dc9e95c9a7b41649ae723aed Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Fri, 8 Nov 2024 09:44:20 +0000 Subject: [PATCH] Add method to delete multiple by id --- src/Engines/AbstractEngine.php | 5 +++++ src/Engines/TypesenseEngine.php | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Engines/AbstractEngine.php b/src/Engines/AbstractEngine.php index 7606695..786a311 100644 --- a/src/Engines/AbstractEngine.php +++ b/src/Engines/AbstractEngine.php @@ -181,6 +181,11 @@ protected function sortByIsValid(): bool return $field && ($field['sort'] ?? false); } + + public function deleteByIds(Collection $ids): array + { + return []; + } abstract public function get(): mixed; diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index 8f43527..6511b60 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -3,8 +3,10 @@ namespace Lunar\Search\Engines; use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; use Laravel\Scout\EngineManager; +use Lunar\Models\Product; use Lunar\Search\Data\SearchFacet; use Lunar\Search\Data\SearchHit; use Lunar\Search\Data\SearchResults; @@ -134,7 +136,7 @@ protected function buildSearch(array $options): array $requests = []; $facets = $this->getFacetConfig(); - + foreach ($searchQueries as $searchQuery) { $filters = collect($options['filter_by']); @@ -195,6 +197,15 @@ protected function buildSearch(array $options): array return $requests; } + public function deleteByIds(Collection $ids): array + { + $typesense = app(EngineManager::class)->engine('typesense'); + $index = (new Product)->searchableAs(); + return $typesense->getCollections()[$index]->documents->delete([ + 'filter_by' => 'id: ['.$ids->join(',').']', + ]); + } + protected function getFieldConfig(): array {