-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
156 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Meilisearch\Query; | ||
|
||
use Meilisearch\Contracts\SearchQuery; | ||
use Setono\SyliusMeilisearchPlugin\Config\Index; | ||
use Setono\SyliusMeilisearchPlugin\Document\Metadata\Facet; | ||
use Setono\SyliusMeilisearchPlugin\Engine\SearchRequest; | ||
use Setono\SyliusMeilisearchPlugin\Meilisearch\Filter\FilterBuilderInterface; | ||
|
||
final class MultiSearchBuilder implements MultiSearchBuilderInterface | ||
{ | ||
public function __construct( | ||
private readonly SearchQueryBuilderInterface $searchQueryBuilder, | ||
private readonly FilterBuilderInterface $filterBuilder, | ||
private readonly int $hitsPerPage, | ||
) { | ||
} | ||
|
||
public function build(Index $index, SearchRequest $searchRequest): array | ||
{ | ||
$metadata = $index->metadata(); | ||
$facetsNames = $metadata->getFacetableAttributeNames(); | ||
$facets = $metadata->getFacetableAttributes(); | ||
|
||
$filters = $this->filterBuilder->build($facets, $searchRequest->filters); | ||
|
||
return array_merge( | ||
[$this->buildSearchQuery($index, $searchRequest, $facetsNames, $filters)], | ||
$this->buildFacetQueries($index, $searchRequest, $facets, $searchRequest->filters), | ||
); | ||
} | ||
|
||
/** | ||
* @param list<string> $facetNames | ||
* @param list<string> $filters | ||
*/ | ||
private function buildSearchQuery(Index $index, SearchRequest $searchRequest, array $facetNames, array $filters): SearchQuery | ||
{ | ||
$query = $this->searchQueryBuilder | ||
->build($index->name, $searchRequest->query, $facetNames, $filters) | ||
->setHitsPerPage($this->hitsPerPage) | ||
->setPage($searchRequest->page) | ||
; | ||
|
||
if (null !== $searchRequest->sort) { | ||
$query->setSort([$searchRequest->sort]); | ||
} | ||
|
||
return $query; | ||
} | ||
|
||
/** | ||
* @param array<string, Facet> $facets | ||
* @param array<string, mixed> $filters | ||
* | ||
* @return list<SearchQuery> | ||
*/ | ||
private function buildFacetQueries(Index $index, SearchRequest $searchRequest, array $facets, array $filters): array | ||
{ | ||
$searchQueries = []; | ||
|
||
foreach ($facets as $facet) { | ||
/** @var array<string, mixed> $filteredFacets */ | ||
$filteredFacets = array_filter($filters, static fn ($value) => $value !== $facet->name, \ARRAY_FILTER_USE_KEY); | ||
|
||
$searchQueries[] = $this->searchQueryBuilder->build( | ||
indexName: $index->uid(), | ||
query: $searchRequest->query, | ||
facets: [$facet->name], | ||
filter: $this->filterBuilder->build($facets, $filteredFacets), | ||
) | ||
->setLimit(1) | ||
; | ||
} | ||
|
||
return $searchQueries; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Meilisearch\Query; | ||
|
||
use Meilisearch\Contracts\SearchQuery; | ||
use Setono\SyliusMeilisearchPlugin\Config\Index; | ||
use Setono\SyliusMeilisearchPlugin\Engine\SearchRequest; | ||
|
||
interface MultiSearchBuilderInterface | ||
{ | ||
/** | ||
* Will build a multi search query that handles the search and the facets | ||
* | ||
* todo explain somewhere why this is necessary | ||
* | ||
* @return list<SearchQuery> | ||
*/ | ||
public function build(Index $index, SearchRequest $searchRequest): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.