Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Buzovskiy/f1508a2f20776f8d511db50f3a08aa2f to your computer and use it in GitHub Desktop.
Save Buzovskiy/f1508a2f20776f8d511db50f3a08aa2f to your computer and use it in GitHub Desktop.
Remove all documents in elasticsearch index
<?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