1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Tasks; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use Firesphere\SolrSearch\Factories\DocumentFactory; |
8
|
|
|
use Firesphere\SolrSearch\Indexes\BaseIndex; |
9
|
|
|
use Firesphere\SolrSearch\Services\SolrCoreService; |
10
|
|
|
use Firesphere\SolrSearch\Traits\LoggerTrait; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
use SilverStripe\Control\Director; |
13
|
|
|
use SilverStripe\Control\HTTPRequest; |
14
|
|
|
use SilverStripe\Core\Injector\Injector; |
15
|
|
|
use SilverStripe\Dev\BuildTask; |
16
|
|
|
use SilverStripe\ORM\ArrayList; |
17
|
|
|
use SilverStripe\ORM\DataList; |
18
|
|
|
use SilverStripe\ORM\DataObject; |
19
|
|
|
use SilverStripe\Versioned\Versioned; |
20
|
|
|
|
21
|
|
|
class SolrIndexTask extends BuildTask |
22
|
|
|
{ |
23
|
|
|
use LoggerTrait; |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private static $segment = 'SolrIndexTask'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $title = 'Solr Index update'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $description = 'Add or update documents to an existing Solr core.'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $debug = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var SolrCoreService |
46
|
|
|
*/ |
47
|
|
|
protected $service; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* SolrIndexTask constructor. Sets up the document factory |
51
|
|
|
*/ |
52
|
|
|
public function __construct() |
53
|
|
|
{ |
54
|
|
|
parent::__construct(); |
55
|
|
|
// Only index live items. |
56
|
|
|
// The old FTS module also indexed Draft items. This is unnecessary |
57
|
|
|
Versioned::set_reading_mode(Versioned::DRAFT . '.' . Versioned::LIVE); |
58
|
|
|
$this->setService(Injector::inst()->get(SolrCoreService::class)); |
59
|
|
|
$this->setLogger(Injector::inst()->get(LoggerInterface::class)); |
60
|
|
|
$this->setDebug(Director::isDev() || Director::is_cli()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param SolrCoreService $service |
65
|
|
|
* @return SolrIndexTask |
66
|
|
|
*/ |
67
|
|
|
public function setService(SolrCoreService $service): SolrIndexTask |
68
|
|
|
{ |
69
|
|
|
$this->service = $service; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param bool $debug |
76
|
|
|
* @return SolrIndexTask |
77
|
|
|
*/ |
78
|
|
|
public function setDebug(bool $debug): SolrIndexTask |
79
|
|
|
{ |
80
|
|
|
$this->debug = $debug; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Implement this method in the task subclass to |
87
|
|
|
* execute via the TaskRunner |
88
|
|
|
* |
89
|
|
|
* @param HTTPRequest $request |
90
|
|
|
* @return int|bool |
91
|
|
|
* @throws Exception |
92
|
|
|
* @todo defer to background because it may run out of memory |
93
|
|
|
*/ |
94
|
|
|
public function run($request) |
95
|
|
|
{ |
96
|
|
|
$startTime = time(); |
97
|
|
|
[$vars, $group, $isGroup] = $this->taskSetup($request); |
98
|
|
|
$groups = 0; |
99
|
|
|
$indexes = $this->service->getValidIndexes($request->getVar('index')); |
100
|
|
|
|
101
|
|
|
foreach ($indexes as $indexName) { |
102
|
|
|
/** @var BaseIndex $index */ |
103
|
|
|
$index = Injector::inst()->get($indexName, false); |
104
|
|
|
|
105
|
|
|
$indexClasses = $index->getClasses(); |
106
|
|
|
$classes = $this->getClasses($vars, $indexClasses); |
107
|
|
|
if (!count($classes)) { |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$vars = $this->clearIndex($vars, $indexName, $index); |
112
|
|
|
|
113
|
|
|
$groups = $this->indexClassForIndex($classes, $isGroup, $index, $group); |
114
|
|
|
} |
115
|
|
|
$this->getLogger()->info( |
116
|
|
|
sprintf('It took me %d seconds to do all the indexing%s', (time() - $startTime), PHP_EOL) |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
return $groups; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param HTTPRequest $request |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
protected function taskSetup($request): array |
127
|
|
|
{ |
128
|
|
|
$vars = $request->getVars(); |
129
|
|
|
$this->debug = $this->debug || isset($vars['debug']); |
130
|
|
|
$group = $vars['group'] ?? 0; |
131
|
|
|
$start = $vars['start'] ?? 0; |
132
|
|
|
$group = ($start > $group) ? $start : $group; |
133
|
|
|
$isGroup = isset($vars['group']); |
134
|
|
|
|
135
|
|
|
return [$vars, $group, $isGroup]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $vars |
140
|
|
|
* @param array $classes |
141
|
|
|
* @return bool|array |
142
|
|
|
*/ |
143
|
|
|
protected function getClasses($vars, array $classes): array |
144
|
|
|
{ |
145
|
|
|
if (isset($vars['class'])) { |
146
|
|
|
return array_intersect($classes, [$vars['class']]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $classes; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param $vars |
154
|
|
|
* @param $indexName |
155
|
|
|
* @param BaseIndex $index |
156
|
|
|
* @return mixed |
157
|
|
|
* @throws Exception |
158
|
|
|
*/ |
159
|
|
|
protected function clearIndex($vars, $indexName, BaseIndex $index) |
160
|
|
|
{ |
161
|
|
|
if (!empty($vars['clear'])) { |
162
|
|
|
$this->getLogger()->info(sprintf('Clearing index %s', $indexName)); |
163
|
|
|
$this->service->doManipulate(ArrayList::create([]), SolrCoreService::DELETE_TYPE_ALL, $index); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $vars; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $classes |
171
|
|
|
* @param $isGroup |
172
|
|
|
* @param BaseIndex $index |
173
|
|
|
* @param $group |
174
|
|
|
* @return int |
175
|
|
|
* @throws Exception |
176
|
|
|
*/ |
177
|
|
|
protected function indexClassForIndex($classes, $isGroup, BaseIndex $index, $group): int |
178
|
|
|
{ |
179
|
|
|
$groups = 0; |
180
|
|
|
foreach ($classes as $class) { |
181
|
|
|
$groups = $this->indexClass($isGroup, $class, $index, $group); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $groups; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param bool $isGroup |
189
|
|
|
* @param string $class |
190
|
|
|
* @param BaseIndex $index |
191
|
|
|
* @param int $group |
192
|
|
|
* @return int |
193
|
|
|
* @throws Exception |
194
|
|
|
*/ |
195
|
|
|
private function indexClass($isGroup, $class, BaseIndex $index, int $group): int |
196
|
|
|
{ |
197
|
|
|
$this->getLogger()->info(sprintf('Indexing %s for %s', $class, $index->getIndexName()), []); |
198
|
|
|
|
199
|
|
|
$batchLength = DocumentFactory::config()->get('batchLength'); |
200
|
|
|
$groups = (int)ceil($class::get()->count() / $batchLength); |
201
|
|
|
$groups = $isGroup ? $group : $groups; |
202
|
|
|
while ($group <= $groups) { // Run from oldest to newest |
203
|
|
|
try { |
204
|
|
|
$this->doReindex($group, $class, $batchLength, $index); |
205
|
|
|
} catch (Exception $e) { |
206
|
|
|
$this->getLogger()->error($e->getMessage()); |
207
|
|
|
$group++; |
208
|
|
|
continue; |
209
|
|
|
} |
210
|
|
|
$group++; |
211
|
|
|
$this->getLogger()->info(sprintf('Indexed group %s', $group)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $groups; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param int $group |
219
|
|
|
* @param string $class |
220
|
|
|
* @param int $batchLength |
221
|
|
|
* @param BaseIndex $index |
222
|
|
|
* @throws Exception |
223
|
|
|
*/ |
224
|
|
|
private function doReindex($group, $class, $batchLength, BaseIndex $index): void |
225
|
|
|
{ |
226
|
|
|
// Generate filtered list of local records |
227
|
|
|
$baseClass = DataObject::getSchema()->baseDataClass($class); |
228
|
|
|
$client = $index->getClient(); |
229
|
|
|
/** @var DataList|DataObject[] $items */ |
230
|
|
|
$items = DataObject::get($baseClass) |
231
|
|
|
->sort('ID ASC') |
232
|
|
|
->limit($batchLength, ($group * $batchLength)); |
233
|
|
|
$update = $client->createUpdate(); |
234
|
|
|
if ($items->count()) { |
235
|
|
|
$this->service->setInDebugMode($this->debug); |
236
|
|
|
$this->service->updateIndex($index, $items, $update); |
237
|
|
|
$update->addCommit(); |
238
|
|
|
$client->update($update); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|