Skip to content

Commit 0015c84

Browse files
committed
Add before query event
1 parent 89c3543 commit 0015c84

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Release Notes for Elasticsearch Plugin
22

3+
## Unreleased
4+
5+
### Added
6+
- Event to manipulate the search parameters before querying Elasticsearch.
7+
38
## 1.2.0 - 2022-06-15
49

510
### Added

src/events/BeforeQueryEvent.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace codemonauts\elastic\events;
4+
5+
use yii\base\Event;
6+
7+
class BeforeQueryEvent extends Event
8+
{
9+
/**
10+
* @var array The params that will be sent to Elasticsearch.
11+
*/
12+
public array $params = [];
13+
}

src/services/Elements.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace codemonauts\elastic\services;
44

55
use codemonauts\elastic\Elastic;
6+
use codemonauts\elastic\events\BeforeQueryEvent;
67
use craft\base\Component;
78
use craft\base\ElementInterface;
89
use craft\models\Site;
@@ -17,6 +18,11 @@
1718
*/
1819
class Elements extends Component
1920
{
21+
/**
22+
* @event BeforeQueryEvent The event that is triggered before the query is sent to ELasticsearch.
23+
*/
24+
public const EVENT_BEFORE_QUERY = 'beforeQuery';
25+
2026
/**
2127
* Adds the keywords of an element to the Elasticsearch index of a site.
2228
*
@@ -158,6 +164,14 @@ public function search(SearchQuery $searchQuery, array $scope, Site $site)
158164
];
159165
}
160166

167+
// Allow plugins to modify the query parameters
168+
$event = new BeforeQueryEvent([
169+
'params' => $params,
170+
]);
171+
$this->trigger(self::EVENT_BEFORE_QUERY, $event);
172+
$params = $event->params;
173+
174+
161175
return Elastic::$plugin->getElasticsearch()->getClient()->search($params);
162176
}
163177

src/services/Indexes.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,14 +461,13 @@ public function buildMapping(): array
461461
$mapping = [];
462462

463463
$predefinedAttributes = [
464-
'title' => 'text',
465-
'slug' => 'text',
466-
'postDate' => 'date'
464+
'title',
465+
'slug',
467466
];
468467

469-
foreach ($predefinedAttributes as $attribute => $type) {
468+
foreach ($predefinedAttributes as $attribute) {
470469
$mapping[$fieldPrefix . 'attribute_' . $attribute] = [
471-
'type' => $type,
470+
'type' => 'text',
472471
'analyzer' => 'standard',
473472
];
474473
}

0 commit comments

Comments
 (0)