From c5de598f6bbea279b2c3a351929473cd95365327 Mon Sep 17 00:00:00 2001 From: Oleksandr Zhyian Date: Fri, 20 Dec 2024 13:09:33 +0200 Subject: [PATCH 1/2] feat: update search keyword filter to handle multi-word queries --- .../Repository/WpQueryArgsBuilder.php | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/src/Infrastructure/Repository/WpQueryArgsBuilder.php b/src/Infrastructure/Repository/WpQueryArgsBuilder.php index 8f3f41d..140dbe4 100644 --- a/src/Infrastructure/Repository/WpQueryArgsBuilder.php +++ b/src/Infrastructure/Repository/WpQueryArgsBuilder.php @@ -270,31 +270,28 @@ private function applyTaxonomyFilter(Filter $filter, WpQueryArgs $queryArgs): Wp private function applySearchFilter(SearchKeywordFilter $filter, WpQueryArgs $queryArgs, ?string $languageCode = null): WpQueryArgs { - if (!$languageCode) { - return $queryArgs->withMetaQueryItem( - [ - 'relation' => 'OR', - [ - 'key' => 'fau_degree_program_searchable_content_' . MultilingualString::EN, - 'value' => $filter->value(), - 'compare' => 'LIKE', - ], - [ - 'key' => 'fau_degree_program_searchable_content_' . MultilingualString::DE, - 'value' => $filter->value(), - 'compare' => 'LIKE', - ], - ] - ); - } + $keywords = array_filter(array_map('trim', explode(' ', $filter->value()))); + $metaKeyPrefix = 'fau_degree_program_searchable_content_'; + $metaKeys = $languageCode + ? [$metaKeyPrefix . $languageCode] + : [$metaKeyPrefix . MultilingualString::EN, $metaKeyPrefix . MultilingualString::DE]; + + $metaQuery = array_reduce($keywords, static function ($metaQuery, $keyword) use ($metaKeys): array { + $keywordConditions = array_values(array_map( + static fn($key) => [ + 'key' => $key, + 'value' => $keyword, + 'compare' => 'LIKE', + ], + $metaKeys + )); - return $queryArgs->withMetaQueryItem( - [ - 'key' => 'fau_degree_program_searchable_content_' . $languageCode, - 'value' => $filter->value(), - 'compare' => 'LIKE', - ] - ); + $metaQuery[] = ['relation' => 'OR'] + $keywordConditions; + + return $metaQuery; + }, ['relation' => 'AND']); + + return $queryArgs->withMetaQueryItem($metaQuery); } private function currentTerm(CollectionCriteria $criteria): ?WP_Term From 4f02dfc748855136e1332015e58bad83dcf8c6a2 Mon Sep 17 00:00:00 2001 From: Oleksandr Zhyian Date: Fri, 20 Dec 2024 15:08:55 +0200 Subject: [PATCH 2/2] refactor: improve code readability --- psalm-baseline.xml | 1 - src/Infrastructure/Repository/WpQueryArgsBuilder.php | 11 ++++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 24e38e5..24c3ea5 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -38,7 +38,6 @@ - diff --git a/src/Infrastructure/Repository/WpQueryArgsBuilder.php b/src/Infrastructure/Repository/WpQueryArgsBuilder.php index 140dbe4..52f5a36 100644 --- a/src/Infrastructure/Repository/WpQueryArgsBuilder.php +++ b/src/Infrastructure/Repository/WpQueryArgsBuilder.php @@ -17,15 +17,12 @@ use Fau\DegreeProgram\Common\Application\Filter\SubjectGroupFilter; use Fau\DegreeProgram\Common\Application\Filter\TeachingLanguageFilter; use Fau\DegreeProgram\Common\Application\Repository\CollectionCriteria; -use Fau\DegreeProgram\Common\Domain\CampoKeys; use Fau\DegreeProgram\Common\Domain\DegreeProgram; use Fau\DegreeProgram\Common\Domain\MultilingualString; use Fau\DegreeProgram\Common\Infrastructure\Content\PostType\DegreeProgramPostType; use Fau\DegreeProgram\Common\Infrastructure\Content\Taxonomy\BachelorOrTeachingDegreeAdmissionRequirementTaxonomy; use Fau\DegreeProgram\Common\Infrastructure\Content\Taxonomy\MasterDegreeAdmissionRequirementTaxonomy; use Fau\DegreeProgram\Common\Infrastructure\Content\Taxonomy\TaxonomiesList; -use Fau\DegreeProgram\Common\Infrastructure\Content\Taxonomy\TeachingDegreeHigherSemesterAdmissionRequirementTaxonomy; -use RuntimeException; use WP_Term; /** @@ -272,19 +269,19 @@ private function applySearchFilter(SearchKeywordFilter $filter, WpQueryArgs $que { $keywords = array_filter(array_map('trim', explode(' ', $filter->value()))); $metaKeyPrefix = 'fau_degree_program_searchable_content_'; - $metaKeys = $languageCode + $metaKeys = is_string($languageCode) && $languageCode ? [$metaKeyPrefix . $languageCode] : [$metaKeyPrefix . MultilingualString::EN, $metaKeyPrefix . MultilingualString::DE]; - $metaQuery = array_reduce($keywords, static function ($metaQuery, $keyword) use ($metaKeys): array { - $keywordConditions = array_values(array_map( + $metaQuery = array_reduce($keywords, static function (array $metaQuery, string $keyword) use ($metaKeys): array { + $keywordConditions = array_map( static fn($key) => [ 'key' => $key, 'value' => $keyword, 'compare' => 'LIKE', ], $metaKeys - )); + ); $metaQuery[] = ['relation' => 'OR'] + $keywordConditions;