Skip to content

Commit

Permalink
feat(SearchEngine): Added DocumentSearchQueryEvent and `NodeSourceS…
Browse files Browse the repository at this point in the history
…earchQueryEvent` event to alter Solr select queries
  • Loading branch information
ambroisemaupate committed Apr 11, 2023
1 parent 51a25ec commit 083d2e5
Show file tree
Hide file tree
Showing 11 changed files with 421 additions and 67 deletions.
5 changes: 5 additions & 0 deletions lib/RoadizCoreBundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ services:
RZ\Roadiz\CoreBundle\SearchEngine\ClientRegistry:
arguments: ['@service_container']

RZ\Roadiz\CoreBundle\SearchEngine\SolariumLogger:
tags:
- { name: data_collector, template: '@RoadizCore/DataCollector/solarium.html.twig', id: 'solarium' }
- { name: monolog.logger, channel: solr }

RZ\Roadiz\CoreBundle\SearchEngine\Indexer\IndexerFactory:
arguments: ['@service_container']

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use RZ\Roadiz\CoreBundle\Entity\NodeType;
use RZ\Roadiz\CoreBundle\Entity\Translation;
use RZ\Roadiz\CoreBundle\Repository\NodesSourcesRepository;
use RZ\Roadiz\CoreBundle\SearchEngine\SolariumLogger;
use RZ\Roadiz\CoreBundle\Webhook\Message\GenericJsonPostMessage;
use RZ\Roadiz\CoreBundle\Webhook\Message\GitlabPipelineTriggerMessage;
use RZ\Roadiz\CoreBundle\Webhook\Message\NetlifyBuildHookMessage;
Expand Down Expand Up @@ -258,6 +259,7 @@ private function registerSolr(array $config, ContainerBuilder $container): void
}
}
if (count($solrEndpoints) > 0) {
$logger = new Reference(SolariumLogger::class);
$container->setDefinition(
'roadiz_core.solr.client',
(new Definition())
Expand All @@ -269,6 +271,7 @@ private function registerSolr(array $config, ContainerBuilder $container): void
new Reference('roadiz_core.solr.adapter'),
new Reference(EventDispatcherInterface::class)
])
->addMethodCall('registerPlugin', ['roadiz_core.solr.client.logger', $logger])
->addMethodCall('setEndpoints', [array_map(function (string $endpointId) {
return new Reference($endpointId);
}, $solrEndpoints)])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,26 @@
use Solarium\Core\Client\Client;
use Solarium\Core\Query\Helper;
use Solarium\QueryType\Select\Query\Query;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

abstract class AbstractSearchHandler implements SearchHandlerInterface
{
private ClientRegistry $clientRegistry;
protected ClientRegistry $clientRegistry;
protected ObjectManager $em;
protected LoggerInterface $logger;
protected EventDispatcherInterface $eventDispatcher;
protected int $highlightingFragmentSize = 150;

public function __construct(
ClientRegistry $clientRegistry,
ObjectManager $em,
LoggerInterface $searchEngineLogger
LoggerInterface $searchEngineLogger,
EventDispatcherInterface $eventDispatcher
) {
$this->clientRegistry = $clientRegistry;
$this->em = $em;
$this->logger = $searchEngineLogger;
$this->eventDispatcher = $eventDispatcher;
}

public function getSolr(): Client
Expand Down
58 changes: 31 additions & 27 deletions lib/RoadizCoreBundle/src/SearchEngine/DocumentSearchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use RZ\Roadiz\CoreBundle\Entity\Folder;
use RZ\Roadiz\CoreBundle\Entity\Translation;
use RZ\Roadiz\CoreBundle\SearchEngine\Event\DocumentSearchQueryEvent;

/**
* @package RZ\Roadiz\CoreBundle\SearchEngine
Expand All @@ -30,35 +31,38 @@ protected function nativeSearch(
int $proximity = 1,
int $page = 1
): ?array {
if (!empty($q)) {
$query = $this->createSolrQuery($args, $rows, $page);
$queryTxt = $this->buildQuery($q, $args, $searchTags, $proximity);
$query->setQuery($queryTxt);

/*
* Only need these fields as Doctrine
* will do the rest.
*/
$query->setFields([
'id',
'sort',
'document_type_s',
SolariumDocumentTranslation::IDENTIFIER_KEY,
'filename_s',
'locale_s',
]);

$this->logger->debug('[Solr] Request document search…', [
'query' => $queryTxt,
'fq' => $args["fq"] ?? [],
'params' => $query->getParams(),
]);

$solrRequest = $this->getSolr()->execute($query);
return $solrRequest->getData();
} else {
if (empty($q)) {
return null;
}
$query = $this->createSolrQuery($args, $rows, $page);
$queryTxt = $this->buildQuery($q, $args, $searchTags, $proximity);
$query->setQuery($queryTxt);

/*
* Only need these fields as Doctrine
* will do the rest.
*/
$query->setFields([
'id',
'sort',
'document_type_s',
SolariumDocumentTranslation::IDENTIFIER_KEY,
'filename_s',
'locale_s',
]);

$this->logger->debug('[Solr] Request document search…', [
'query' => $queryTxt,
'fq' => $args["fq"] ?? [],
'params' => $query->getParams(),
]);

$query = $this->eventDispatcher->dispatch(
new DocumentSearchQueryEvent($query, $args)
)->getQuery();

$solrRequest = $this->getSolr()->execute($query);
return $solrRequest->getData();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\SearchEngine\Event;

use Solarium\QueryType\Select\Query\Query;
use Symfony\Contracts\EventDispatcher\Event;

abstract class AbstractSearchQueryEvent extends Event
{
private Query $query;
private array $args;

public function __construct(Query $query, array $args)
{
$this->query = $query;
$this->args = $args;
}

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

/**
* @return array
*/
public function getArgs(): array
{
return $this->args;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\SearchEngine\Event;

final class DocumentSearchQueryEvent extends AbstractSearchQueryEvent
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\SearchEngine\Event;

final class NodeSourceSearchQueryEvent extends AbstractSearchQueryEvent
{
}
80 changes: 42 additions & 38 deletions lib/RoadizCoreBundle/src/SearchEngine/NodeSourceSearchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\Tag;
use RZ\Roadiz\CoreBundle\SearchEngine\Event\NodeSourceSearchQueryEvent;

/**
* @package RZ\Roadiz\CoreBundle\SearchEngine
Expand Down Expand Up @@ -37,49 +38,52 @@ protected function nativeSearch(
int $proximity = 1,
int $page = 1
): ?array {
if (!empty($q)) {
$query = $this->createSolrQuery($args, $rows, $page);
$queryTxt = $this->buildQuery($q, $args, $searchTags, $proximity);
if (empty($q)) {
return null;
}
$query = $this->createSolrQuery($args, $rows, $page);
$queryTxt = $this->buildQuery($q, $args, $searchTags, $proximity);

if ($this->boostByPublicationDate) {
$boost = '{!boost b=recip(ms(NOW,published_at_dt),3.16e-11,1,1)}';
$queryTxt = $boost . $queryTxt;
}
if ($this->boostByUpdateDate) {
$boost = '{!boost b=recip(ms(NOW,updated_at_dt),3.16e-11,1,1)}';
$queryTxt = $boost . $queryTxt;
}
if ($this->boostByCreationDate) {
$boost = '{!boost b=recip(ms(NOW,created_at_dt),3.16e-11,1,1)}';
$queryTxt = $boost . $queryTxt;
}
if ($this->boostByPublicationDate) {
$boost = '{!boost b=recip(ms(NOW,published_at_dt),3.16e-11,1,1)}';
$queryTxt = $boost . $queryTxt;
}
if ($this->boostByUpdateDate) {
$boost = '{!boost b=recip(ms(NOW,updated_at_dt),3.16e-11,1,1)}';
$queryTxt = $boost . $queryTxt;
}
if ($this->boostByCreationDate) {
$boost = '{!boost b=recip(ms(NOW,created_at_dt),3.16e-11,1,1)}';
$queryTxt = $boost . $queryTxt;
}

$query->setQuery($queryTxt);
$query->setQuery($queryTxt);

/*
* Only need these fields as Doctrine
* will do the rest.
*/
$query->setFields([
'score',
'id',
'document_type_s',
SolariumNodeSource::IDENTIFIER_KEY,
'node_name_s',
'locale_s',
]);
/*
* Only need these fields as Doctrine
* will do the rest.
*/
$query->setFields([
'score',
'id',
'document_type_s',
SolariumNodeSource::IDENTIFIER_KEY,
'node_name_s',
'locale_s',
]);

$this->logger->debug('[Solr] Request node-sources search…', [
'query' => $queryTxt,
'fq' => $args["fq"] ?? [],
'params' => $query->getParams(),
]);
$this->logger->debug('[Solr] Request node-sources search…', [
'query' => $queryTxt,
'fq' => $args["fq"] ?? [],
'params' => $query->getParams(),
]);

$solrRequest = $this->getSolr()->execute($query);
return $solrRequest->getData();
} else {
return null;
}
$query = $this->eventDispatcher->dispatch(
new NodeSourceSearchQueryEvent($query, $args)
)->getQuery();

$solrRequest = $this->getSolr()->execute($query);
return $solrRequest->getData();
}

/**
Expand Down
Loading

0 comments on commit 083d2e5

Please sign in to comment.