Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes for Elasticsearch Plugin

## Unreleased

### Added
- Event to manipulate the search parameters before querying Elasticsearch.

## 1.2.0 - 2022-06-15

### Added
Expand Down
13 changes: 13 additions & 0 deletions src/events/BeforeQueryEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace codemonauts\elastic\events;

use yii\base\Event;

class BeforeQueryEvent extends Event
{
/**
* @var array The params that will be sent to Elasticsearch.
*/
public array $params = [];
}
14 changes: 14 additions & 0 deletions src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace codemonauts\elastic\services;

use codemonauts\elastic\Elastic;
use codemonauts\elastic\events\BeforeQueryEvent;
use craft\base\Component;
use craft\base\ElementInterface;
use craft\models\Site;
Expand All @@ -17,6 +18,11 @@
*/
class Elements extends Component
{
/**
* @event BeforeQueryEvent The event that is triggered before the query is sent to ELasticsearch.
*/
public const EVENT_BEFORE_QUERY = 'beforeQuery';

/**
* Adds the keywords of an element to the Elasticsearch index of a site.
*
Expand Down Expand Up @@ -158,6 +164,14 @@ public function search(SearchQuery $searchQuery, array $scope, Site $site)
];
}

// Allow plugins to modify the query parameters
$event = new BeforeQueryEvent([
'params' => $params,
]);
$this->trigger(self::EVENT_BEFORE_QUERY, $event);
$params = $event->params;


return Elastic::$plugin->getElasticsearch()->getClient()->search($params);
}

Expand Down