Skip to content

Commit 5add3ae

Browse files
authored
[BUG] Trim colon from binds to prevent Named parameter "text_ma_xxx" does not have a bound value error on matchAgainst search (#141)
1 parent 3cc7b93 commit 5add3ae

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

Classes/Filter/SearchFilter.php

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static function getOpenApiParameters(ApiFilter $apiFilter): array
3636
*/
3737
public function filterProperty(
3838
string $property,
39-
$values,
39+
$values,
4040
QueryInterface $query,
4141
ApiFilter $apiFilter
4242
): ?ConstraintInterface {
@@ -82,6 +82,7 @@ protected function matchAgainstFindIds(
8282
$binds = [];
8383
$rootAlias = 'o';
8484
$queryExpansion = (bool)$apiFilter->getArgument('withQueryExpansion');
85+
$booleanQuery = (bool)$apiFilter->getArgument('withBooleanQuery');
8586

8687
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
8788
->getQueryBuilderForTable($tableName);
@@ -100,16 +101,43 @@ protected function matchAgainstFindIds(
100101

101102
foreach ($values as $i => $value) {
102103
$key = ':text_ma_' . ((int)$i);
103-
$conditions[] = sprintf(
104-
'MATCH(%s) AGAINST (%s IN NATURAL LANGUAGE MODE %s)',
105-
$queryBuilder->quoteIdentifier(
106-
$tableAlias . '.' . GeneralUtility::makeInstance(DataMapper::class)
107-
->convertPropertyNameToColumnName($propertyName, $apiFilter->getFilterClass())
108-
),
109-
$key,
110-
$queryExpansion ? ' WITH QUERY EXPANSION ' : ''
111-
);
112-
$binds[$key] = $value;
104+
105+
if ($booleanQuery) {
106+
// Split the value into individual words and create OR conditions
107+
$words = explode(' ', trim($value));
108+
$booleanQuery = '';
109+
foreach ($words as $word) {
110+
if (!empty(trim($word))) {
111+
$booleanQuery .= ' +'. trim($word) . '* ';
112+
}
113+
}
114+
$booleanQuery = trim($booleanQuery);
115+
116+
// use IN BOOLEAN MODE to search for partials of words
117+
$conditions[] = sprintf(
118+
'MATCH(%s) AGAINST (%s IN BOOLEAN MODE)',
119+
$queryBuilder->quoteIdentifier(
120+
$tableAlias . '.' . GeneralUtility::makeInstance(DataMapper::class)
121+
->convertPropertyNameToColumnName($propertyName, $apiFilter->getFilterClass())
122+
),
123+
$key
124+
);
125+
126+
$binds[ltrim($key, ':')] = $booleanQuery;
127+
} else {
128+
// Original natural language mode query
129+
$conditions[] = sprintf(
130+
'MATCH(%s) AGAINST (%s IN NATURAL LANGUAGE MODE %s)',
131+
$queryBuilder->quoteIdentifier(
132+
$tableAlias . '.' . GeneralUtility::makeInstance(DataMapper::class)
133+
->convertPropertyNameToColumnName($propertyName, $apiFilter->getFilterClass())
134+
),
135+
$key,
136+
$queryExpansion ? ' WITH QUERY EXPANSION ' : ''
137+
);
138+
139+
$binds[ltrim($key, ':')] = $value;
140+
}
113141
}
114142

115143
return $queryBuilder

0 commit comments

Comments
 (0)