1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\LaravelZendSearch\Laravel\Console; |
4
|
|
|
|
5
|
|
|
use BestServedCold\LaravelZendSearch\Laravel\Store; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
9
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
10
|
|
|
use Illuminate\Support\Facades\App; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class RebuildCommand |
14
|
|
|
* @package BestServedCold\LaravelZendSearch\Laravel\Console |
15
|
|
|
*/ |
16
|
|
|
class Rebuild extends Command |
17
|
|
|
{ |
18
|
|
|
protected $name = 'search:rebuild'; |
19
|
|
|
protected $description = 'Rebuild the search index'; |
20
|
|
|
|
21
|
|
|
private $models = []; |
22
|
|
|
|
23
|
|
|
public function getModels() |
24
|
|
|
{ |
25
|
|
|
foreach (get_declared_classes() as $class) { |
26
|
|
|
if (is_subclass_of($class, Model::class) && method_exists($class, 'searchFields')) { |
|
|
|
|
27
|
|
|
$this->models[] = $class; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function handle() |
33
|
|
|
{ |
34
|
|
|
$this->getModels(); |
35
|
|
|
|
36
|
|
|
if (!$this->option('verbose')) { |
37
|
|
|
$this->output = new NullOutput; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->call('search:destroy', $this->getArguments()); |
41
|
|
|
|
42
|
|
|
$store = App::make(Store::class); |
43
|
|
|
|
44
|
|
|
if (empty($this->models)) { |
45
|
|
|
$this->error('No models configured for search.'); |
46
|
|
|
} else { |
47
|
|
|
foreach ($this->models as $model) { |
48
|
|
|
$object = App::make($model); |
49
|
|
|
|
50
|
|
|
$this->info('Creating index for model [' . $model . ']'); |
51
|
|
|
|
52
|
|
|
$count = $object->count(); |
53
|
|
|
|
54
|
|
|
if ($object->count() === 0) { |
55
|
|
|
$this->comment('No records for model [' . $model . ']'); |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$progress = new ProgressBar($this->getOutput(), $count); |
60
|
|
|
$progress->start(); |
61
|
|
|
|
62
|
|
|
$object->chunk(1000, function($chunk) use ($progress, $store) { |
63
|
|
|
foreach ($chunk as $record) { |
64
|
|
|
$store->insertModel($record, false); |
65
|
|
|
$progress->advance(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$progress->finish(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->call('search:optimise', $this->getArguments()); |
74
|
|
|
|
75
|
|
|
$this->info(PHP_EOL . 'Search engine rebuild complete.'); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|