1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Colligator\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Colligator\Document; |
6
|
|
|
use Colligator\Search\DocumentsIndex; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
|
9
|
|
|
class Reindex extends Command |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The name and signature of the console command. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $signature = 'colligator:reindex'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command description. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'Re-build ElasticSearch index.'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Execute the console command. |
27
|
|
|
* |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
|
|
public function handle(DocumentsIndex $docIndex) |
31
|
|
|
{ |
32
|
|
|
$t0 = microtime(true); |
33
|
|
|
|
34
|
|
|
\Log::info('[ReindexJob] Starting job.'); |
35
|
|
|
|
36
|
|
|
$this->info(''); |
37
|
|
|
$this->info(' Rebuilding the Elasticsearch index will take some time.'); |
38
|
|
|
$this->info(''); |
39
|
|
|
|
40
|
|
|
//$docIndex->dropVersion(); |
41
|
|
|
$oldVersion = $docIndex->getCurrentVersion(); |
42
|
|
|
$newVersion = $oldVersion + 1; |
43
|
|
|
$this->comment(' Old version: ' . $oldVersion . ', new version: ' . $newVersion); |
44
|
|
|
|
45
|
|
|
if ($docIndex->versionExists($newVersion)) { |
46
|
|
|
$this->comment(' New version already existed, probably from a crashed job. Removing.'); |
47
|
|
|
$docIndex->dropVersion($newVersion); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->comment(' Creating new index'); |
51
|
|
|
$docIndex->createVersion($newVersion); |
52
|
|
|
|
53
|
|
|
$this->comment(sprintf(' [%03d] Building entity usage cache', microtime(true) - $t0)); |
54
|
|
|
$docIndex->buildCompleteUsageCache(); |
55
|
|
|
|
56
|
|
|
$this->comment(sprintf(' [%03d] Filling new index...', microtime(true) - $t0)); |
57
|
|
|
|
58
|
|
|
$docCount = Document::count(); |
59
|
|
|
$this->output->progressStart($docCount); |
60
|
|
|
|
61
|
|
|
Document::with('subjects', 'genres', 'cover')->chunk(1000, function ($docs) use ($docIndex, $newVersion) { |
|
|
|
|
62
|
|
|
foreach ($docs as $doc) { |
63
|
|
|
$docIndex->index($doc, $newVersion); |
64
|
|
|
$this->output->progressAdvance(); |
65
|
|
|
} |
66
|
|
|
}); |
67
|
|
|
$this->output->progressFinish(); |
68
|
|
|
|
69
|
|
|
$this->comment(sprintf(' [%03d] Swapping indices', microtime(true) - $t0)); |
70
|
|
|
$docIndex->activateVersion($newVersion); |
71
|
|
|
|
72
|
|
|
$this->comment(sprintf(' [%03d] Dropping old index', microtime(true) - $t0)); |
73
|
|
|
$docIndex->dropVersion($oldVersion); |
74
|
|
|
|
75
|
|
|
$dt = microtime(true) - $t0; |
76
|
|
|
$this->info(' Completed in ' . round($dt) . ' seconds.'); |
77
|
|
|
\Log::info('[ReindexJob] Completed in ' . round($dt) . ' seconds.'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: