From bc2b73c864b2b07d73d1fccbe21264e1a204ccf0 Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Tue, 10 Jan 2023 11:04:14 +0100 Subject: [PATCH 01/15] Prevent getting error if no jobId returned Fix problem, if you get no jobId, because this job already exists and you use a queue "optimizer" like https://github.com/ostark/craft-relax. --- src/services/ReindexQueueManagementService.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/services/ReindexQueueManagementService.php b/src/services/ReindexQueueManagementService.php index d354acd..afe56ba 100644 --- a/src/services/ReindexQueueManagementService.php +++ b/src/services/ReindexQueueManagementService.php @@ -78,7 +78,9 @@ public function enqueueJob(int $entryId, int $siteId, string $type) $jobId = Craft::$app->queue->push($job); - $this->addJobIdToCache($jobId); + if ($jobId > 0) { + $this->addJobIdToCache($jobId); + } } /** From 3c40b021899b82e29208c65d5c7ba3a9b3fbdb1d Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Thu, 2 Feb 2023 16:02:23 +0100 Subject: [PATCH 02/15] disable the deprecated include_type_name parameter for ES8 --- README.md | 4 ++++ src/records/ElasticsearchRecord.php | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d47312d..a2419c0 100755 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ This plugin works with **Craft CMS 3 or 4**. In order to index data, you will need an **Elasticsearch 6.0** (or later) instance, with the **Ingest attachment processor** plugin activated. +In order to use **Elasticsearch 8.0** (or later) you will need to set the +[yii2-elasticsearch][yii2-elasticsearch] dslVersion to 8. The default +dslVersion is set at 5. Setting the dslVersion can be done inside the +elasticsearchComponentConfig. ## Installation diff --git a/src/records/ElasticsearchRecord.php b/src/records/ElasticsearchRecord.php index f78a3ad..13bce8a 100644 --- a/src/records/ElasticsearchRecord.php +++ b/src/records/ElasticsearchRecord.php @@ -302,7 +302,12 @@ public static function createIndex(array $schema, $force = false) ) ); - $db->put(static::index(), ['include_type_name' => 'false'], Json::encode($schema)); + // https://www.elastic.co/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0 + if ($db->dslVersion >= 7) { + $db->put(static::index(), [], Json::encode($schema)); + } else { + $db->put(static::index(), ['include_type_name' => 'false'], Json::encode($schema)); + } } /** From e3b9b8532a405c1c930a63adba786622eb37d1ed Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Tue, 13 Jun 2023 16:48:58 +0200 Subject: [PATCH 03/15] respect backlisted asset volumes This fix adds the support for blacklisted asset volumes to exclude from indexing. --- src/services/ElementIndexerService.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/services/ElementIndexerService.php b/src/services/ElementIndexerService.php index 04a7466..6f78797 100755 --- a/src/services/ElementIndexerService.php +++ b/src/services/ElementIndexerService.php @@ -160,6 +160,15 @@ protected function getReasonForNotReindexing(Element $element): ?string return $message; } } + + if ($element instanceof Asset) { + $blacklist = $this->plugin->getSettings()->blacklistedAssetVolumes; + if (in_array($element->getVolume()->handle, $blacklist, true)) { + $message = "Not indexing asset #{$element->id} since it's in a blacklisted asset volume."; + Craft::debug($message, __METHOD__); + return $message; + } + } return null; } From c67fa54b32c41fba726645c7587d4889b0deed31 Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Tue, 25 Jul 2023 11:13:13 +0200 Subject: [PATCH 04/15] optionally recreate the index from the utility --- src/controllers/CpController.php | 7 ++++++- src/templates/cp/utility.twig | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/controllers/CpController.php b/src/controllers/CpController.php index 8e5e7b5..ca0b40f 100644 --- a/src/controllers/CpController.php +++ b/src/controllers/CpController.php @@ -93,7 +93,12 @@ public function actionReindexPerformAction(): Response if (!empty($params['start'])) { try { $siteIds = $this->getSiteIds($request); - Elasticsearch::getInstance()->indexManagementService->recreateSiteIndex(...$siteIds); + + // Don't match exactly. On false we receive an empty string, on true we + // receive a string '1'. We only recreate the indexes when requested. + if ($params['recreate'] == true) { + Elasticsearch::getInstance()->indexManagementService->recreateSiteIndex(...$siteIds); + } } catch (\Exception $e) { return $this->asErrorJson($e->getMessage()); } diff --git a/src/templates/cp/utility.twig b/src/templates/cp/utility.twig index d23dc2f..d1a964c 100755 --- a/src/templates/cp/utility.twig +++ b/src/templates/cp/utility.twig @@ -32,6 +32,16 @@ > {{ csrfInput() }} +

{{ 'Index'|t('elasticsearch') }}

+ +
+ {{ forms.checkbox({ + name: 'recreate', + label: 'Recreate the index'|t('elasticsearch'), + info: 'Recreating the index removes all existing data, it may take a while before all content is available again.'|t('elasticsearch'), + }) }} +
+

{{ 'Sites'|t('elasticsearch') }}

{{ forms.checkboxSelect({ name: 'sites', From dc95d7b2b35b4d34a0d5153772a421d2cf5dc862 Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Wed, 16 Aug 2023 10:53:03 +0200 Subject: [PATCH 05/15] Update ElementIndexerService.php add method to delete jobs in queue Update Elasticsearch.php Add deleting also for assets --- src/Elasticsearch.php | 15 ++++++++++ src/services/ElementIndexerService.php | 38 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/Elasticsearch.php b/src/Elasticsearch.php index f91acbe..7603d6f 100755 --- a/src/Elasticsearch.php +++ b/src/Elasticsearch.php @@ -100,6 +100,21 @@ function (Event $event) { } ); + // Remove asset from the index upon deletion + Event::on( + Asset::class, + Asset::EVENT_AFTER_DELETE, + function (Event $event) { + /** @var Asset $asset */ + $asset = $event->sender; + try { + $this->elementIndexerService->deleteElement($asset); + } catch (Exception $e) { + // Noop, the element must have already been deleted + } + } + ); + // Index entry, asset & products upon save (creation or update) Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, [$this, 'onElementSaved']); Event::on(Asset::class, Asset::EVENT_AFTER_SAVE, [$this, 'onElementSaved']); diff --git a/src/services/ElementIndexerService.php b/src/services/ElementIndexerService.php index 04a7466..1cdb94b 100755 --- a/src/services/ElementIndexerService.php +++ b/src/services/ElementIndexerService.php @@ -22,7 +22,9 @@ use craft\helpers\UrlHelper; use lhs\elasticsearch\Elasticsearch as ElasticsearchPlugin; use lhs\elasticsearch\exceptions\IndexElementException; +use lhs\elasticsearch\jobs\IndexElementJob; use lhs\elasticsearch\records\ElasticsearchRecord; +use yii\db\Query; /** */ @@ -94,6 +96,7 @@ public function indexElement(Element $element): ?string /** * Removes an entry from the Elasticsearch index + * * @param Element $element The entry to delete * @return int The number of rows deleted * @throws \yii\elasticsearch\Exception If the entry to be deleted cannot be found @@ -102,11 +105,46 @@ public function deleteElement(Element $element): int { Craft::info("Deleting entry #{$element->id}: {$element->url}", __METHOD__); + $this->deleteElementFromQueue($element); + ElasticsearchRecord::$siteId = $element->siteId; return ElasticsearchRecord::deleteAll(['_id' => $element->id]); } + /** + * Removes all entries for an element from queue + * @param Element $element + * @return void + * @throws \yii\base\InvalidConfigException + */ + protected function deleteElementFromQueue(Element $element): void + { + $job = new IndexElementJob( + [ + 'siteId' => $element->getSite()->id, + 'elementId' => $element->id, + 'type' => get_class($element), + ] + ); + + $queueService = Craft::$app->getQueue(); + $queueClass = get_class($queueService); + + $entries = (new Query()) + ->from($queueClass::TABLE) + ->where([ + 'job' => Craft::$app->getQueue()->serializer->serialize($job) + ])->all(); + + foreach ($entries as $entry) { + if (isset($entry['id'])) { + $methodName = $queueService instanceof \yii\queue\db\Queue ? 'remove' : 'release'; + $queueService->$methodName($entry['id']); + } + } + } + /** * Get the reason why an entry should NOT be reindex. * @param Element $element The element to consider for reindexing From f819d81b956f23bd5e0c2260fdffbfaf1093265f Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Mon, 18 Mar 2024 15:03:29 +0100 Subject: [PATCH 06/15] check if an element is correctly enabled for the given site --- src/Elasticsearch.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Elasticsearch.php b/src/Elasticsearch.php index 7603d6f..979869e 100755 --- a/src/Elasticsearch.php +++ b/src/Elasticsearch.php @@ -393,7 +393,7 @@ public function onElementSaved(ModelEvent $event): void } if ($notDraftOrRevision) { - if ($element->enabled) { + if ($element->enabled && $element->getEnabledForSite()) { $this->reindexQueueManagementService->enqueueJob($element->id, $element->siteId, get_class($element)); } else { try { From 9d94e5c460967358a70fecab9dfbde4d99faf764 Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Mon, 18 Mar 2024 16:03:28 +0100 Subject: [PATCH 07/15] fix queue table name for pull-37 --- src/services/ElementIndexerService.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/services/ElementIndexerService.php b/src/services/ElementIndexerService.php index 0e63666..2cc75bc 100755 --- a/src/services/ElementIndexerService.php +++ b/src/services/ElementIndexerService.php @@ -129,10 +129,8 @@ protected function deleteElementFromQueue(Element $element): void ); $queueService = Craft::$app->getQueue(); - $queueClass = get_class($queueService); - $entries = (new Query()) - ->from($queueClass::TABLE) + ->from($queueService->tableName) ->where([ 'job' => Craft::$app->getQueue()->serializer->serialize($job) ])->all(); @@ -198,7 +196,7 @@ protected function getReasonForNotReindexing(Element $element): ?string return $message; } } - + if ($element instanceof Asset) { $blacklist = $this->plugin->getSettings()->blacklistedAssetVolumes; if (in_array($element->getVolume()->handle, $blacklist, true)) { From ac4032713ce74a1880937e06fb09f97a263a324c Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Tue, 24 Sep 2024 16:48:59 +0200 Subject: [PATCH 08/15] fix filemodes 755 -> 644 --- .gitignore | 0 CHANGELOG.md | 0 LICENSE.md | 0 README.md | 0 composer.json | 0 src/Elasticsearch.php | 0 src/config.php | 0 src/console/controllers/ElasticsearchController.php | 0 src/jobs/IndexElementJob.php | 0 src/models/SettingsModel.php | 0 src/resources/cp/js/utilities/reindex.js | 0 src/services/ElasticsearchService.php | 0 src/services/ElementIndexerService.php | 0 src/templates/cp/settings.twig | 0 src/templates/cp/utility.twig | 0 src/translations/fr/elasticsearch.php | 0 src/utilities/RefreshElasticsearchIndexUtility.php | 0 src/variables/ElasticsearchVariable.php | 0 18 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 .gitignore mode change 100755 => 100644 CHANGELOG.md mode change 100755 => 100644 LICENSE.md mode change 100755 => 100644 README.md mode change 100755 => 100644 composer.json mode change 100755 => 100644 src/Elasticsearch.php mode change 100755 => 100644 src/config.php mode change 100755 => 100644 src/console/controllers/ElasticsearchController.php mode change 100755 => 100644 src/jobs/IndexElementJob.php mode change 100755 => 100644 src/models/SettingsModel.php mode change 100755 => 100644 src/resources/cp/js/utilities/reindex.js mode change 100755 => 100644 src/services/ElasticsearchService.php mode change 100755 => 100644 src/services/ElementIndexerService.php mode change 100755 => 100644 src/templates/cp/settings.twig mode change 100755 => 100644 src/templates/cp/utility.twig mode change 100755 => 100644 src/translations/fr/elasticsearch.php mode change 100755 => 100644 src/utilities/RefreshElasticsearchIndexUtility.php mode change 100755 => 100644 src/variables/ElasticsearchVariable.php diff --git a/.gitignore b/.gitignore old mode 100755 new mode 100644 diff --git a/CHANGELOG.md b/CHANGELOG.md old mode 100755 new mode 100644 diff --git a/LICENSE.md b/LICENSE.md old mode 100755 new mode 100644 diff --git a/README.md b/README.md old mode 100755 new mode 100644 diff --git a/composer.json b/composer.json old mode 100755 new mode 100644 diff --git a/src/Elasticsearch.php b/src/Elasticsearch.php old mode 100755 new mode 100644 diff --git a/src/config.php b/src/config.php old mode 100755 new mode 100644 diff --git a/src/console/controllers/ElasticsearchController.php b/src/console/controllers/ElasticsearchController.php old mode 100755 new mode 100644 diff --git a/src/jobs/IndexElementJob.php b/src/jobs/IndexElementJob.php old mode 100755 new mode 100644 diff --git a/src/models/SettingsModel.php b/src/models/SettingsModel.php old mode 100755 new mode 100644 diff --git a/src/resources/cp/js/utilities/reindex.js b/src/resources/cp/js/utilities/reindex.js old mode 100755 new mode 100644 diff --git a/src/services/ElasticsearchService.php b/src/services/ElasticsearchService.php old mode 100755 new mode 100644 diff --git a/src/services/ElementIndexerService.php b/src/services/ElementIndexerService.php old mode 100755 new mode 100644 diff --git a/src/templates/cp/settings.twig b/src/templates/cp/settings.twig old mode 100755 new mode 100644 diff --git a/src/templates/cp/utility.twig b/src/templates/cp/utility.twig old mode 100755 new mode 100644 diff --git a/src/translations/fr/elasticsearch.php b/src/translations/fr/elasticsearch.php old mode 100755 new mode 100644 diff --git a/src/utilities/RefreshElasticsearchIndexUtility.php b/src/utilities/RefreshElasticsearchIndexUtility.php old mode 100755 new mode 100644 diff --git a/src/variables/ElasticsearchVariable.php b/src/variables/ElasticsearchVariable.php old mode 100755 new mode 100644 From 3cb3caf661895db04485120818fc20461c962dcc Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Tue, 24 Sep 2024 16:56:45 +0200 Subject: [PATCH 09/15] allow indexing with cli commands (used via feedme) --- src/Elasticsearch.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Elasticsearch.php b/src/Elasticsearch.php index 979869e..6bc7571 100644 --- a/src/Elasticsearch.php +++ b/src/Elasticsearch.php @@ -84,7 +84,9 @@ public function init(): void $this->controllerNamespace = 'lhs\elasticsearch\console\controllers'; } - if (Craft::$app->getRequest()->getIsCpRequest()) { + $isCpRequest = Craft::$app->getRequest()->getIsCpRequest(); + $isConsoleRequest = Craft::$app->getRequest()->getIsConsoleRequest(); + if ($isCpRequest || $isConsoleRequest) { // Remove entry from the index upon deletion Event::on( Entry::class, From 34761138062e06c29894ec5ac4788de7500a807a Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Tue, 29 Oct 2024 12:22:21 +0100 Subject: [PATCH 10/15] setup craft 5 support --- .gitignore | 1 + composer.json | 16 ++++++++++++++-- phpstan.neon | 7 +++++++ src/Elasticsearch.php | 4 +++- src/models/IndexableElementModel.php | 3 +-- .../RefreshElasticsearchIndexUtility.php | 13 ++++++++++++- 6 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 phpstan.neon diff --git a/.gitignore b/.gitignore index a17970c..0fcdeb0 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ !.vscode/extensions.json config.codekit3 prepros-6.config +composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json index c9172f4..0628d3a 100644 --- a/composer.json +++ b/composer.json @@ -23,9 +23,13 @@ } ], "require": { - "craftcms/cms": "^4.0.0", + "craftcms/cms": "^4.0|^5.0", "yiisoft/yii2-elasticsearch": "^2.1.0" }, + "require-dev": { + "craftcms/phpstan": "dev-main", + "craftcms/rector": "dev-main" + }, "autoload": { "psr-4": { "lhs\\elasticsearch\\": "src/" @@ -37,5 +41,13 @@ "schemaVersion": "1.3.0", "changelogUrl": "https://raw.githubusercontent.com/la-haute-societe/craft-elasticsearch/master/CHANGELOG.md", "class": "lhs\\elasticsearch\\Elasticsearch" - } + }, + "config": { + "allow-plugins": { + "yiisoft/yii2-composer": true, + "craftcms/plugin-installer": true + } + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..f9f81af --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,7 @@ +includes: + - vendor/craftcms/phpstan/phpstan.neon + +parameters: + level: 0 + paths: + - src diff --git a/src/Elasticsearch.php b/src/Elasticsearch.php index 6bc7571..d51c823 100644 --- a/src/Elasticsearch.php +++ b/src/Elasticsearch.php @@ -151,7 +151,9 @@ function (ExecEvent $event) { // Register the plugin's CP utility Event::on( Utilities::class, - Utilities::EVENT_REGISTER_UTILITY_TYPES, + version_compare(Craft::$app->getInfo()->version, '5.0', '<') + ? Utilities::EVENT_REGISTER_UTILITY_TYPES + : Utilities::EVENT_REGISTER_UTILITIES, function (RegisterComponentTypesEvent $event) { $event->types[] = RefreshElasticsearchIndexUtility::class; } diff --git a/src/models/IndexableElementModel.php b/src/models/IndexableElementModel.php index 1583168..785c1af 100644 --- a/src/models/IndexableElementModel.php +++ b/src/models/IndexableElementModel.php @@ -58,8 +58,7 @@ public function getElement() return $element; } - - public function jsonSerialize() + public function jsonSerialize(): mixed { return $this->toArray(); } diff --git a/src/utilities/RefreshElasticsearchIndexUtility.php b/src/utilities/RefreshElasticsearchIndexUtility.php index e0e25f6..57bd2a3 100644 --- a/src/utilities/RefreshElasticsearchIndexUtility.php +++ b/src/utilities/RefreshElasticsearchIndexUtility.php @@ -46,11 +46,22 @@ public static function id(): string * Returns the path to the utility's SVG icon. * @return string|null The path to the utility SVG icon */ - public static function iconPath(): ?string + public static function icon(): ?string { return Craft::getAlias('@lhs/elasticsearch/resources/cp/img/utility-icon.svg'); } + /** + * Icon path for Craft < 5. + * + * Returns the path to the utility's SVG icon. + * @return string|null The path to the utility SVG icon + */ + public static function iconPath(): ?string + { + return self::icon(); + } + /** * Returns the number that should be shown in the utility’s nav item badge. * If `0` is returned, no badge will be shown From 4f4ed6ceb63043b95ddf371554fbf0b815978a61 Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Tue, 29 Oct 2024 14:05:40 +0100 Subject: [PATCH 11/15] remove craft 4 support, replace ->sections with ->getEntries() --- composer.json | 2 +- src/Elasticsearch.php | 6 ++---- src/services/ElementIndexerService.php | 6 ------ src/templates/cp/settings.twig | 2 +- 4 files changed, 4 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index 0628d3a..e717d60 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ } ], "require": { - "craftcms/cms": "^4.0|^5.0", + "craftcms/cms": "^5.0", "yiisoft/yii2-elasticsearch": "^2.1.0" }, "require-dev": { diff --git a/src/Elasticsearch.php b/src/Elasticsearch.php index d51c823..7285a78 100644 --- a/src/Elasticsearch.php +++ b/src/Elasticsearch.php @@ -151,9 +151,7 @@ function (ExecEvent $event) { // Register the plugin's CP utility Event::on( Utilities::class, - version_compare(Craft::$app->getInfo()->version, '5.0', '<') - ? Utilities::EVENT_REGISTER_UTILITY_TYPES - : Utilities::EVENT_REGISTER_UTILITIES, + Utilities::EVENT_REGISTER_UTILITIES, function (RegisterComponentTypesEvent $event) { $event->types[] = RefreshElasticsearchIndexUtility::class; } @@ -256,7 +254,7 @@ protected function settingsHtml(): string $overrides = Craft::$app->getConfig()->getConfigFromFile(strtolower($this->handle)); $sections = ArrayHelper::map( - Craft::$app->sections->getAllSections(), + Craft::$app->getEntries()->getAllSections(), 'id', function (Section $section): array { return [ diff --git a/src/services/ElementIndexerService.php b/src/services/ElementIndexerService.php index 2cc75bc..df0e801 100644 --- a/src/services/ElementIndexerService.php +++ b/src/services/ElementIndexerService.php @@ -176,12 +176,6 @@ protected function getReasonForNotReindexing(Element $element): ?string } } - if (!$element->hasContent()) { - $message = "Not indexing entry #{$element->id} since it has no content."; - Craft::debug($message, __METHOD__); - return $message; - } - if (!$element->getUrl()) { $message = "Not indexing entry #{$element->id} since it has no URL."; Craft::debug($message, __METHOD__); diff --git a/src/templates/cp/settings.twig b/src/templates/cp/settings.twig index 3c1cc7e..dc81e17 100644 --- a/src/templates/cp/settings.twig +++ b/src/templates/cp/settings.twig @@ -137,7 +137,7 @@ - {% for section in craft.app.sections.allSections %} + {% for section in craft.app.entries.allSections %} {% for entryType in section.entryTypes %} {{ section.type|ucfirst }} From 5bf268ec3d0d9c67c1dd624d87f920369fdc87d5 Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Tue, 29 Oct 2024 14:15:36 +0100 Subject: [PATCH 12/15] cleanup for craft 5, updated readme --- .craftplugin | 32 ++++++++++++++++++- README.md | 2 +- src/Elasticsearch.php | 4 +-- src/config.php | 4 +-- .../controllers/ElasticsearchController.php | 4 +-- src/controllers/CpController.php | 4 +-- src/events/ErrorEvent.php | 4 +-- src/exceptions/IndexElementException.php | 4 +-- src/jobs/IndexElementJob.php | 4 +-- src/models/SettingsModel.php | 4 +-- src/records/ElasticsearchRecord.php | 4 +-- src/resources/CpAssetBundle.php | 4 +-- src/resources/cp/css/elastic-branding.css | 4 +-- src/resources/cp/js/utilities/reindex.js | 4 +-- src/services/ElasticsearchService.php | 4 +-- src/services/ElementIndexerService.php | 4 +-- src/services/IndexManagementService.php | 4 +-- .../ReindexQueueManagementService.php | 4 +-- .../components/elastic-branding.twig | 4 +-- src/templates/cp/settings.twig | 4 +-- src/templates/cp/utility.twig | 4 +-- src/translations/fr/elasticsearch.php | 4 +-- .../RefreshElasticsearchIndexUtility.php | 15 +-------- src/variables/ElasticsearchVariable.php | 4 +-- 24 files changed, 54 insertions(+), 79 deletions(-) diff --git a/.craftplugin b/.craftplugin index 0354cb5..86f077e 100644 --- a/.craftplugin +++ b/.craftplugin @@ -1 +1,31 @@ -{"pluginName":"Elasticsearch","pluginDescription":"Bring the power of Elasticsearch to you Craft 3 CMS project","pluginVersion":"1.1.0,1.0.0","pluginAuthorName":"La Haute Société","pluginVendorName":"la-haute-societe","pluginAuthorUrl":"https://www.lahautesociete.com","pluginAuthorGithub":"juban","codeComments":"codeComments","pluginComponents":["utilities","jobs","tasks","consolecommands","controllers","cpsection","models","records","services","settings","variables"],"consolecommandName":"Elasticsearch","controllerName":"Elasticsearch","cpsectionName":"Elasticsearch","modelName":"Elasticsearch","recordName":"Elasticsearch","serviceName":"Elasticsearch","utilityName":"elasticsearch","apiVersion":"api_version_3_0"} +{ + "pluginName": "Elasticsearch", + "pluginDescription": "Bring the power of Elasticsearch to you Craft 5 CMS project", + "pluginVersion": "1.1.0,1.0.0", + "pluginAuthorName": "La Haute Société", + "pluginVendorName": "la-haute-societe", + "pluginAuthorUrl": "https://www.lahautesociete.com", + "pluginAuthorGithub": "juban", + "codeComments": "codeComments", + "pluginComponents": [ + "utilities", + "jobs", + "tasks", + "consolecommands", + "controllers", + "cpsection", + "models", + "records", + "services", + "settings", + "variables" + ], + "consolecommandName": "Elasticsearch", + "controllerName": "Elasticsearch", + "cpsectionName": "Elasticsearch", + "modelName": "Elasticsearch", + "recordName": "Elasticsearch", + "serviceName": "Elasticsearch", + "utilityName": "elasticsearch", + "apiVersion": "api_version_3_0" +} diff --git a/README.md b/README.md index a2419c0..9eb92a6 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Bring the power of Elasticsearch to your Craft CMS projects. ## Requirements -This plugin works with **Craft CMS 3 or 4**. +This plugin works with **Craft CMS 5**. Use v2 for **Craft CMS 3 or 4** In order to index data, you will need an **Elasticsearch 6.0** (or later) instance, with the **Ingest attachment processor** plugin activated. diff --git a/src/Elasticsearch.php b/src/Elasticsearch.php index 7285a78..3630283 100644 --- a/src/Elasticsearch.php +++ b/src/Elasticsearch.php @@ -1,8 +1,6 @@ Date: Tue, 29 Oct 2024 15:13:38 +0100 Subject: [PATCH 13/15] replace ->sections with ->getEntries() in migration --- src/migrations/m200929_155818_project_config_support.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/migrations/m200929_155818_project_config_support.php b/src/migrations/m200929_155818_project_config_support.php index 3dc1b20..55d225f 100644 --- a/src/migrations/m200929_155818_project_config_support.php +++ b/src/migrations/m200929_155818_project_config_support.php @@ -21,7 +21,7 @@ public function safeUp() $ids = $projectConfig->get('plugins.elasticsearch.settings.blacklistedEntryTypes'); $IdToHandleMapping = ArrayHelper::map( - Craft::$app->sections->getAllEntryTypes(), + Craft::$app->getEntries()->getAllEntryTypes(), static function (EntryType $entryType) { return $entryType->id; }, static function (EntryType $entryType) { return $entryType->handle; } ); From 801f1a6f5613d0acb3c1148265ef82b0a80e0dbb Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Wed, 6 Nov 2024 16:41:32 +0100 Subject: [PATCH 14/15] disabled deleting jobs on entry delete - When working with codeception and the cleanup parameter the queue serializer would be a string instead of an exception. This results in failing tests. We now execute the job and mark it as success when models are missing. --- .../controllers/ElasticsearchController.php | 4 ++- src/controllers/CpController.php | 4 ++- src/jobs/IndexElementJob.php | 5 ++- src/models/IndexableElementModel.php | 6 +--- src/services/ElementIndexerService.php | 36 ------------------- 5 files changed, 11 insertions(+), 44 deletions(-) diff --git a/src/console/controllers/ElasticsearchController.php b/src/console/controllers/ElasticsearchController.php index 33972ff..0e30e75 100644 --- a/src/console/controllers/ElasticsearchController.php +++ b/src/console/controllers/ElasticsearchController.php @@ -194,6 +194,8 @@ protected function reindexElement(IndexableElementModel $indexableElementModel): return $e->getMessage(); } - return ElasticsearchPlugin::getInstance()->elementIndexerService->indexElement($element); + return $element + ? ElasticsearchPlugin::getInstance()->elementIndexerService->indexElement($element) + : null; } } diff --git a/src/controllers/CpController.php b/src/controllers/CpController.php index 2cc4b1e..d677355 100644 --- a/src/controllers/CpController.php +++ b/src/controllers/CpController.php @@ -184,7 +184,9 @@ protected function reindexElement(): ?string $element = $model->getElement(); try { - return Elasticsearch::getInstance()->elementIndexerService->indexElement($element); + return $element + ? Elasticsearch::getInstance()->elementIndexerService->indexElement($element) + : null; } catch (\Exception $e) { Craft::error("Error while re-indexing element {$element->url}: {$e->getMessage()}", __METHOD__); Craft::error(VarDumper::dumpAsString($e), __METHOD__); diff --git a/src/jobs/IndexElementJob.php b/src/jobs/IndexElementJob.php index 9c49ff8..9bd1256 100644 --- a/src/jobs/IndexElementJob.php +++ b/src/jobs/IndexElementJob.php @@ -40,7 +40,10 @@ public function execute($queue): void $model->elementId = $this->elementId; $model->siteId = $this->siteId; $model->type = $this->type; - Elasticsearch::getInstance()->elementIndexerService->indexElement($model->getElement()); + + if ($element = $model->getElement()) { + Elasticsearch::getInstance()->elementIndexerService->indexElement($element); + } } /** diff --git a/src/models/IndexableElementModel.php b/src/models/IndexableElementModel.php index 785c1af..ab2adcf 100644 --- a/src/models/IndexableElementModel.php +++ b/src/models/IndexableElementModel.php @@ -21,7 +21,7 @@ class IndexableElementModel extends \craft\base\Model implements \JsonSerializab public $type; /** - * @return Element + * @return Element|null * @throws IndexableElementModelException */ public function getElement() @@ -51,10 +51,6 @@ public function getElement() throw new IndexableElementModelException($this, IndexableElementModelException::UNEXPECTED_TYPE); } - if ($element === null) { - throw new IndexableElementModelException($this, IndexableElementModelException::ELEMENT_NOT_FOUND); - } - return $element; } diff --git a/src/services/ElementIndexerService.php b/src/services/ElementIndexerService.php index 388bfa0..3890e81 100644 --- a/src/services/ElementIndexerService.php +++ b/src/services/ElementIndexerService.php @@ -20,9 +20,7 @@ use craft\helpers\UrlHelper; use lhs\elasticsearch\Elasticsearch as ElasticsearchPlugin; use lhs\elasticsearch\exceptions\IndexElementException; -use lhs\elasticsearch\jobs\IndexElementJob; use lhs\elasticsearch\records\ElasticsearchRecord; -use yii\db\Query; /** */ @@ -91,7 +89,6 @@ public function indexElement(Element $element): ?string return null; } - /** * Removes an entry from the Elasticsearch index * @@ -103,44 +100,11 @@ public function deleteElement(Element $element): int { Craft::info("Deleting entry #{$element->id}: {$element->url}", __METHOD__); - $this->deleteElementFromQueue($element); - ElasticsearchRecord::$siteId = $element->siteId; return ElasticsearchRecord::deleteAll(['_id' => $element->id]); } - /** - * Removes all entries for an element from queue - * @param Element $element - * @return void - * @throws \yii\base\InvalidConfigException - */ - protected function deleteElementFromQueue(Element $element): void - { - $job = new IndexElementJob( - [ - 'siteId' => $element->getSite()->id, - 'elementId' => $element->id, - 'type' => get_class($element), - ] - ); - - $queueService = Craft::$app->getQueue(); - $entries = (new Query()) - ->from($queueService->tableName) - ->where([ - 'job' => Craft::$app->getQueue()->serializer->serialize($job) - ])->all(); - - foreach ($entries as $entry) { - if (isset($entry['id'])) { - $methodName = $queueService instanceof \yii\queue\db\Queue ? 'remove' : 'release'; - $queueService->$methodName($entry['id']); - } - } - } - /** * Get the reason why an entry should NOT be reindex. * @param Element $element The element to consider for reindexing From af4106a6d80eb969d7ffbbd3309ffeb7c52ad4d9 Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Mon, 3 Mar 2025 14:23:15 +0100 Subject: [PATCH 15/15] prevent indexing jobs for matrix entries --- src/Elasticsearch.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Elasticsearch.php b/src/Elasticsearch.php index 3630283..8d0e9ca 100644 --- a/src/Elasticsearch.php +++ b/src/Elasticsearch.php @@ -394,7 +394,11 @@ public function onElementSaved(ModelEvent $event): void if ($notDraftOrRevision) { if ($element->enabled && $element->getEnabledForSite()) { - $this->reindexQueueManagementService->enqueueJob($element->id, $element->siteId, get_class($element)); + + // Only index entry items with an uri. This prevents jobs being spawned for Matrix entries in Craft 5. + if (! $element instanceof Entry || $element->uri) { + $this->reindexQueueManagementService->enqueueJob($element->id, $element->siteId, get_class($element)); + } } else { try { $this->elementIndexerService->deleteElement($element);