Last active
June 17, 2023 13:54
-
-
Save Buzovskiy/f1508a2f20776f8d511db50f3a08aa2f to your computer and use it in GitHub Desktop.
Remove all documents in elasticsearch index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$index = 'menu_cat_list_by_type_ru'; | |
$flag = True; | |
$ids_to_exclude = array(); | |
while ($flag) { | |
// Удаляем документы в индексе, пока они не закончатся | |
$params = [ | |
'index' => $index, | |
'from' => 0, | |
'size' => 1, | |
]; | |
$params['body']['query']['bool']['must_not']['ids']['values'] = $ids_to_exclude; | |
try { | |
$response = $this->elasticsearch->client->search($params); | |
if (empty($response['hits']['hits'])){ | |
$flag = False; | |
} | |
foreach ($response['hits']['hits'] as $document) { | |
$this->elasticsearch->client->delete([ | |
'index' => $index, | |
'id' => $document['_id'], | |
]); | |
$ids_to_exclude[] = $document['_id']; | |
} | |
} catch (ClientResponseException $e) { | |
if ($e->getCode() === 404) { | |
// echo 'the document does not exist'; | |
} | |
$flag = False; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment