Skip to content

Commit

Permalink
add indexation of elements without jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
kringkaste committed Jun 15, 2022
1 parent b12b104 commit 3e00f5b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

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

### Changed

Expand Down
39 changes: 29 additions & 10 deletions src/console/controllers/ElementsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace codemonauts\elastic\console\controllers;

use codemonauts\elastic\Elastic;
use codemonauts\elastic\jobs\UpdateElasticsearchIndex;
use Craft;
use craft\base\ElementInterface;
Expand All @@ -17,11 +18,15 @@ class ElementsController extends Controller
* Index elements to current index.
*
* @param string $siteHandle The site to index. Default '*' to reindex elements of all sites.
* @param bool $useQueue Whether to use jobs in a queue for indexing.
* @param string $queue The queue to use.
* @param int $priority The queue priority to use.
*
* @throws \craft\errors\SiteNotFoundException
*/
public function actionIndex(string $siteHandle = '*', string $queue = 'queue', int $priority = 2048)
public function actionIndex(string $siteHandle = '*', bool $useQueue = true, string $queue = 'queue', int $priority = 2048)
{
$search = Elastic::$plugin->getSearch();
$queue = Craft::$app->$queue;
$elementsTable = Table::ELEMENTS;

Expand Down Expand Up @@ -60,15 +65,29 @@ public function actionIndex(string $siteHandle = '*', string $queue = 'queue', i
Console::startProgress(0, $total);
foreach ($query->batch() as $rows) {
foreach ($rows as $element) {
$job = new UpdateElasticsearchIndex([
'elementType' => $element['type'],
'elementId' => $element['id'],
'siteId' => $siteHandle,
]);
try {
$queue->priority($priority)->push($job);
} catch (NotSupportedException) {
$queue->push($job);
if ($useQueue) {
$job = new UpdateElasticsearchIndex([
'elementType' => $element['type'],
'elementId' => $element['id'],
'siteId' => $siteHandle,
]);
try {
$queue->priority($priority)->push($job);
} catch (NotSupportedException) {
$queue->push($job);
}
} else {
$elementsOfType = $element['type']::find()
->drafts(null)
->id($element['id'])
->siteId($siteHandle)
->status(null)
->provisionalDrafts(null)
->all();

foreach ($elementsOfType as $e) {
$search->indexElementAttributes($e);
}
}
Console::updateProgress(++$counter, $total);
}
Expand Down

0 comments on commit 3e00f5b

Please sign in to comment.