Skip to content

Commit

Permalink
Move indexing to jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rias committed Dec 12, 2017
1 parent 7ca46da commit 2f70499
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 72 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 0.1.1 - 2017-12-12
### Added
- Move indexing to jobs

## 0.1.0 - 2017-12-11
### Added
- Initial release
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Scout plugin for Craft CMS 3.x
# Scout plugin for Craft CMS 3

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.

Expand All @@ -22,9 +22,9 @@ To install the plugin, follow these instructions.

## Setup

To define your Indices, create a new `scout.php` file within your `config` folder. This file should return an array with 3 keys, an `application_id`, your `admin_api_key` (which are both found in your [Algolia](https://www.algolia.com/api-keys) account) and a `mappings` key, which defines your site's mappings.
To define your indices, create a new `scout.php` file within your `config` folder. This file should return an array with 3 keys, an `application_id`, your `admin_api_key` (which are both found in your [Algolia](https://www.algolia.com/api-keys) account) and a `mappings` key, which defines your site's mappings.

Within the mappings array, each index is represented by an array, and values are the configuration.
Within the mappings array, each index is represented by a configuration array.

```php
<?php
Expand All @@ -43,6 +43,7 @@ return [
return $element->toArray();
}
],
...
],
];
```
Expand All @@ -53,7 +54,7 @@ return [
The index name in Algolia, if you don't already have an index created, Scout will create one for you.

#### `elementType`
The element type that this index contains, most of the time this will be `\craft\elements\Entry::class`
The element type that this index contains, most of the time this will be `craft\elements\Entry::class`

Craft's default element type classes are:

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "rias/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": "0.1.0",
"version": "0.1.1",
"keywords": [
"craft",
"cms",
Expand Down
17 changes: 11 additions & 6 deletions src/Scout.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use craft\base\Element;
use craft\events\ModelEvent;
use craft\records\Entry;
use rias\scout\jobs\DeIndexElement;
use rias\scout\jobs\IndexElement;
use rias\scout\services\ScoutService as ScoutServiceService;
use rias\scout\models\Settings;

Expand Down Expand Up @@ -64,8 +66,9 @@ public function init()
Element::class,
Element::EVENT_AFTER_SAVE,
function (ModelEvent $event) {
/* @var Element $event->sender */
$this->scoutService->indexElement($event->sender);
Craft::$app->queue->push(new IndexElement([
'element' => $event->sender,
]));
}
);

Expand All @@ -76,8 +79,9 @@ function (ModelEvent $event) {
Element::class,
Element::EVENT_AFTER_MOVE_IN_STRUCTURE,
function (ModelEvent $event) {
/* @var Element $event->sender */
$this->scoutService->indexElement($event->sender);
Craft::$app->queue->push(new IndexElement([
'element' => $event->sender,
]));
}
);

Expand All @@ -88,8 +92,9 @@ function (ModelEvent $event) {
Element::class,
Element::EVENT_BEFORE_DELETE,
function (ModelEvent $event) {
/* @var Element $event->sender */
$this->scoutService->deindexElement($event->sender);
Craft::$app->queue->push(new DeIndexElement([
'element' => $event->sender,
]));
}
);
}
Expand Down
55 changes: 55 additions & 0 deletions src/jobs/DeIndexElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Scout plugin for Craft CMS 3.x
*
* 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.
*
* @link https://rias.be
* @copyright Copyright (c) 2017 Rias
*/

namespace rias\scout\jobs;

use craft\base\Element;
use craft\queue\BaseJob;
use craft\queue\QueueInterface;
use rias\scout\Scout;

use Craft;

/**
* @author Rias
* @package Scout
* @since 0.1.0e
*/
class DeIndexElement extends BaseJob
{
// Properties
// =========================================================================

/* @var Element */
public $element;

// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function execute($queue)
{
/* @var Element $event->sender */
Scout::$plugin->scoutService->deindexElement($this->element);
}

// Protected Methods
// =========================================================================

/**
* @inheritdoc
*/
protected function defaultDescription(): string
{
return Craft::t('scout', 'De-indexing Element');
}
}
42 changes: 12 additions & 30 deletions src/tasks/ScoutTask.php → src/jobs/IndexElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,38 @@
* @copyright Copyright (c) 2017 Rias
*/

namespace rias\scout\tasks;
namespace rias\scout\jobs;

use craft\base\Element;
use craft\queue\BaseJob;
use craft\queue\QueueInterface;
use rias\scout\Scout;

use Craft;
use craft\base\Task;

/**
* @author Rias
* @package Scout
* @since 0.1.0
*/
class ScoutTask extends Task
class IndexElement extends BaseJob
{
// Public Properties
// Properties
// =========================================================================

/**
* @var string
*/
public $someAttribute = 'Some Default';
/* @var Element */
public $element;

// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function rules()
{
return [
['someAttribute', 'string'],
['someAttribute', 'default', 'value' => 'Some Default'],
];
}

/**
* @inheritdoc
*/
public function getTotalSteps(): int
{
return 1;
}

/**
* @inheritdoc
*/
public function runStep(int $step)
public function execute($queue)
{
return true;
/* @var Element $event->sender */
Scout::$plugin->scoutService->indexElement($this->element);
}

// Protected Methods
Expand All @@ -68,6 +50,6 @@ public function runStep(int $step)
*/
protected function defaultDescription(): string
{
return Craft::t('scout', 'ScoutTask');
return Craft::t('scout', 'Indexing Element');
}
}
32 changes: 1 addition & 31 deletions src/models/IndexModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,37 +143,7 @@ public function deindexElement(Element $element)
}

/**
* Adds or removes the supplied elements from the index.
*
* @param $elements array
*
* @return mixed
* @throws \AlgoliaSearch\AlgoliaException
*/
public function indexElements($elements)
{
$toIndex = [];
$toDelete = [];
array_map(function ($element) use (&$toIndex, &$toDelete) {
if ($this->canIndexElement($element)) {
if ($element->enabled) {
$toIndex[] = $this->transformElement($element);
} else {
$toDelete[] = $this->transformElement($element);
}
}
}, $elements);
if (count($toIndex)) {
$this->getIndex()->addObjects($toIndex);
}
if (count($toDelete)) {
$this->getIndex()->deleteObjects($toDelete);
}
return true;
}

/**
* Returns the transformer based on the given endpoint
* Returns the transformer
*
* @return callable|TransformerAbstract|object
* @throws \yii\base\InvalidConfigException
Expand Down

0 comments on commit 2f70499

Please sign in to comment.