Skip to content

Commit db57f86

Browse files
committed
add indexation of elements without jobs
1 parent 8fa5706 commit db57f86

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Release Notes for Elasticsearch Plugin
22

3+
## 1.2.0 - 2022-06-15
4+
5+
### Added
6+
7+
- Indexation of elements can be done without using jobs and queues.
8+
39
## 1.1.0 - 2022-04-27
410

511
### Added

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "codemonauts/craft-elasticsearch",
33
"description": "Replace Craft's database full-text search with Elasticsearch.",
4-
"version": "1.1.0",
4+
"version": "1.2.0",
55
"type": "craft-plugin",
66
"keywords": [
77
"craft",

src/console/controllers/ElementsController.php

Lines changed: 36 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,17 @@ 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
2022
* @param string $queue The queue to use.
2123
* @param int $priority The queue priority to use.
24+
*
25+
* @throws \craft\errors\InvalidFieldException
26+
* @throws \craft\errors\SiteNotFoundException
27+
* @throws \yii\base\InvalidConfigException
2228
*/
23-
public function actionIndex(string $siteHandle = '*', string $queue = 'queue', int $priority = 2048)
29+
public function actionIndex(string $siteHandle = '*', bool $useQueue = true, string $queue = 'queue', int $priority = 2048)
2430
{
31+
$search = Elastic::$plugin->getSearch();
2532
$queue = Craft::$app->$queue;
2633
$elementsTable = Table::ELEMENTS;
2734

@@ -60,15 +67,34 @@ public function actionIndex(string $siteHandle = '*', string $queue = 'queue', i
6067
Console::startProgress(0, $total);
6168
foreach ($query->batch() as $rows) {
6269
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 $e) {
71-
$queue->push($job);
70+
if ($useQueue) {
71+
$job = new UpdateElasticsearchIndex([
72+
'elementType' => $element['type'],
73+
'elementId' => $element['id'],
74+
'siteId' => $siteHandle,
75+
]);
76+
try {
77+
$queue->priority($priority)->push($job);
78+
} catch (NotSupportedException $e) {
79+
$queue->push($job);
80+
}
81+
} else {
82+
$query = $element['type']::find()
83+
->drafts(null)
84+
->id($element['id'])
85+
->siteId($siteHandle)
86+
->anyStatus();
87+
88+
// TODO: Remove when dropping 3.6 support.
89+
$craft37 = version_compare(Craft::$app->getVersion(), '3.7', '>=');
90+
if ($craft37) {
91+
$query->provisionalDrafts(null);
92+
}
93+
94+
$elementsOfType = $query->all();
95+
foreach ($elementsOfType as $e) {
96+
$search->indexElementAttributes($e);
97+
}
7298
}
7399
Console::updateProgress(++$counter, $total);
74100
}

0 commit comments

Comments
 (0)