Skip to content

Commit

Permalink
[FEATURE] send an event for suggest queries
Browse files Browse the repository at this point in the history
This change adds a `AfterSearchQueryHasBeenPreparedEvent` for the suggest event.

Resolves: #3902
  • Loading branch information
dmitryd authored Dec 4, 2023
1 parent 8440800 commit 2344dd5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Classes/Domain/Search/Suggest/SuggestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService;
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequest;
use ApacheSolrForTypo3\Solr\Event\Search\AfterSuggestQueryHasBeenPreparedEvent;
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
use ApacheSolrForTypo3\Solr\Search;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use ApacheSolrForTypo3\Solr\System\Solr\ParsingUtil;
use ApacheSolrForTypo3\Solr\Util;
use Doctrine\DBAL\Exception as DBALException;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
Expand Down Expand Up @@ -81,7 +83,7 @@ public function getSuggestions(SearchRequest $searchRequest, array $additionalFi
$frontendUserGroupIds = Util::getFrontendUserGroups();

$suggestQuery = $this->queryBuilder->buildSuggestQuery($searchRequest->getRawUserQuery(), $additionalFilters, $requestId, $frontendUserGroupIds);
$solrSuggestions = $this->getSolrSuggestions($suggestQuery);
$solrSuggestions = $this->getSolrSuggestions($suggestQuery, $searchRequest);

if ($solrSuggestions === []) {
return ['status' => false];
Expand Down Expand Up @@ -146,12 +148,17 @@ protected function addTopResultsToSuggestions(SearchRequest $searchRequest, arra
* @throws NoSolrConnectionFoundException
* @throws DBALException
*/
protected function getSolrSuggestions(SuggestQuery $suggestQuery): array
protected function getSolrSuggestions(SuggestQuery $suggestQuery, SearchRequest $searchRequest): array
{
$pageId = $this->tsfe->getRequestedId();
$languageId = $this->tsfe->getLanguage()->getLanguageId();
$solr = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($pageId, $languageId);
$search = GeneralUtility::makeInstance(Search::class, $solr);

$event = new AfterSuggestQueryHasBeenPreparedEvent($suggestQuery, $searchRequest, $search, $this->typoScriptConfiguration);
$event = GeneralUtility::makeInstance(EventDispatcherInterface::class)->dispatch($event);
$suggestQuery = $event->getQuery();

$response = $search->search($suggestQuery, 0, 0);

$rawResponse = $response->getRawResponse();
Expand Down
63 changes: 63 additions & 0 deletions Classes/Event/Search/AfterSuggestQueryHasBeenPreparedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace ApacheSolrForTypo3\Solr\Event\Search;

use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query;
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequest;
use ApacheSolrForTypo3\Solr\Search;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;

/**
* Main event when a suggest query is done by a user in the Frontend.
*
* This is commonly used to add components or modify the actual query.
*/
final class AfterSuggestQueryHasBeenPreparedEvent
{
public function __construct(
private Query $query,
private readonly SearchRequest $searchRequest,
private readonly Search $search,
private readonly TypoScriptConfiguration $typoScriptConfiguration
) {}

public function getQuery(): Query
{
return $this->query;
}

public function setQuery(Query $query): void
{
$this->query = $query;
}

public function getSearchRequest(): SearchRequest
{
return $this->searchRequest;
}

public function getSearch(): Search
{
return $this->search;
}

public function getTypoScriptConfiguration(): TypoScriptConfiguration
{
return $this->typoScriptConfiguration;
}
}

0 comments on commit 2344dd5

Please sign in to comment.