Skip to content

Commit

Permalink
Merge pull request #21 from RRZE-Webteam/FAU-446
Browse files Browse the repository at this point in the history
[FAU-446] Support for Multi-Word Free Text Search
  • Loading branch information
zhyian authored Dec 24, 2024
2 parents 46e5105 + 4f02dfc commit 97b104c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
1 change: 0 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
</file>
<file src="src/Infrastructure/Repository/WpQueryArgsBuilder.php">
<RiskyTruthyFalsyComparison>
<code><![CDATA[!$languageCode]]></code>
<code><![CDATA[!$orderBy]]></code>
<code><![CDATA[!$taxonomy]]></code>
<code><![CDATA[$languageCode]]></code>
Expand Down
46 changes: 20 additions & 26 deletions src/Infrastructure/Repository/WpQueryArgsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -270,31 +267,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 = is_string($languageCode) && $languageCode
? [$metaKeyPrefix . $languageCode]
: [$metaKeyPrefix . MultilingualString::EN, $metaKeyPrefix . MultilingualString::DE];

$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
);
}

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
Expand Down

0 comments on commit 97b104c

Please sign in to comment.