Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable the deprecated include_type_name parameter for ES8 #34

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion .craftplugin
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions .gitignore
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
!.vscode/extensions.json
config.codekit3
prepros-6.config
composer.lock
Empty file modified CHANGELOG.md
100755 → 100644
Empty file.
Empty file modified LICENSE.md
100755 → 100644
Empty file.
6 changes: 5 additions & 1 deletion README.md
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ 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.

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

Expand Down
16 changes: 14 additions & 2 deletions composer.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
}
],
"require": {
"craftcms/cms": "^4.0.0",
"craftcms/cms": "^5.0",
"yiisoft/yii2-elasticsearch": "^2.1.0"
},
"require-dev": {
"craftcms/phpstan": "dev-main",
"craftcms/rector": "dev-main"
},
"autoload": {
"psr-4": {
"lhs\\elasticsearch\\": "src/"
Expand All @@ -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
}
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- vendor/craftcms/phpstan/phpstan.neon

parameters:
level: 0
paths:
- src
35 changes: 27 additions & 8 deletions src/Elasticsearch.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
/**
* Elasticsearch plugin for Craft CMS 3.x
*
* Bring the power of Elasticsearch to you Craft 3 CMS project
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
Expand Down Expand Up @@ -84,7 +82,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,
Expand All @@ -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']);
Expand Down Expand Up @@ -134,7 +149,7 @@ function (ExecEvent $event) {
// Register the plugin's CP utility
Event::on(
Utilities::class,
Utilities::EVENT_REGISTER_UTILITY_TYPES,
Utilities::EVENT_REGISTER_UTILITIES,
function (RegisterComponentTypesEvent $event) {
$event->types[] = RefreshElasticsearchIndexUtility::class;
}
Expand Down Expand Up @@ -237,7 +252,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 [
Expand Down Expand Up @@ -378,8 +393,12 @@ public function onElementSaved(ModelEvent $event): void
}

if ($notDraftOrRevision) {
if ($element->enabled) {
$this->reindexQueueManagementService->enqueueJob($element->id, $element->siteId, get_class($element));
if ($element->enabled && $element->getEnabledForSite()) {

// 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);
Expand Down
4 changes: 1 addition & 3 deletions src/config.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
/**
* Elasticsearch plugin for Craft CMS 3.x
*
* Bring the power of Elasticsearch to you Craft 3 CMS project
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
Expand Down
8 changes: 4 additions & 4 deletions src/console/controllers/ElasticsearchController.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
/**
* Elasticsearch plugin for Craft CMS 3.x
*
* Bring the power of Elasticsearch to you Craft 3 CMS project
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
Expand Down Expand Up @@ -196,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;
}
}
15 changes: 10 additions & 5 deletions src/controllers/CpController.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
/**
* Elasticsearch plugin for Craft CMS 3.x
*
* Bring the power of Elasticsearch to you Craft 3 CMS project
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
Expand Down Expand Up @@ -93,7 +91,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());
}
Expand Down Expand Up @@ -181,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__);
Expand Down
4 changes: 1 addition & 3 deletions src/events/ErrorEvent.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
/**
* Elasticsearch plugin for Craft CMS 3.x
*
* Bring the power of Elasticsearch to you Craft 3 CMS project
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
Expand Down
4 changes: 1 addition & 3 deletions src/exceptions/IndexElementException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
/**
* Elasticsearch plugin for Craft CMS 3.x
*
* Bring the power of Elasticsearch to you Craft 3 CMS project
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
Expand Down
9 changes: 5 additions & 4 deletions src/jobs/IndexElementJob.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
/**
* Elasticsearch plugin for Craft CMS 3.x
*
* Bring the power of Elasticsearch to you Craft 3 CMS project
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
Expand Down Expand Up @@ -42,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);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/migrations/m200929_155818_project_config_support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
);
Expand Down
9 changes: 2 additions & 7 deletions src/models/IndexableElementModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class IndexableElementModel extends \craft\base\Model implements \JsonSerializab
public $type;

/**
* @return Element
* @return Element|null
* @throws IndexableElementModelException
*/
public function getElement()
Expand Down Expand Up @@ -51,15 +51,10 @@ public function getElement()
throw new IndexableElementModelException($this, IndexableElementModelException::UNEXPECTED_TYPE);
}

if ($element === null) {
throw new IndexableElementModelException($this, IndexableElementModelException::ELEMENT_NOT_FOUND);
}

return $element;
}


public function jsonSerialize()
public function jsonSerialize(): mixed
{
return $this->toArray();
}
Expand Down
4 changes: 1 addition & 3 deletions src/models/SettingsModel.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
/**
* Elasticsearch plugin for Craft CMS 3.x
*
* Bring the power of Elasticsearch to you Craft 3 CMS project
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
Expand Down
11 changes: 7 additions & 4 deletions src/records/ElasticsearchRecord.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
/**
* Elasticsearch plugin for Craft CMS 3.x
*
* Bring the power of Elasticsearch to you Craft 3 CMS project
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
Expand Down Expand Up @@ -302,7 +300,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));
}
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/resources/CpAssetBundle.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
/**
* Elasticsearch plugin for Craft CMS 3.x
*
* Bring the power of Elasticsearch to you Craft 3 CMS project
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
Expand Down
4 changes: 1 addition & 3 deletions src/resources/cp/css/elastic-branding.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/**
* Elasticsearch plugin for Craft CMS 3.x
*
* Bring the power of Elasticsearch to you Craft 3 CMS project
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
Expand Down
4 changes: 1 addition & 3 deletions src/resources/cp/js/utilities/reindex.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/**
* Elasticsearch plugin for Craft CMS 3.x
*
* Bring the power of Elasticsearch to you Craft 3 CMS project
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
Expand Down
4 changes: 1 addition & 3 deletions src/services/ElasticsearchService.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
/** @noinspection PhpUndefinedClassInspection */

/**
* Elasticsearch plugin for Craft CMS 3.x
*
* Bring the power of Elasticsearch to you Craft 3 CMS project
* Elasticsearch plugin for Craft CMS.
*
* @link https://www.lahautesociete.com
* @copyright Copyright (c) 2018 La Haute Société
Expand Down
Loading