2022-12-04 19:53:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Testing;
|
|
|
|
|
2023-02-02 23:37:37 +00:00
|
|
|
use Typesense\LaravelTypesense\Typesense;
|
|
|
|
|
2023-02-13 21:15:07 +00:00
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
2022-12-04 19:53:27 +00:00
|
|
|
trait ScoutFlush
|
|
|
|
{
|
2022-12-04 20:18:12 +00:00
|
|
|
protected array $searchIndexModelCleanupList = [
|
2023-02-02 23:37:37 +00:00
|
|
|
"App\\Anime",
|
|
|
|
"App\\Manga",
|
|
|
|
"App\\Character",
|
|
|
|
"App\\GenreAnime",
|
|
|
|
"App\\GenreManga",
|
|
|
|
"App\\Person",
|
|
|
|
"App\\Club",
|
2023-04-22 02:30:32 +05:00
|
|
|
"App\\Magazine",
|
2024-01-31 19:11:27 +00:00
|
|
|
"App\\Producers",
|
|
|
|
"App\\Profile",
|
2022-12-04 20:18:12 +00:00
|
|
|
];
|
|
|
|
|
2022-12-04 19:53:27 +00:00
|
|
|
public function runScoutFlush(): void
|
|
|
|
{
|
2023-02-02 23:37:37 +00:00
|
|
|
if (config("scout.driver") === "typesense") {
|
|
|
|
/**
|
|
|
|
* @var Typesense $typeSenseClient
|
|
|
|
*/
|
|
|
|
$typeSenseClient = app(Typesense::class);
|
|
|
|
// more optimized approach for quicker tests.
|
|
|
|
foreach ($this->searchIndexModelCleanupList as $model) {
|
|
|
|
$modelInstance = new $model;
|
|
|
|
$collection = $typeSenseClient->getCollectionIndex($modelInstance);
|
|
|
|
// we count items by exporting
|
|
|
|
$items = $collection->documents->export();
|
|
|
|
if (strlen($items) > 1) {
|
|
|
|
$typeSenseClient->deleteDocuments($collection, [
|
|
|
|
"filter_by" => "mal_id:>0",
|
|
|
|
"batch_size" => 500
|
|
|
|
]);
|
2024-01-31 19:31:15 +00:00
|
|
|
|
|
|
|
$typeSenseClient->deleteCollection($modelInstance->searchableAs());
|
2023-02-02 23:37:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach ($this->searchIndexModelCleanupList as $model) {
|
|
|
|
$this->artisan("scout:flush", ["model" => $model]);
|
|
|
|
}
|
2022-12-04 20:11:35 +00:00
|
|
|
}
|
2022-12-04 19:53:27 +00:00
|
|
|
}
|
|
|
|
}
|