-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a80e84a
Showing
21 changed files
with
2,313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Release Notes for Elasticsearch Plugin | ||
|
||
## 1.0.0 - 2022-4-08 | ||
|
||
### Added | ||
|
||
- Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Copyright © Codemonauts | ||
|
||
Permission is hereby granted to any person obtaining a copy of this software (the “Software”) to use, copy, modify, merge, publish and/or distribute copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
Don’t plagiarize. The above copyright notice and this license shall be included in all copies or substantial portions of the Software. | ||
|
||
Don’t use the same license on more than one project. Each licensed copy of the Software shall be actively installed in no more than one production environment at a time. | ||
|
||
Don’t mess with the licensing features. Software features related to licensing shall not be altered or circumvented in any way, including (but not limited to) license validation, payment prompts, feature restrictions, and update eligibility. | ||
|
||
Pay up. Payment shall be made immediately upon receipt of any notice, prompt, reminder, or other message indicating that a payment is owed. | ||
|
||
Follow the law. All use of the Software shall not violate any applicable law or regulation, nor infringe the rights of any other person or entity. | ||
|
||
Failure to comply with the foregoing conditions will automatically and immediately result in termination of the permission granted hereby. This license does not include any right to receive updates to the Software or technical support. Licensees bear all risk related to the quality and performance of the Software and any modifications made or obtained to it, including liability for actual and consequential harm, such as loss or corruption of data, and any necessary service, repair, or correction. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, INCLUDING SPECIAL, INCIDENTAL AND CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Craft's search powered by Elasticsearch | ||
|
||
![Icon](resources/search.png) | ||
|
||
## Background | ||
|
||
Replace Craft's database full-text search with Elasticsearch. | ||
|
||
## Requirements | ||
|
||
* Craft CMS >= 3.6.18 | ||
|
||
## Installation | ||
|
||
Open your terminal and go to your Craft project: | ||
|
||
``` shell | ||
cd /path/to/project | ||
composer require codemonauts/craft-elasticsearch | ||
./craft install/plugin elastic | ||
``` | ||
|
||
## Usage | ||
|
||
For detailed usage information, [visit the docs](https://plugins.codemonauts.com/plugins/elasticsearch/Introduction.html). | ||
|
||
With ❤ by [codemonauts](https://codemonauts.com) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "codemonauts/craft-elasticsearch", | ||
"description": "Replace Craft's database full-text search with Elasticsearch.", | ||
"version": "1.0.0", | ||
"type": "craft-plugin", | ||
"keywords": [ | ||
"craft", | ||
"cms", | ||
"craftcms", | ||
"craft-plugin", | ||
"Elasticsearch" | ||
], | ||
"license": "proprietary", | ||
"authors": [ | ||
{ | ||
"name": "codemonauts", | ||
"homepage": "https://codemonauts.com" | ||
} | ||
], | ||
"support": { | ||
"source": "https://github.com/codemonauts/craft-elasticsearch", | ||
"docs": "https://plugins.codemonauts.com/plugins/elasticsearch/Introduction.html", | ||
"issues": "https://github.com/codemonauts/craft-elasticsearch/issues" | ||
}, | ||
"require": { | ||
"craftcms/cms": "^3.6.18", | ||
"elasticsearch/elasticsearch": "^7.0", | ||
"jsq/amazon-es-php": "^0.3.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"codemonauts\\elastic\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"handle": "elastic", | ||
"class": "codemonauts\\elastic\\Elastic", | ||
"name": "Search powered by Elasticsearch", | ||
"description": "Replace Craft's database full-text search with Elasticsearch.", | ||
"changelogUrl": "https://raw.githubusercontent.com/codemonauts/craft-elasticsearch/blob/main/CHANGELOG.md", | ||
"documentationUrl": "https://plugins.codemonauts.com/plugins/elasticsearch/Introduction.html" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
<?php | ||
|
||
namespace codemonauts\elastic; | ||
|
||
use codemonauts\elastic\jobs\UpdateMapping; | ||
use codemonauts\elastic\jobs\UpdateSearchIndex; | ||
use codemonauts\elastic\models\Settings; | ||
use codemonauts\elastic\services\Elasticsearch; | ||
use codemonauts\elastic\services\Indexes; | ||
use codemonauts\elastic\services\Search; | ||
use codemonauts\elastic\utilities\IndexUtility; | ||
use Craft; | ||
use craft\base\ElementInterface; | ||
use craft\base\Plugin; | ||
use craft\events\ElementEvent; | ||
use craft\events\RegisterComponentTypesEvent; | ||
use craft\helpers\App; | ||
use craft\helpers\ElementHelper; | ||
use craft\helpers\UrlHelper; | ||
use craft\services\Elements; | ||
use craft\services\Fields; | ||
use craft\services\Utilities; | ||
|
||
/** | ||
* @property Elasticsearch $elasticsearch | ||
* @property Indexes $indexes | ||
* @property Search $search | ||
*/ | ||
class Elastic extends Plugin | ||
{ | ||
/** | ||
* @var Elastic | ||
*/ | ||
public static $plugin; | ||
|
||
/** | ||
* @var Settings | ||
*/ | ||
public static $settings; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public $hasCpSettings = true; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function init(): void | ||
{ | ||
parent::init(); | ||
|
||
self::$plugin = $this; | ||
|
||
self::$settings = self::$plugin->getSettings(); | ||
|
||
// If no endpoint is set, do nothing. | ||
if (Elastic::env(self::$settings->endpoint) === '') { | ||
return; | ||
} | ||
|
||
// If not in transition mode, replace the Craft internal search component | ||
if (!self::$settings->transition) { | ||
$componentConfig = [ | ||
'search' => Search::class, | ||
]; | ||
Craft::$app->setComponents($componentConfig); | ||
} | ||
|
||
// Add the plugin search components | ||
$componentConfig = [ | ||
'elasticsearch' => [ | ||
'class' => Elasticsearch::class, | ||
'hosts' => [ | ||
Elastic::env(self::$settings->endpoint), | ||
], | ||
'authentication' => self::$settings->authentication, | ||
'username' => Elastic::env(self::$settings->username), | ||
'password' => Elastic::env(self::$settings->password), | ||
'region' => Elastic::env(self::$settings->region), | ||
], | ||
'indexes' => [ | ||
'class' => Indexes::class, | ||
'indexName' => Elastic::env(self::$settings->indexName), | ||
], | ||
'elements' => services\Elements::class, | ||
'search' => Search::class, | ||
]; | ||
$this->setComponents($componentConfig); | ||
|
||
// When in transition mode, add event to update Elasticsearch indexes as well. | ||
if (self::$settings->transition) { | ||
Craft::$app->elements->on(Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $event) { | ||
/** | ||
* @var ElementInterface $element | ||
*/ | ||
$element = $event->element; | ||
$elementType = get_class($element); | ||
|
||
if (ElementHelper::isDraftOrRevision($element)) { | ||
return; | ||
} | ||
|
||
if (count($element::searchableAttributes()) === 0) { | ||
return; | ||
} | ||
|
||
Craft::$app->getQueue()->push(new UpdateSearchIndex([ | ||
'elementType' => $elementType, | ||
'elementId' => $element->id, | ||
])); | ||
}); | ||
} | ||
|
||
// Register event when changing field definitions | ||
Craft::$app->fields->on(Fields::EVENT_AFTER_SAVE_FIELD, function() { | ||
Craft::$app->queue->push(new UpdateMapping()); | ||
}); | ||
|
||
// Register utilities | ||
Craft::$app->getUtilities()->on(Utilities::EVENT_REGISTER_UTILITY_TYPES, function(RegisterComponentTypesEvent $event) { | ||
$event->types[] = IndexUtility::class; | ||
}); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function afterInstall() | ||
{ | ||
parent::afterInstall(); | ||
|
||
if (Craft::$app->getRequest()->getIsConsoleRequest()) { | ||
return; | ||
} | ||
|
||
Craft::$app->getResponse()->redirect( | ||
UrlHelper::cpUrl('settings/plugins/elastic') | ||
)->send(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function createSettingsModel() | ||
{ | ||
return new Settings(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function settingsHtml(): ?string | ||
{ | ||
return Craft::$app->getView()->renderTemplate('elastic/settings', [ | ||
'settings' => $this->getSettings(), | ||
'authenticationOptions' => [ | ||
'aws' => 'AWS', | ||
'basicauth' => 'BasicAuth' | ||
] | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Parse environment string. Should be replaced with Craft's App::parseEnv after dropping 3.6 support. | ||
* | ||
* @param string|null $str The string to parse. | ||
* | ||
* @return array|string|null | ||
*/ | ||
public static function env(string $str = null) | ||
{ | ||
if ($str === null) { | ||
return null; | ||
} | ||
|
||
if (preg_match('/^\$(\w+)$/', $str, $matches)) { | ||
$value = App::env($matches[1]); | ||
if ($value !== false) { | ||
$str = $value; | ||
} | ||
} | ||
|
||
return $str; | ||
} | ||
|
||
public function getElasticsearch(): Elasticsearch | ||
{ | ||
return $this->get('elasticsearch'); | ||
} | ||
|
||
public function getIndexes(): Indexes | ||
{ | ||
return $this->get('indexes'); | ||
} | ||
|
||
public function getSearch(): Search | ||
{ | ||
return $this->get('search'); | ||
} | ||
|
||
public function getElements(): services\Elements | ||
{ | ||
return $this->get('elements'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace codemonauts\elastic\console\controllers; | ||
|
||
use codemonauts\elastic\jobs\UpdateSearchIndex; | ||
use Craft; | ||
use craft\base\ElementInterface; | ||
use craft\db\Query; | ||
use craft\db\Table; | ||
use craft\helpers\Console; | ||
use yii\base\NotSupportedException; | ||
use yii\console\Controller; | ||
|
||
class ElementsController extends Controller | ||
{ | ||
/** | ||
* Reindex elements to current index. | ||
* | ||
* @param string $siteHandle The site to index. Default '*' to reindex elements of all sites. | ||
* @param string $channel The queue channel to use. | ||
* @param int $priority The queue priority to use. | ||
*/ | ||
public function actionReindex(string $siteHandle = '*', string $channel = 'queue', int $priority = 2048) | ||
{ | ||
$queue = Craft::$app->$channel; | ||
$elementsTable = Table::ELEMENTS; | ||
|
||
/** | ||
* @var ElementInterface $elementType | ||
*/ | ||
$elementTypesToIndex = []; | ||
$elementTypes = Craft::$app->elements->getAllElementTypes(); | ||
foreach ($elementTypes as $elementType) { | ||
$attributes = $elementType::searchableAttributes(); | ||
if (!$elementType::hasTitles() && count($attributes) === 0) { | ||
continue; | ||
} | ||
$count = (new Query())->from($elementsTable)->where([ | ||
'dateDeleted' => null, | ||
'revisionId' => null, | ||
'type' => $elementType, | ||
])->count(); | ||
|
||
if ($this->confirm("Reindex all $count elements of type '$elementType'? ")) { | ||
$elementTypesToIndex[] = $elementType; | ||
} | ||
} | ||
|
||
foreach ($elementTypesToIndex as $type) { | ||
$query = (new Query())->select(['id', 'type']) | ||
->from($elementsTable) | ||
->where([ | ||
'dateDeleted' => null, | ||
'revisionId' => null, | ||
'type' => $type, | ||
]) | ||
->orderBy('dateCreated desc'); | ||
|
||
$total = $query->count(); | ||
$counter = 0; | ||
|
||
$this->stdout("Reindex $total elements of type '$type' ..." . PHP_EOL); | ||
|
||
Console::startProgress(0, $total); | ||
foreach ($query->batch() as $rows) { | ||
foreach ($rows as $element) { | ||
$job = new UpdateSearchIndex([ | ||
'elementType' => $element['type'], | ||
'elementId' => $element['id'], | ||
'siteId' => $siteHandle, | ||
]); | ||
try { | ||
$queue->priority($priority)->push($job); | ||
} catch (NotSupportedException $e) { | ||
$queue->push($job); | ||
} | ||
Console::updateProgress(++$counter, $total); | ||
} | ||
} | ||
Console::endProgress(); | ||
} | ||
} | ||
} |
Oops, something went wrong.