0

I'm trying to make a piece of code that will be responsible for deleting an indexed file from the elasticsearch index, I pass with the indexed file md5(file name), to the id value. It is necessary to make sure that when deleting a file from a folder, the deletion code by id (md5) is executed. I wrote part of the script, but I understand that it is incorrect because if there is no file, then md5 will not be created, please help)

<?php

$path = __DIR__ . ("/uploads/Арне.pptx");
$filename = basename($path);
$md5 = md5_file($path);
print($md5); //Передаю переменную md5 в 'id' индексации elasticsearch 

//echo 'MD5-хеш файла ' . $filename . ': ' . md5_file($path);

if (empty(md5_file($path))) {//Провожу проверку, если md5 не        совпадает с именем файла в папке
    $params = [ //Произвожу удаление индекса из ES
        'index' => 'bower',
        'id' => $md5
    ];
    $respose = $client->delete($params);
} else {
    echo '' .  '  есть, файл существует';
}
hkulekci
  • 1,894
  • 15
  • 27
SplendX
  • 1
  • 1

2 Answers2

0

You need to use the delete index method instead of deleting documents:

$params = ['index' => 'bower_' . $md5];
$response = $client->indices()->delete($params);
hkulekci
  • 1,894
  • 15
  • 27
  • You suggest deleting by index name, would that be correct? – SplendX Feb 12 '23 at 02:23
  • According to your question, you are looking for the files on the filesystem, and you want to delete the index according to file existence. So, you need to delete the index, not the documents. For deleting indices, you need to use `indices()` functions. – hkulekci Feb 12 '23 at 15:30
0

You can close the topic, the issue is resolved. Solved the problem: Iterating over the array in the uploads folder, and iterating over the elasticsearch array id, then used array_diff, compared the arrays to exclude files that are not in the uploads folder, but they were in the elasticsearch index

SplendX
  • 1
  • 1