Skip to content

Commit

Permalink
Fix for entries and products filter and documentation updates
Browse files Browse the repository at this point in the history
Migration now recreate indexes.
Entries and products are filtered in order to get any enabled element regardless of its status
Elasticsearch debug module is also enabled for frontend now.
  • Loading branch information
Alban Jubert committed Jul 4, 2019
1 parent 9146a01 commit c3e540d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Elasticsearch plugin for Craft CMS 3.x Changelog

## 1.2.0 - 2019-06-04

> {warning} This update introduce a way to honor post and expiry date for entries and products.
The default search query has been updated in order to filter indexed elements based on that fields.
If you used `ElasticsearchRecord::EVENT_BEFORE_SEARCH` event to alter the search query, please be sure to update to reflect these changes.
See README for more infos.
After plugin update, Elastisearch indexes will be rebuilt in order to take these changes in consideration.

### Added
- Craft CMS 3.2 compatibility

### Changed
- `postDate` and `expiryDate` are now available in search results
- Default search query has been updated in order to filter live elements
- All enabled entries and products are indexed now regardless of there live status.

### Fixed
- The search method now honor `postDate` and `expiryDate` to only show live elements

## 1.1.0 - 2019-03-25

> {warning} The way page content are indexed have changed and now rely on a Guzzle client implementation.
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ Each entry consists of the following attributes:
- `id`: unique ID of the result
- `title`: page title
- `url`: full url to the page
- `postDate` : date of element publication start date
- `expiryDate` : date of element expiration date
- `elementHandle`: the element handle name. Can be either `entry` or `product`
- `score`: entry result score
- `highlights`: array of highlighted content matching the query terms
Expand Down Expand Up @@ -411,7 +413,7 @@ You can get even more control over your additional data by listening to the foll
$query = $event->query;
// Customise the query params
$queryParams = $esRecord->getQueryParams($query);
$queryParams['multi_match']['fields'] = ArrayHelper::merge($queryParams['multi_match']['fields'], ['color']);
$queryParams['bool']['must'][0]['multi_match']['fields'] = ArrayHelper::merge($queryParams['bool']['must'][0]['multi_match']['fields'], ['color']);
$esRecord->setQueryParams($queryParams);
// Customise the highlighter params
$highlightParams = $esRecord->getHighlightParams();
Expand Down
26 changes: 13 additions & 13 deletions src/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ function (RegisterUrlRulesEvent $event) {
self::EVENT_ERROR_NO_ATTACHMENT_PROCESSOR,
function () {
$application = Craft::$app;

if ($application instanceof \yii\web\Application) {
$application->getSession()->setError(Craft::t(
self::TRANSLATION_CATEGORY,
Expand All @@ -197,18 +196,19 @@ function () {
}
);

if (YII_DEBUG) {
// Add the Elasticsearch panel to the Yii debug bar
Event::on(
Application::class,
Application::EVENT_BEFORE_REQUEST,
function () {
/** @var \yii\debug\Module $debugModule */
$debugModule = Craft::$app->getModule('debug');
$debugModule->panels['elasticsearch'] = new DebugPanel(['module' => $debugModule]);
}
);
}
}

if (YII_DEBUG) {
// Add the Elasticsearch panel to the Yii debug bar
Event::on(
Application::class,
Application::EVENT_BEFORE_REQUEST,
function () {
/** @var \yii\debug\Module $debugModule */
$debugModule = Craft::$app->getModule('debug');
$debugModule->panels['elasticsearch'] = new DebugPanel(['module' => $debugModule]);
}
);
}

// Register variables
Expand Down
8 changes: 7 additions & 1 deletion src/migrations/m190602_000000_recreate_indexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class m190602_000000_recreate_indexes extends Migration
{
public function safeUp()
{
Elasticsearch::getInstance()->service->recreateIndexesForAllSites();
// Indexes need to be updated to take new fields in consideration
$elasticsearch = Elasticsearch::getInstance();
$elasticsearch->service->recreateIndexesForAllSites();
$elasticsearch->reindexQueueManagementService->enqueueReindexJobs($elasticsearch->service->getEnabledEntries());
if ($elasticsearch->isCommerceEnabled()) {
$elasticsearch->reindexQueueManagementService->enqueueReindexJobs($elasticsearch->service->getEnabledProducts());
}
}
}
10 changes: 8 additions & 2 deletions src/services/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ public function isIndexInSync(): bool
ElasticsearchRecord::$siteId = $site->id;

$countEntries = (int)Entry::find()
->status(Entry::STATUS_ENABLED)
->anyStatus()
->enabledForSite()
->typeId(ArrayHelper::merge(['not'], $blacklistedEntryTypes))
->count();
if (ElasticsearchPlugin::getInstance()->isCommerceEnabled()) {
$countEntries += (int)Product::find()
->status(Entry::STATUS_ENABLED)
->anyStatus()
->enabledForSite()
->count();
}
$countEsRecords = (int)ElasticsearchRecord::find()->count();
Expand Down Expand Up @@ -484,6 +486,8 @@ public function getEnabledEntries($siteIds = null): array
$siteEntries = Entry::find()
->select(['elements.id as elementId', 'elements_sites.siteId'])
->siteId($siteId)
->anyStatus()
->enabledForSite()
->asArray(true)
->all();
$entries = ArrayHelper::merge($entries, $siteEntries);
Expand Down Expand Up @@ -513,6 +517,8 @@ public function getEnabledProducts($siteIds = null): array
$siteEntries = Product::find()
->select(['commerce_products.id as elementId', 'elements_sites.siteId'])
->siteId($siteId)
->anyStatus()
->enabledForSite()
->asArray(true)
->all();
$products = ArrayHelper::merge($products, $siteEntries);
Expand Down

0 comments on commit c3e540d

Please sign in to comment.