-
Notifications
You must be signed in to change notification settings - Fork 611
/
UpdateSearchIndex.php
76 lines (67 loc) · 1.89 KB
/
UpdateSearchIndex.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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\queue\jobs;
use Craft;
use craft\base\ElementInterface;
use craft\i18n\Translation;
use craft\queue\BaseJob;
/**
* UpdateSearchIndex job
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.2.0
*/
class UpdateSearchIndex extends BaseJob
{
/**
* @var string The type of elements to update.
* @phpstan-var class-string<ElementInterface>
*/
public string $elementType;
/**
* @var int|int[]|null The ID(s) of the element(s) to update
*/
public array|int|null $elementId = null;
/**
* @var int|string|null The site ID of the elements to update, or `'*'` to update all sites
*/
public string|int|null $siteId = '*';
/**
* @var string[]|null The field handles that should be indexed
* @since 3.4.0
*/
public ?array $fieldHandles = null;
/**
* @inheritdoc
*/
public function execute($queue): void
{
/** @var string|ElementInterface $class */
/** @phpstan-var class-string<ElementInterface>|ElementInterface $class */
$class = $this->elementType;
$elements = $class::find()
->drafts(null)
->provisionalDrafts(null)
->id($this->elementId)
->siteId($this->siteId)
->status(null)
->all();
$total = count($elements);
$searchService = Craft::$app->getSearch();
foreach ($elements as $i => $element) {
$this->setProgress($queue, ($i + 1) / $total);
$searchService->indexElementAttributes($element, $this->fieldHandles);
}
}
/**
* @inheritdoc
*/
protected function defaultDescription(): ?string
{
return Translation::prep('app', 'Updating search indexes');
}
}