Skip to content

Commit 3e00f5b

Browse files
committed
add indexation of elements without jobs
1 parent b12b104 commit 3e00f5b

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66

77
- Craft CMS 4 compatibility
8+
- Indexation of elements can be done without using jobs and queues.
89

910
### Changed
1011

src/console/controllers/ElementsController.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace codemonauts\elastic\console\controllers;
44

5+
use codemonauts\elastic\Elastic;
56
use codemonauts\elastic\jobs\UpdateElasticsearchIndex;
67
use Craft;
78
use craft\base\ElementInterface;
@@ -17,11 +18,15 @@ class ElementsController extends Controller
1718
* Index elements to current index.
1819
*
1920
* @param string $siteHandle The site to index. Default '*' to reindex elements of all sites.
21+
* @param bool $useQueue Whether to use jobs in a queue for indexing.
2022
* @param string $queue The queue to use.
2123
* @param int $priority The queue priority to use.
24+
*
25+
* @throws \craft\errors\SiteNotFoundException
2226
*/
23-
public function actionIndex(string $siteHandle = '*', string $queue = 'queue', int $priority = 2048)
27+
public function actionIndex(string $siteHandle = '*', bool $useQueue = true, string $queue = 'queue', int $priority = 2048)
2428
{
29+
$search = Elastic::$plugin->getSearch();
2530
$queue = Craft::$app->$queue;
2631
$elementsTable = Table::ELEMENTS;
2732

@@ -60,15 +65,29 @@ public function actionIndex(string $siteHandle = '*', string $queue = 'queue', i
6065
Console::startProgress(0, $total);
6166
foreach ($query->batch() as $rows) {
6267
foreach ($rows as $element) {
63-
$job = new UpdateElasticsearchIndex([
64-
'elementType' => $element['type'],
65-
'elementId' => $element['id'],
66-
'siteId' => $siteHandle,
67-
]);
68-
try {
69-
$queue->priority($priority)->push($job);
70-
} catch (NotSupportedException) {
71-
$queue->push($job);
68+
if ($useQueue) {
69+
$job = new UpdateElasticsearchIndex([
70+
'elementType' => $element['type'],
71+
'elementId' => $element['id'],
72+
'siteId' => $siteHandle,
73+
]);
74+
try {
75+
$queue->priority($priority)->push($job);
76+
} catch (NotSupportedException) {
77+
$queue->push($job);
78+
}
79+
} else {
80+
$elementsOfType = $element['type']::find()
81+
->drafts(null)
82+
->id($element['id'])
83+
->siteId($siteHandle)
84+
->status(null)
85+
->provisionalDrafts(null)
86+
->all();
87+
88+
foreach ($elementsOfType as $e) {
89+
$search->indexElementAttributes($e);
90+
}
7291
}
7392
Console::updateProgress(++$counter, $total);
7493
}

0 commit comments

Comments
 (0)