Skip to content

Commit

Permalink
Allow customizing instant search
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Apr 19, 2024
1 parent 26e1332 commit 2ff485c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions config/services/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
$parameters = $containerConfigurator->parameters();
$parameters->set('webgriffe.sylius_elasticsearch_plugin.taxon_default_page_limit', 9);
$parameters->set('webgriffe.sylius_elasticsearch_plugin.search_default_page_limit', 9);
$parameters->set('webgriffe.sylius_elasticsearch_plugin.instant_search_page_limit', 8);
$parameters->set('webgriffe.sylius_elasticsearch_plugin.instant_search_completion_suggesters_size', 5);

$services = $containerConfigurator->services();

Expand Down Expand Up @@ -44,6 +46,8 @@
service('webgriffe.sylius_elasticsearch_plugin.provider.document_type'),
service('webgriffe.sylius_elasticsearch_plugin.builder.query'),
service('webgriffe.sylius_elasticsearch_plugin.mapper.query_result'),
param('webgriffe.sylius_elasticsearch_plugin.instant_search_page_limit'),
param('webgriffe.sylius_elasticsearch_plugin.instant_search_completion_suggesters_size'),
])
->call('setContainer', [service('service_container')])
->tag('controller.service_arguments')
Expand Down
1 change: 1 addition & 0 deletions src/Builder/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function buildSearchQuery(
public function buildCompletionSuggestersQuery(
string $searchTerm,
?string $source = 'suggest',
int $size = 5,
): array;

public function buildTermSuggestersQuery(
Expand Down
2 changes: 2 additions & 0 deletions src/Builder/TwigQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,13 @@ public function buildSearchQuery(
public function buildCompletionSuggestersQuery(
string $searchTerm,
?string $source = 'suggest',
int $size = 5,
): array {
$localeCode = $this->localeContext->getLocaleCode();
$query = $this->twig->render('@WebgriffeSyliusElasticsearchPlugin/query/completion-suggesters/query.json.twig', [
'searchTerm' => $searchTerm,
'localeCode' => $localeCode,
'size' => $size,
]);
$completionSuggestersQuery = [];
/** @var array $queryNormalized */
Expand Down
9 changes: 7 additions & 2 deletions src/Controller/InstantSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@
*/
final class InstantSearchController extends AbstractController implements InstantSearchControllerInterface
{
/**
* @param int<1,10_000> $maxResults
*/
public function __construct(
private readonly ClientInterface $client,
private readonly ChannelContextInterface $channelContext,
private readonly IndexNameGeneratorInterface $indexNameGenerator,
private readonly DocumentTypeProviderInterface $documentTypeProvider,
private readonly QueryBuilderInterface $queryBuilder,
private readonly QueryResultMapperInterface $queryResultMapper,
private readonly int $maxResults,
private readonly int $completionSuggestersSize,
) {
}

Expand All @@ -48,12 +53,12 @@ public function __invoke(Request $request, string $query): Response
}

$completionSuggesters = $this->client->completionSuggesters(
$this->queryBuilder->buildCompletionSuggestersQuery($query),
$this->queryBuilder->buildCompletionSuggestersQuery($query, 'suggest', $this->completionSuggestersSize),
$indexAliasNames,
);

$esResult = $this->client->query(
$this->queryBuilder->buildSearchQuery($query),
$this->queryBuilder->buildSearchQuery($query, null, $this->maxResults),
$indexAliasNames,
);
$queryResult = $this->queryResultMapper->map($esResult);
Expand Down
2 changes: 1 addition & 1 deletion templates/query/completion-suggesters/query.json.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"prefix": "{{ searchTerm }}",
"completion": {
"field": "suggest",
"size": 5,
"size": {{ size }},
"skip_duplicates": true,
"fuzzy": {
"fuzziness": 1,
Expand Down

0 comments on commit 2ff485c

Please sign in to comment.