1 | <?php |
||
2 | |||
3 | namespace Firesphere\ElasticSearch\Tasks; |
||
4 | |||
5 | use Exception; |
||
6 | use Firesphere\ElasticSearch\Services\ElasticCoreService; |
||
7 | use Psr\Container\NotFoundExceptionInterface; |
||
8 | use SilverStripe\CMS\Model\SiteTree; |
||
9 | use SilverStripe\Core\Injector\Injector; |
||
10 | use SilverStripe\Dev\BuildTask; |
||
11 | |||
12 | /** |
||
13 | * Example task to empty an Elastic index |
||
14 | */ |
||
15 | class BulkDeleteTask extends BuildTask |
||
16 | { |
||
17 | /** |
||
18 | * URLSegment of this task |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | private static $segment = 'Truncastic'; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
23 | /** |
||
24 | * My name |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $title = 'Truncate elastic core'; |
||
29 | /** |
||
30 | * What do I do? |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $description = 'Try to remove everything from an Elastic core, to start fresh.'; |
||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $enabled = false; |
||
39 | |||
40 | /** |
||
41 | * @param $request |
||
42 | * @return void |
||
43 | * @throws NotFoundExceptionInterface |
||
44 | */ |
||
45 | public function run($request) |
||
46 | { |
||
47 | $service = Injector::inst()->get(ElasticCoreService::class); |
||
48 | $pages = SiteTree::get(); |
||
49 | foreach ($pages as $page) { |
||
50 | $q = [ |
||
51 | 'index' => 'search-health', |
||
52 | 'body' => [ |
||
53 | 'query' => [ |
||
54 | 'match' => [ |
||
55 | 'ObjectID' => $page->ID |
||
56 | ] |
||
57 | ] |
||
58 | ] |
||
59 | ]; |
||
60 | try { |
||
61 | $service->getClient()->deleteByQuery($q); |
||
62 | } catch (Exception $e) { |
||
63 | // noop, just continue |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 |