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\ScoutExtended\Algolia; |
19
|
|
|
use Algolia\AlgoliaSearch\SearchIndex; |
20
|
|
|
use Algolia\ScoutExtended\Helpers\SearchableFinder; |
21
|
|
|
use Algolia\ScoutExtended\Searchable\RecordsCounter; |
22
|
|
|
use Algolia\AlgoliaSearch\Exceptions\NotFoundException; |
23
|
|
|
|
24
|
|
|
final class ReImportCommand extends Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
protected $signature = 'scout:reimport {searchable? : The name of the searchable}'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
protected $description = 'Reimport the given searchable into the search index'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private static $prefix = 'temp'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
1 |
|
public function handle( |
45
|
|
|
Algolia $algolia, |
46
|
|
|
SearchableFinder $searchableModelsFinder |
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
|
|
|
try { |
66
|
1 |
|
$searchable::search()->get(); |
67
|
|
|
|
68
|
1 |
|
$index->copyTo($temporaryName, [ |
69
|
1 |
|
'scope' => [ |
70
|
|
|
'settings', |
71
|
|
|
'synonyms', |
72
|
|
|
'rules', |
73
|
|
|
], |
74
|
1 |
|
])->wait(); |
75
|
|
|
} catch (NotFoundException $e) { |
76
|
|
|
// .. |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
tap($this->output)->progressAdvance()->text("Importing records to index <info>{$temporaryName}</info>"); |
80
|
|
|
|
81
|
|
|
try { |
82
|
1 |
|
$config->set('scout.prefix', self::$prefix.'_'.$scoutPrefix); |
83
|
1 |
|
$searchable::makeAllSearchable(); |
84
|
1 |
|
} finally { |
85
|
1 |
|
$config->set('scout.prefix', $scoutPrefix); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
tap($this->output)->progressAdvance()->text("Replacing index <info>{$index->getIndexName()}</info> by index <info>{$temporaryName}</info>"); |
89
|
|
|
|
90
|
1 |
|
$temporaryIndex = $algolia->client() |
91
|
1 |
|
->initIndex($temporaryName); |
92
|
|
|
|
93
|
|
|
try { |
94
|
1 |
|
$temporaryIndex->getSettings(); |
95
|
|
|
|
96
|
1 |
|
$response = $temporaryIndex->moveTo($index->getIndexName()); |
97
|
|
|
|
98
|
1 |
|
if ($scoutSynchronous) { |
99
|
1 |
|
$response->wait(); |
100
|
|
|
} |
101
|
|
|
} catch (NotFoundException $e) { |
102
|
1 |
|
$index->setSettings(['attributesForFaceting' => null])->wait(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
tap($this->output)->success('All ['.implode(',', $searchables).'] records have been imported')->newLine(); |
107
|
1 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get a temporary index name. |
111
|
|
|
* |
112
|
|
|
* @param \Algolia\AlgoliaSearch\SearchIndex $index |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
1 |
|
private function getTemporaryIndexName(SearchIndex $index): string |
117
|
|
|
{ |
118
|
1 |
|
return self::$prefix.'_'.$index->getIndexName(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|