1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Scout Extended. |
7
|
|
|
* |
8
|
|
|
* (c) Algolia Team <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Algolia\ScoutExtended\Console\Commands; |
15
|
|
|
|
16
|
|
|
use function count; |
17
|
|
|
use Illuminate\Console\Command; |
18
|
|
|
use Algolia\AlgoliaSearch\Index; |
19
|
|
|
use Algolia\ScoutExtended\Algolia; |
20
|
|
|
use Algolia\ScoutExtended\Helpers\SearchableFinder; |
21
|
|
|
use Algolia\ScoutExtended\Searchable\RecordsCounter; |
22
|
|
|
|
23
|
|
|
final class ReImportCommand extends Command |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
protected $signature = 'scout:reimport {searchable? : The name of the searchable}'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected $description = 'Reimport the given searchable into the search index'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private static $prefix = 'temp'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
1 |
|
public function handle( |
44
|
|
|
Algolia $algolia, |
45
|
|
|
SearchableFinder $searchableModelsFinder, |
46
|
|
|
RecordsCounter $recordsCounter |
|
|
|
|
47
|
|
|
): void { |
48
|
1 |
|
$searchables = $searchableModelsFinder->fromCommand($this); |
49
|
|
|
|
50
|
1 |
|
$config = config(); |
51
|
|
|
|
52
|
1 |
|
$scoutPrefix = $config->get('scout.prefix'); |
53
|
1 |
|
$scoutSynchronous = $config->get('scout.synchronous', false); |
54
|
|
|
|
55
|
1 |
|
$this->output->text('🔎 Importing: <info>['.implode(',', $searchables).']</info>'); |
56
|
1 |
|
$this->output->newLine(); |
57
|
1 |
|
$this->output->progressStart(count($searchables) * 3); |
58
|
|
|
|
59
|
1 |
|
foreach ($searchables as $searchable) { |
60
|
1 |
|
$index = $algolia->index($searchable); |
61
|
1 |
|
$temporaryName = $this->getTemporaryIndexName($index); |
62
|
|
|
|
63
|
1 |
|
tap($this->output)->progressAdvance()->text("Creating temporary index <info>{$temporaryName}</info>"); |
64
|
|
|
|
65
|
1 |
|
$algolia->client()->copyIndex($index->getIndexName(), $temporaryName, [ |
66
|
1 |
|
'scope' => [ |
67
|
|
|
'settings', |
68
|
|
|
'synonyms', |
69
|
|
|
'rules', |
70
|
|
|
], |
71
|
1 |
|
])->wait(); |
72
|
|
|
|
73
|
1 |
|
tap($this->output)->progressAdvance()->text("Importing records to index <info>{$temporaryName}</info>"); |
74
|
|
|
|
75
|
|
|
try { |
76
|
1 |
|
$config->set('scout.prefix', self::$prefix.'_'.$scoutPrefix); |
77
|
1 |
|
$config->set('scout.synchronous', true); |
78
|
1 |
|
$searchable::makeAllSearchable(); |
79
|
1 |
|
} finally { |
80
|
1 |
|
$config->set('scout.prefix', $scoutPrefix); |
81
|
1 |
|
$config->set('scout.synchronous', $scoutSynchronous); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
tap($this->output)->progressAdvance()->text("Replacing index <info>{$index->getIndexName()}</info> by index <info>{$temporaryName}</info>"); |
85
|
|
|
|
86
|
1 |
|
$algolia->client()->moveIndex($temporaryName, $index->getIndexName())->wait(); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
tap($this->output)->success('All ['.implode(',', $searchables).'] records have been imported')->newLine(); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get a temporary index name. |
94
|
|
|
* |
95
|
|
|
* @param \Algolia\AlgoliaSearch\Index $index |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
1 |
|
private function getTemporaryIndexName(Index $index): string |
100
|
|
|
{ |
101
|
1 |
|
return self::$prefix.'_'.$index->getIndexName(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.