Skip to content

Commit 83b31c6

Browse files
committed
rewrote matchAlwaysAvailable === true part to also use {terms} query parser
1 parent b96aee5 commit 83b31c6

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

src/lib/Query/Common/CriterionVisitor/LanguageCodeIn.php

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,24 @@ public function canVisit(Criterion $criterion)
3131
public function visit(Criterion $criterion, ?CriterionVisitor $subVisitor = null): string
3232
{
3333
/** @var Criterion\LanguageCode $criterion */
34-
if (!$criterion->matchAlwaysAvailable &&
35-
($criterion->operator === null || $criterion->operator === Operator::IN || $criterion->operator === Operator::EQ)
36-
) {
37-
$values = is_array($criterion->value) ? $criterion->value : [$criterion->value];
38-
// content_language_codes_ms is a string field which uses LowerCaseFilter.
39-
// Since {!terms} bypasses analysis, we must manually lowercase the values to match the index.
40-
/** @param bool|float|int|string $value */
41-
$values = array_map(static function ($value): string {
42-
return strtolower((string)$value);
43-
}, $values);
44-
45-
return sprintf(
46-
'_query_:"{!terms f=content_language_codes_ms}%s"',
47-
implode(',', $values)
48-
);
49-
}
50-
51-
$languageCodeExpressions = array_map(
52-
static function ($value) {
53-
return 'content_language_codes_ms:"' . $value . '"';
54-
},
55-
is_array($criterion->value) ? $criterion->value : [$criterion->value]
34+
$values = is_array($criterion->value) ? $criterion->value : [$criterion->value];
35+
// content_language_codes_ms is a string field which uses LowerCaseFilter.
36+
// Since {!terms} bypasses analysis, we must manually lowercase the values to match the index.
37+
/** @param bool|float|int|string $value */
38+
$values = array_map(static function ($value): string {
39+
return strtolower((string)$value);
40+
}, $values);
41+
42+
$termsQuery = sprintf(
43+
'_query_:"{!terms f=content_language_codes_ms}%s"',
44+
implode(',', $values)
5645
);
5746

5847
if ($criterion->matchAlwaysAvailable) {
59-
$languageCodeExpressions[] = 'content_always_available_b:true';
48+
return '(' . $termsQuery . ' OR content_always_available_b:true)';
6049
}
6150

62-
return '(' . implode(' OR ', $languageCodeExpressions) . ')';
51+
return $termsQuery;
6352
}
6453
}
6554

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Solr\Search\Query\Common\CriterionVisitor;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LanguageCode;
12+
use Ibexa\Contracts\Solr\Query\CriterionVisitor;
13+
use Ibexa\Solr\Query\Common\CriterionVisitor\LanguageCodeIn;
14+
use Ibexa\Tests\Solr\Search\Query\BaseCriterionVisitorTestCase;
15+
16+
class LanguageCodeInTest extends BaseCriterionVisitorTestCase
17+
{
18+
protected function getVisitor(): CriterionVisitor
19+
{
20+
return new LanguageCodeIn();
21+
}
22+
23+
protected function getSupportedCriterion(): LanguageCode
24+
{
25+
return new LanguageCode('eng-GB');
26+
}
27+
28+
public function provideDataForTestVisit(): iterable
29+
{
30+
yield 'Single language, match always available' => [
31+
'(_query_:"{!terms f=content_language_codes_ms}eng-gb" OR content_always_available_b:true)',
32+
new LanguageCode('eng-GB', true),
33+
];
34+
35+
yield 'Multiple languages, match always available' => [
36+
'(_query_:"{!terms f=content_language_codes_ms}eng-gb,pol-pl" OR content_always_available_b:true)',
37+
new LanguageCode(['eng-GB', 'pol-PL'], true),
38+
];
39+
40+
yield 'Multiple languages, do NOT match always available (optimization)' => [
41+
'_query_:"{!terms f=content_language_codes_ms}eng-gb,pol-pl"',
42+
new LanguageCode(['eng-GB', 'pol-PL'], false),
43+
];
44+
45+
yield 'Single language, do NOT match always available (optimization)' => [
46+
'_query_:"{!terms f=content_language_codes_ms}eng-gb"',
47+
new LanguageCode('eng-GB', false),
48+
];
49+
}
50+
}

0 commit comments

Comments
 (0)