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 8fa5706 commit db57f86
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes for Elasticsearch Plugin

## 1.2.0 - 2022-06-15

### Added

- Indexation of elements can be done without using jobs and queues.

## 1.1.0 - 2022-04-27

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "codemonauts/craft-elasticsearch",
"description": "Replace Craft's database full-text search with Elasticsearch.",
"version": "1.1.0",
"version": "1.2.0",
"type": "craft-plugin",
"keywords": [
"craft",
Expand Down
46 changes: 36 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,17 @@ 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
* @param string $queue The queue to use.
* @param int $priority The queue priority to use.
*
* @throws \craft\errors\InvalidFieldException
* @throws \craft\errors\SiteNotFoundException
* @throws \yii\base\InvalidConfigException
*/
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 +67,34 @@ 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 $e) {
$queue->push($job);
if ($useQueue) {
$job = new UpdateElasticsearchIndex([
'elementType' => $element['type'],
'elementId' => $element['id'],
'siteId' => $siteHandle,
]);
try {
$queue->priority($priority)->push($job);
} catch (NotSupportedException $e) {
$queue->push($job);
}
} else {
$query = $element['type']::find()
->drafts(null)
->id($element['id'])
->siteId($siteHandle)
->anyStatus();

// TODO: Remove when dropping 3.6 support.
$craft37 = version_compare(Craft::$app->getVersion(), '3.7', '>=');
if ($craft37) {
$query->provisionalDrafts(null);
}

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

0 comments on commit db57f86

Please sign in to comment.