|
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\SearchIndex; |
|
19
|
|
|
use Algolia\AlgoliaSearch\SearchClient; |
|
20
|
|
|
use Algolia\ScoutExtended\Helpers\SearchableFinder; |
|
21
|
|
|
use Algolia\AlgoliaSearch\Exceptions\NotFoundException; |
|
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
|
|
|
SearchClient $client, |
|
45
|
|
|
SearchableFinder $searchableModelsFinder |
|
46
|
|
|
): void { |
|
47
|
1 |
|
$searchables = $searchableModelsFinder->fromCommand($this); |
|
48
|
|
|
|
|
49
|
1 |
|
$config = config(); |
|
50
|
|
|
|
|
51
|
1 |
|
$scoutPrefix = $config->get('scout.prefix'); |
|
52
|
|
|
|
|
53
|
1 |
|
$this->output->text('🔎 Importing: <info>['.implode(',', $searchables).']</info>'); |
|
54
|
1 |
|
$this->output->newLine(); |
|
55
|
1 |
|
$this->output->progressStart(count($searchables) * 3); |
|
56
|
|
|
|
|
57
|
1 |
|
foreach ($searchables as $searchable) { |
|
58
|
1 |
|
$index = $client->initIndex((new $searchable)->searchableAs()); |
|
59
|
1 |
|
$temporaryName = $this->getTemporaryIndexName($index); |
|
60
|
|
|
|
|
61
|
1 |
|
tap($this->output)->progressAdvance()->text("Creating temporary index <info>{$temporaryName}</info>"); |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
1 |
|
$searchable::search()->get(); |
|
65
|
|
|
|
|
66
|
1 |
|
$client->copyIndex($index->getIndexName(), $temporaryName, [ |
|
67
|
1 |
|
'scope' => [ |
|
68
|
|
|
'settings', |
|
69
|
|
|
'synonyms', |
|
70
|
|
|
'rules', |
|
71
|
|
|
], |
|
72
|
1 |
|
])->wait(); |
|
73
|
|
|
} catch (NotFoundException $e) { |
|
74
|
|
|
// .. |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
tap($this->output)->progressAdvance()->text("Importing records to index <info>{$temporaryName}</info>"); |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
1 |
|
$config->set('scout.prefix', self::$prefix.'_'.$scoutPrefix); |
|
81
|
1 |
|
$searchable::makeAllSearchable(); |
|
82
|
1 |
|
} finally { |
|
83
|
1 |
|
$config->set('scout.prefix', $scoutPrefix); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
tap($this->output)->progressAdvance() |
|
87
|
1 |
|
->text("Replacing index <info>{$index->getIndexName()}</info> by index <info>{$temporaryName}</info>"); |
|
88
|
|
|
|
|
89
|
1 |
|
$temporaryIndex = $client->initIndex($temporaryName); |
|
90
|
|
|
|
|
91
|
|
|
try { |
|
92
|
1 |
|
$temporaryIndex->getSettings(); |
|
93
|
|
|
|
|
94
|
1 |
|
$response = $client->moveIndex($temporaryName, $index->getIndexName()); |
|
95
|
|
|
|
|
96
|
1 |
|
if ($config->get('scout.synchronous', false)) { |
|
97
|
1 |
|
$response->wait(); |
|
98
|
|
|
} |
|
99
|
|
|
} catch (NotFoundException $e) { |
|
100
|
1 |
|
$index->setSettings(['attributesForFaceting' => null])->wait(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
tap($this->output)->success('All ['.implode(',', $searchables).'] records have been imported')->newLine(); |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get a temporary index name. |
|
109
|
|
|
* |
|
110
|
|
|
* @param \Algolia\AlgoliaSearch\SearchIndex $index |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
1 |
|
private function getTemporaryIndexName(SearchIndex $index): string |
|
115
|
|
|
{ |
|
116
|
1 |
|
return self::$prefix.'_'.$index->getIndexName(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|