diff --git a/CHANGELOG.md b/CHANGELOG.md index 24c8e70..1f07b44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. +## 3.3.1 - 2023-11-08 + +### Added +- Added `AfterIndexImport` event, which fires after the ImportIndex job has finished. ([#283](https://github.com/studioespresso/craft-scout/issues/283)) +### Fixed +- Deindexing now also takes the correct siteId into account ([#281](https://github.com/studioespresso/craft-scout/issues/281)) + ## 3.3.0 - 2023-10-23 ### Added - Added support for creating and configuring replica indexes ([#275](https://github.com/studioespresso/craft-scout/pull/275), thanks [@johnnynotsolucky](https://github.com/johnnynotsolucky)!) diff --git a/README.md b/README.md index 757e44c..a14e562 100644 --- a/README.md +++ b/README.md @@ -332,6 +332,29 @@ The event has a properties: An example use-case for this would be to check the type of the element that's beind saved and settings `shouldBeSearchable` to `false` when it's a Matrix block. +### AfterIndexImport + +This event runs at the end of the `ImportIndex` job, when every item has been processed. + +```php + +use rias\scout\events\AfterIndexImport; +use rias\scout\jobs\ImportIndex; + +Event::on( + ImportIndex::class, + ImportIndex::EVENT_AFTER_INDEX_IMPORT, + function (AfterIndexImport $event) { + // Your code here + }); +``` + +The event has one property: +- $indexName (the name of the index that just finished importing + +An example use-case for this would be to keep a log or dateLastImported if you're running imports on a schedule. + + --- ## Upgrading from 1.x diff --git a/composer.json b/composer.json index 6151f03..5aeb77a 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "studioespresso/craft-scout", "description": "Craft Scout provides a simple solution for adding full-text search to your entries. Scout will automatically keep your search indexes in sync with your entries.", "type": "craft-plugin", - "version": "3.3.0", + "version": "3.3.1", "keywords": [ "craft", "cms", diff --git a/src/Scout.php b/src/Scout.php index ce17635..e6efe18 100644 --- a/src/Scout.php +++ b/src/Scout.php @@ -201,7 +201,10 @@ function (ElementEvent $event) { ->ttr(Scout::$plugin->getSettings()->ttr) ->priority(Scout::$plugin->getSettings()->priority) ->push( - new DeindexElement(['id' => $element->id]) + new DeindexElement([ + 'id' => $element->id, + 'siteId' => $element->site->id + ]) ); } } diff --git a/src/events/AfterIndexImport.php b/src/events/AfterIndexImport.php new file mode 100644 index 0000000..438fb75 --- /dev/null +++ b/src/events/AfterIndexImport.php @@ -0,0 +1,22 @@ +getElements()->getElementById($this->id, null, null, [ + $element = Craft::$app->getElements()->getElementById($this->id, null, $this->siteId, [ 'trashed' => null, ]); diff --git a/src/jobs/ImportIndex.php b/src/jobs/ImportIndex.php index bcf4adb..dbcf71f 100644 --- a/src/jobs/ImportIndex.php +++ b/src/jobs/ImportIndex.php @@ -2,8 +2,10 @@ namespace rias\scout\jobs; +use craft\base\Event; use craft\queue\BaseJob; use rias\scout\engines\Engine; +use rias\scout\events\AfterIndexImport; use rias\scout\Scout; class ImportIndex extends BaseJob @@ -11,10 +13,12 @@ class ImportIndex extends BaseJob /** @var string */ public $indexName; + public const EVENT_AFTER_INDEX_IMPORT = "afterIndexImport"; + public function execute($queue): void { /** @var Engine $engine */ - $engine = Scout::$plugin->getSettings()->getEngines()->first(function(Engine $engine) { + $engine = Scout::$plugin->getSettings()->getEngines()->first(function (Engine $engine) { return $engine->scoutIndex->indexName === $this->indexName; }); @@ -33,6 +37,12 @@ public function execute($queue): void $elementsUpdated += count($elements); $this->setProgress($queue, $elementsUpdated / $elementsCount); } + + $event = new AfterIndexImport([ + 'indexName' => $this->indexName + ]); + + Event::trigger(self::class, self::EVENT_AFTER_INDEX_IMPORT, $event); } protected function defaultDescription(): string