-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchIndexer.php
533 lines (489 loc) · 18.4 KB
/
SearchIndexer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php
namespace dmstr\activeRecordSearch\components;
use dmstr\activeRecordSearch\models\Search;
use dmstr\activeRecordSearch\models\SearchGroup;
use Yii;
use yii\base\Component;
use yii\db\ActiveRecord;
use yii\helpers\Json;
use yii\helpers\VarDumper;
/**
* Search IndexController
* @package dmstr\activeRecordSearch\commands
* Author: Jens Giessmann <[email protected]>
*
* Example Config:
*
* $config['components']['searchIndexer'] = [
* 'class' => \dmstr\activeRecordSearch\components\SearchIndexer::class,
* 'languages' => function() {
* return project\components\CountryHelper::activeLanguages();
* },
* 'searchItems' => [
* 'products' => [
* 'model_class' => Product::class,
* 'find_method' => function ($item) {
* return $item['model_class']::find()->andWhere(['archived' => 0]);
* },
* 'route' => '/frontend/product/detail',
* 'attributes' => [
* 'name',
* 'frame',
* 'tags' => ['name'],
* ],
* 'url_params' => ['productId' => 'id', 'productName' => 'name'],
* 'link_text' => function ($item) {
* $parts = [];
* if ($item->getClassificationTag()) {
* $cTag = $item->getClassificationTag();
* $cTag !== null && $parts[] = $cTag->name;
* }
* $parts[] = $item->name;
* return implode(': ', array_filter($parts));
* },
* 'group' => 'Products',
* ],
* 'product-archive' => [
* 'model_class' => Product::class,
* 'find_method' => function ($item) {
* return $item['model_class']::find()->andWhere(['archived' => 1]);
* },
* 'route' => '/frontend/product/detail',
* 'attributes' => [
* 'name',
* 'frame',
* 'tags' => ['name'],
* ],
* 'url_params' => ['productId' => 'id', 'productName' => 'name'],
* 'link_text' => function ($item) {
* $parts = [];
* if ($item->getClassificationTag()) {
* $cTag = $item->getClassificationTag();
* $cTag !== null && $parts[] = $cTag->name;
* }
* $parts[] = $item->name;
* return implode(': ', array_filter($parts));
* },
* 'group' => 'Products Archive',
* ],
* 'news' => [
* 'model_class' => PublicationItem::class,
* 'route' => '/publication/default/detail',
* 'attributes' => [
* 'title',
* 'content' => function ($item) {
* $content = Json::decode($item->content_widget_json);
* return html_entity_decode(strip_tags($content['text_html']));
* }
* ],
* 'url_params' => [
* 'itemId' => 'id',
* 'title' => function ($item) {
* return $item->title;
* }
* ],
* 'link_text' => 'title',
* 'group' => 'News',
* ],
* 'tags' => [
* 'model_class' => \project\modules\cruds\models\Tag::class,
* 'route' => '/productfinder/default/index',
* 'find_method' => function ($item) {
* // get used tagIds from finder
* $tag_ids = \project\modules\productfinder\models\Productfinder::getFacetIdList('tag_ids');
* return $item['model_class']::find()->andWhere(['id' => $tag_ids]);
* },
* 'attributes' => [
* 'name',
* ],
* 'url_params' => [
* 'mainTag' => 'id',
* 'mainTagName' => 'name',
* ],
* 'link_text' => function ($item) {
* return implode(': ', [$item->tagGroup->name, $item->name]);
* },
* 'group' => 'Product Tags',
* ],
* ],
* ];
*
*/
class SearchIndexer extends Component
{
/**
* definition what should be indexed
*
* @var
*/
public $searchItems;
/**
* array of languages for which we should index
*
* @var array
*/
public $languages = [];
/**
* fallbackLanguage (for translatable behaviour) which will be used to create new SearchGroup entries.
* if not set, we will use the first lang from the languages array.
*
* @var
*/
public $fallbackLanguage;
/**
* php mem_limit for the indexer
* @var string
*/
public $memoryLimit = '1024M';
/**
* php max_execution_time for the indexer
* @var int
*/
public $maxExecutionTime = 1800;
/**
* output some debug infos while indexing
* @var bool
*/
public $debug = false;
protected $transaction;
protected $currentLang;
/**
* array keys for the $detailInfo data struct provided by the self::update() method
* @var string
*/
protected $infoItemsTotalKey = 'items-total-count';
protected $infoItemsPerKey = 'items-per-config-key';
/**
* init system for this long running process
* - set memory_limit and max_execution_time
* - disable/remove audit LogTarget (which eats a lot of memory...)
*/
protected function sysInit()
{
ini_set('memory_limit', $this->memoryLimit);
ini_set('max_execution_time', $this->maxExecutionTime);
// unset audit logTarget due to huge memory peaks
if (isset(Yii::$app->log->targets['audit'])) {
Yii::$app->log->targets['audit'] = null;
unset(Yii::$app->log->targets['audit']);
}
}
/**
* Update search index
*/
public function update()
{
$this->sysInit();
$detailInfo = [];
$sTime = microtime(true);
$this->transaction = Yii::$app->db->beginTransaction();
Search::deleteAll();
$this->languages = $this->initLanguages();
$this->fallbackLanguage = $this->initFallbackLanguage();
$this->updateSearchItemGroupsFromConfig();
$cmd_lang = \Yii::$app->language;
foreach ($this->languages as $lang) {
$this->currentLang = $lang;
$detailInfo[$this->currentLang] = [];
\Yii::$app->language = $this->currentLang;
$this->out("Start processing language: {$this->currentLang}");
$this->memDebug();
foreach ($this->searchItems as $itemKey => $item) {
if (! $this->checkItemConfig($item)) {
$this->out('Config item with key: ' . $itemKey . ' is empty/unset and will be ignored.');
continue;
}
/** @var ActiveRecord[] $models */
$models = $this->getItemsQuery($item)->all();
#$this->memDebug();
// as more than one $itemKey can provide data for the same item->group,
// check/init subarray to get the correct info from indexer
if (empty($detailInfo[$this->currentLang][$item['group']])) {
$detailInfo[$this->currentLang][$item['group']] = [];
$detailInfo[$this->currentLang][$item['group']][$this->infoItemsTotalKey] = 0;
$detailInfo[$this->currentLang][$item['group']][$this->infoItemsPerKey] = [];
}
$detailInfo[$this->currentLang][$item['group']][$this->infoItemsPerKey][$itemKey] = 0;
if (empty($models)) {
$this->debug('no models found');
continue;
}
// todo skip if empty
foreach ($models as $model) {
$text = '';
foreach ($item['attributes'] as $a_key => $a_value) {
if (is_array($a_value)) {
// try to get submodels
if (!empty($model->$a_key)) {
$this->debug('process submodels');
// if 1:1 relation, init dummy array for loop below
$sub_models = is_array($model->$a_key) ? $model->$a_key : [$model->$a_key];
} else {
$this->debug('no submodels found');
continue;
}
// get values from submodel
foreach ($a_value as $prop) {
foreach ($sub_models as $sub_model) {
$text .= ' ' . $this->processProperty($sub_model, $prop);
}
}
} else {
$this->debug('process basemodel property');
$text .= ' ' . $this->processProperty($model, $a_value);
}
}
if (empty(trim($text))) {
$this->debug('text for this model is empty -> continue');
continue;
}
$modelPk = $model->getPrimaryKey();
$searchItem = new Search();
$searchItem->model_class = $item['model_class'];
$searchItem->route = $this->processRoute($model, $item['route']);
$searchItem->language = $this->currentLang;
$searchItem->model_id = is_array($modelPk) ? Json::encode($model->getPrimaryKey(true)) : (string)$modelPk;
$searchItem->search_text = $text;
$searchItem->url_params = Json::encode($this->processUrlParams($model, $item['url_params']));
$searchItem->link_text = $this->processProperty($model, $item['link_text']);
$searchItem->group = $item['group'];
if ($searchItem->validate()) {
if ($searchItem->save()) {
$this->outItemInfo($itemKey, $searchItem);
} else {
\Yii::$app->language = $cmd_lang;
$this->transaction->rollBack();
Yii::error('error while saving model ' . $item['model_class'] . ' with lang ' . $this->currentLang . ', id ' . $model->id . ' rolled back transaction!');
Yii::error($searchItem->errors);
}
} else {
\Yii::$app->language = $cmd_lang;
$this->transaction->rollBack();
Yii::error('error while validating model ' . $item['model_class'] . ' with lang ' . $this->currentLang . ', id ' . $model->id . ' rolled back transaction!');
Yii::error($searchItem->errors);
exit;
}
++$detailInfo[$this->currentLang][$item['group']][$this->infoItemsTotalKey];
++$detailInfo[$this->currentLang][$item['group']][$this->infoItemsPerKey][$itemKey];
// cleanup mem
$searchItem = null;
unset($searchItem);
$model = null;
}
// cleanup mem
$models = null;
unset($models);
}
$this->out('Done language: ' . $this->currentLang);
$this->memDebug();
}
\Yii::$app->language = $cmd_lang;
if ($this->transaction->isActive) {
$this->transaction->commit();
}
$this->out("done, used time: " . (microtime(true) - $sTime) . 'sec');
$this->out(VarDumper::dumpAsString($detailInfo));
}
/**
* if item is array we check required properties.
* if item is null, item will be ignored.
*
* @param $item
*/
protected function checkItemConfig($item) {
if (empty($item)) {
return false;
}
$required = ['model_class', 'route', 'attributes', 'url_params', 'link_text', 'group'];
foreach ($required as $key) {
if (!array_key_exists($key, $item)) {
Yii::error('Required property [' . $key . '] not set in config item.' . PHP_EOL . print_r($item, 1));
exit;
}
}
return true;
}
/**
* process languages param
*
* @return array|false|mixed
*/
protected function initLanguages()
{
if (is_callable($this->languages)) {
$this->debug('get languages from config callable');
return call_user_func($this->languages);
}
if (is_array($this->languages)) {
$this->debug('get languages from config array');
return $this->languages;
}
return [];
}
/**
* init fallbackLanguage param which is used as init translation lang when creating new groups
*
* @return false|mixed|string
*/
protected function initFallbackLanguage()
{
if (is_callable($this->fallbackLanguage)) {
$this->debug('get fallbackLanguage from config callable');
return call_user_func($this->fallbackLanguage);
}
if (is_string($this->fallbackLanguage)) {
$this->debug('get fallbackLanguage from config string');
return $this->fallbackLanguage;
}
return $this->languages[0];
}
/**
* get Query for item
* if not defined we will use the default AR::find() Method
* if defined as callback return result
* if defined as string exec model_class::find_method()
*
* @param $item
*
* @return mixed
*/
protected function getItemsQuery($item)
{
// as default we use the default find() Method from AR-ModelClass
if (empty($item['find_method'])) {
return $item['model_class']::find();
}
// if callback is defined, use this
if (is_callable($item['find_method'])) {
return $item['find_method']($item);
}
// otherwise call find_method from given item
return $item['model_class']::{$item['find_method']}();
}
/**
* get route for item, can be defined as callback or string
*
* @param $item
* @param $param
*
* @return string
*/
protected function processRoute($item, $param)
{
if (is_callable($param)) {
$this->debug('process callable to get route from model');
return trim($param($item));
}
return $param;
}
/**
* get url_params for each item
* each param can be defined as string (attribute name) or callback
*
* @param $item
* @param $params
*
* @return array
*/
protected function processUrlParams($item, $params)
{
$url_params = [];
foreach ($params as $p_key => $p_value) {
$value = $this->processProperty($item, $p_value);
if (!empty($value)) {
$url_params[$p_key] = $value;
}
}
return $url_params;
}
/**
* process given property for given item
*
* if property is a callback, it will be called with item as param
* if property is a string, and item is an object we return $item->$prop
* if property is a string, and item is an array we return $item[$prop]
*
* @param $item
* @param $prop
*
* @return string
*/
protected function processProperty($item, $prop)
{
if (is_callable($prop)) {
$this->debug('process callable');
return trim($prop($item));
}
if (is_object($item) && isset($item->$prop)) {
$this->debug('process property : ' . $prop . ' -> ' . $item->$prop);
return trim($item->$prop);
}
if (is_array($item) && isset($item[$prop])) {
$this->debug('process sub array key : ' . $prop . ' -> ' . $item[$prop]);
return trim($item[$prop]);
}
return '';
}
/**
* if required create new SearchGroup while indexing
*
* @return void
*/
protected function updateSearchItemGroupsFromConfig() {
foreach ($this->searchItems as $item) {
if (! isset($item['group'])) {
continue;
}
if (SearchGroup::find()->andWhere(['ref_name' => $item['group']])->one() === null) {
$cmd_lang = \Yii::$app->language;
\Yii::$app->language = $this->fallbackLanguage;
$this->out("need to create new group for " . $item['group']);
$sGroup = new SearchGroup();
$sGroup->ref_name = $item['group'];
$sGroup->name = $item['group'];
if (! $sGroup->save()) {
VarDumper::dumpAsString($sGroup->errors);
}
\Yii::$app->language = $cmd_lang;
}
}
}
/**
* info, debug output methods while indexing
*/
/**
* @param string $itemKey
* @param Search $searchItem
*
* @return void
*/
protected function outItemInfo($itemKey, Search $searchItem)
{
$values = [$this->currentLang, $itemKey, $searchItem->group, $searchItem->model_class, $searchItem->model_id];
if ($this->debug) {
$values[] = $searchItem->search_text;
}
$msg = 'Saved: ' . implode(' | ', $values);
$this->out($msg);
}
protected function debug($msg) {
$this->debug && $this->out($msg);
}
protected function out($msg)
{
echo $msg . PHP_EOL;
}
protected function err($msg)
{
echo $msg . PHP_EOL;
}
protected function memDebug()
{
if (YII_ENV_DEV) {
$this->out($this->currentLang . " memory_usage: " . memory_get_usage() / 1024 / 1024);
$this->out($this->currentLang . " memory_usage real: " . memory_get_usage(true) / 1024 / 1024);
$this->out($this->currentLang . " memory_peak_usage: " . memory_get_peak_usage(true) / 1024 / 1024);
sleep(1);
}
}
}