Skip to content

Commit

Permalink
Search::shouldCallSearchElements()
Browse files Browse the repository at this point in the history
Resolves #14293
  • Loading branch information
brandonkelly committed Feb 14, 2024
1 parent 6efe0d9 commit cdfdbf4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Extensibility
- Added `craft\services\Relations::deleteLeftoverRelations()`. ([#13956](https://github.com/craftcms/cms/issues/13956))
- Added `craft\services\Search::shouldCallSearchElements()`. ([#14293](https://github.com/craftcms/cms/issues/14293))

### System
- Relations for fields that are no longer included in an element’s field layout are now deleted after element save. ([#13956](https://github.com/craftcms/cms/issues/13956))
8 changes: 5 additions & 3 deletions src/elements/db/ElementQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2776,9 +2776,11 @@ private function _applySearchParam(): void
return;
}

if (isset($this->orderBy['score'])) {
$searchService = Craft::$app->getSearch();

if (isset($this->orderBy['score']) || $searchService->shouldCallSearchElements($this)) {
// Get the scored results up front
$searchResults = Craft::$app->getSearch()->searchElements($this);
$searchResults = $searchService->searchElements($this);

if ($this->orderBy['score'] === SORT_ASC) {
$searchResults = array_reverse($searchResults, true);
Expand All @@ -2804,7 +2806,7 @@ private function _applySearchParam(): void
$this->subQuery->andWhere(['elements.id' => array_keys($searchResults)]);
} else {
// Just filter the main query by the search query
$searchQuery = Craft::$app->getSearch()->createDbQuery($this->search, $this);
$searchQuery = $searchService->createDbQuery($this->search, $this);

if ($searchQuery === false) {
throw new QueryAbortedException();
Expand Down
16 changes: 16 additions & 0 deletions src/services/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,22 @@ public function indexElementAttributes(ElementInterface $element, ?array $fieldH
return true;
}

/**
* Returns whether we should search for the resulting elements up front via [[searchElements()]],
* rather than supply a subquery which should be applied to the main element query via [[createDbQuery()]].
*
* If the element query is being ordered by `score`, [[searchElements()]] will be called regardless of
* what this returns.
*
* @param ElementQuery $elementQuery
* @return bool
* @since 4.8.0
*/
public function shouldCallSearchElements(ElementQuery $elementQuery): bool
{
return false;
}

/**
* Searches for elements that match the given element query.
*
Expand Down

0 comments on commit cdfdbf4

Please sign in to comment.