From 4a32ef4058109cd9d2e6fe5ad09ff0976833790d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20S=C5=82omka?= Date: Wed, 29 Nov 2023 13:56:23 +0100 Subject: [PATCH 1/7] Added support of % on both sides of like statement in fulltext criterion --- .../Gateway/CriterionHandler/FullText.php | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php b/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php index 2b17830ac3..c422a3471f 100644 --- a/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php +++ b/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php @@ -146,17 +146,21 @@ protected function tokenizeString($string) */ protected function getWordExpression(QueryBuilder $query, string $token): string { - if ($this->configuration['enableWildcards'] && $token[0] === '*') { - return $query->expr()->like( - 'word', - $query->createNamedParameter('%' . substr($token, 1)) - ); - } + if ($this->configuration['enableWildcards']) { + $hasLeadingWildcard = $token[0] === '*'; + $hasTrailingWildcard = $token[strlen($token) - 1] === '*'; + + $token = $hasLeadingWildcard ? substr($token, 1) : $token; + $token = $hasTrailingWildcard ? substr($token, 0, -1) : $token; + + $token = str_replace('%', '\\%', $token); + + $token = $hasLeadingWildcard ? '%' . $token : $token; + $token = $hasTrailingWildcard ? $token . '%' : $token; - if ($this->configuration['enableWildcards'] && $token[strlen($token) - 1] === '*') { return $query->expr()->like( 'word', - $query->createNamedParameter(substr($token, 0, -1) . '%') + $query->createNamedParameter($token) ); } From f7f4fc3cefc4733e8dfdf271082fcbfcf82a8083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20S=C5=82omka?= Date: Wed, 29 Nov 2023 14:19:39 +0100 Subject: [PATCH 2/7] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Paweł Niedzielski --- .../Content/Common/Gateway/CriterionHandler/FullText.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php b/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php index c422a3471f..b2de8bea80 100644 --- a/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php +++ b/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php @@ -147,8 +147,8 @@ protected function tokenizeString($string) protected function getWordExpression(QueryBuilder $query, string $token): string { if ($this->configuration['enableWildcards']) { - $hasLeadingWildcard = $token[0] === '*'; - $hasTrailingWildcard = $token[strlen($token) - 1] === '*'; + $hasLeadingWildcard = str_starts_with($token, '*'); + $hasTrailingWildcard = str_ends_with($token, '*'); $token = $hasLeadingWildcard ? substr($token, 1) : $token; $token = $hasTrailingWildcard ? substr($token, 0, -1) : $token; From 9803516f6b2fcdd161daf37d6b21498bb831743f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20S=C5=82omka?= Date: Thu, 30 Nov 2023 14:11:00 +0100 Subject: [PATCH 3/7] Restored checking if token contains wildcards --- .../Legacy/Content/Common/Gateway/CriterionHandler/FullText.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php b/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php index b2de8bea80..3662720902 100644 --- a/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php +++ b/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php @@ -146,7 +146,7 @@ protected function tokenizeString($string) */ protected function getWordExpression(QueryBuilder $query, string $token): string { - if ($this->configuration['enableWildcards']) { + if ($this->configuration['enableWildcards'] && str_contains('*', $token)) { $hasLeadingWildcard = str_starts_with($token, '*'); $hasTrailingWildcard = str_ends_with($token, '*'); From 3b098924e93ad2a529d20dff4388bb20cff4ef6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20S=C5=82omka?= Date: Fri, 1 Dec 2023 09:02:13 +0100 Subject: [PATCH 4/7] Tests --- .../integration/Core/Repository/SearchServiceFulltextTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/integration/Core/Repository/SearchServiceFulltextTest.php b/tests/integration/Core/Repository/SearchServiceFulltextTest.php index a1af8b37a8..976404b62e 100644 --- a/tests/integration/Core/Repository/SearchServiceFulltextTest.php +++ b/tests/integration/Core/Repository/SearchServiceFulltextTest.php @@ -165,6 +165,10 @@ public function providerForTestFulltextSearchSolr7(): array 'qui*', [[1, 5, 6, 7, 11, 12, 13, 15]], ], + [ + '*row*', + [[2,5,8,9,11,12,14,15]], + ], [ '+qui* +fox', [6, [11, 13], 15], From 44859ab244556d57b7e78d0ffc8f059f1a7e671c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20S=C5=82omka?= Date: Fri, 1 Dec 2023 10:58:41 +0100 Subject: [PATCH 5/7] Fixed code styles --- tests/integration/Core/Repository/SearchServiceFulltextTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/Core/Repository/SearchServiceFulltextTest.php b/tests/integration/Core/Repository/SearchServiceFulltextTest.php index 976404b62e..15725d4208 100644 --- a/tests/integration/Core/Repository/SearchServiceFulltextTest.php +++ b/tests/integration/Core/Repository/SearchServiceFulltextTest.php @@ -167,7 +167,7 @@ public function providerForTestFulltextSearchSolr7(): array ], [ '*row*', - [[2,5,8,9,11,12,14,15]], + [[2, 5, 8, 9, 11, 12, 14, 15]], ], [ '+qui* +fox', From 889f301c653e9f931009e9397f95358c01f3728f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20S=C5=82omka?= Date: Fri, 1 Dec 2023 11:53:37 +0100 Subject: [PATCH 6/7] Fixed argument position for str_contains --- .../Legacy/Content/Common/Gateway/CriterionHandler/FullText.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php b/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php index 3662720902..0797ea9f60 100644 --- a/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php +++ b/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php @@ -146,7 +146,7 @@ protected function tokenizeString($string) */ protected function getWordExpression(QueryBuilder $query, string $token): string { - if ($this->configuration['enableWildcards'] && str_contains('*', $token)) { + if ($this->configuration['enableWildcards'] && str_contains($token, '*')) { $hasLeadingWildcard = str_starts_with($token, '*'); $hasTrailingWildcard = str_ends_with($token, '*'); From 9aca6a1040f57bb0e2f0763445fac13ea4592606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20S=C5=82omka?= Date: Fri, 1 Dec 2023 12:07:36 +0100 Subject: [PATCH 7/7] Applied code review suggestion --- .../Content/Common/Gateway/CriterionHandler/FullText.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php b/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php index 0797ea9f60..8c4e07d8e5 100644 --- a/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php +++ b/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FullText.php @@ -146,10 +146,9 @@ protected function tokenizeString($string) */ protected function getWordExpression(QueryBuilder $query, string $token): string { - if ($this->configuration['enableWildcards'] && str_contains($token, '*')) { - $hasLeadingWildcard = str_starts_with($token, '*'); - $hasTrailingWildcard = str_ends_with($token, '*'); - + $hasLeadingWildcard = str_starts_with($token, '*'); + $hasTrailingWildcard = str_ends_with($token, '*'); + if ($this->configuration['enableWildcards'] && ($hasLeadingWildcard || $hasTrailingWildcard)) { $token = $hasLeadingWildcard ? substr($token, 1) : $token; $token = $hasTrailingWildcard ? substr($token, 0, -1) : $token;