public static function deleteAll($condition = [])
{
$primaryKeys = static::primaryKeysByCondition($condition);
if (empty($primaryKeys)) {
return 0;
}
$bulkCommand = static::getDb()->createBulkCommand(["index" => static::index(), "type" => static::type()]);
foreach ($primaryKeys as $pk) {
$bulkCommand->addDeleteAction($pk);
}
$response = $bulkCommand->execute();
$n = 0;
$errors = [];
foreach ($response['items'] as $item) {
if (isset($item['delete']['status']) && $item['delete']['status'] == 200) {
if (isset($item['delete']['found']) && $item['delete']['found']) {
$n++;
}
} else {
$errors[] = $item['delete'];
}
}
if (!empty($errors) || isset($response['errors']) && $response['errors']) {
throw new Exception(__METHOD__ . ' failed deleting records.', $errors);
}
return $n;
}