Skip to content

Commit 8360ba4

Browse files
Merge pull request #15 from ginkelsoft-development/fix/elasticsearch-delete-implementation
fix: implement actual document deletion in Elasticsearch
2 parents cb55a50 + 4237d91 commit 8360ba4

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/Services/ElasticsearchService.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,19 @@ public function search(string $index, array $query): array
8888

8989
return $response->json('hits.hits', []);
9090
}
91+
92+
/**
93+
* Delete documents matching a query from an Elasticsearch index.
94+
*
95+
* @param string $index The Elasticsearch index name.
96+
* @param array<string, mixed> $query The Elasticsearch query body.
97+
* @return bool True if successful, false otherwise.
98+
*/
99+
public function deleteByQuery(string $index, array $query): bool
100+
{
101+
$url = "{$this->host}/{$index}/_delete_by_query";
102+
$response = Http::post($url, $query);
103+
104+
return $response->successful();
105+
}
91106
}

src/Traits/HasEncryptedSearchIndex.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,10 @@ protected function syncToElasticsearch(array $rows): void
177177
}
178178

179179
/**
180-
* Remove this models tokens from the configured Elasticsearch index.
180+
* Remove this model's tokens from the configured Elasticsearch index.
181181
*
182-
* Uses a boolean query to match documents by model_type and model_id.
182+
* Uses delete-by-query to efficiently remove all documents matching
183+
* the model_type and model_id.
183184
*
184185
* @return void
185186
*/
@@ -200,8 +201,7 @@ protected function removeFromElasticsearch(): void
200201
];
201202

202203
try {
203-
$service->search($index, $query);
204-
// Optional: replace with Elasticsearch delete-by-query API for optimization
204+
$service->deleteByQuery($index, $query);
205205
} catch (\Throwable $e) {
206206
logger()->warning("Failed to remove Elasticsearch docs for model {$this->getKey()}: {$e->getMessage()}");
207207
}

0 commit comments

Comments
 (0)