Skip to content

Commit 6c21279

Browse files
authored
Escape special characters in tag field filters (#78)
1 parent b614b3f commit 6c21279

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

src/Index.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,12 @@ protected function makeQueryBuilder(): QueryBuilder
313313
/**
314314
* @param string $fieldName
315315
* @param array $values
316+
* @param array|null $charactersToEscape
316317
* @return QueryBuilderInterface
317318
*/
318-
public function tagFilter(string $fieldName, array $values): QueryBuilderInterface
319+
public function tagFilter(string $fieldName, array $values, array $charactersToEscape = null): QueryBuilderInterface
319320
{
320-
return $this->makeQueryBuilder()->tagFilter($fieldName, $values);
321+
return $this->makeQueryBuilder()->tagFilter($fieldName, $values, $charactersToEscape);
321322
}
322323

323324
/**

src/Query/Builder.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,20 @@ public function verbatim(): BuilderInterface
128128
return $this;
129129
}
130130

131-
public function tagFilter(string $fieldName, array $values): BuilderInterface
131+
public function tagFilter(string $fieldName, array $values, array $charactersToEscape = null): BuilderInterface
132132
{
133-
$separatedValues = implode('|', $values);
133+
if ($charactersToEscape == null) {
134+
$charactersToEscape = [' ', '-'];
135+
}
136+
$escapedValues = [];
137+
foreach ($values as $value) {
138+
$escapedValue = $value;
139+
foreach ($charactersToEscape as $character) {
140+
$escapedValue = str_replace($character, "\\$character", $escapedValue);
141+
}
142+
$escapedValues[] = $escapedValue;
143+
}
144+
$separatedValues = implode('|', $escapedValues);
134145
$this->tagFilters[] = "@$fieldName:{{$separatedValues}}";
135146
return $this;
136147
}

src/Query/BuilderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function noStopWords(): BuilderInterface;
1818
public function withPayloads(): BuilderInterface;
1919
public function withScores(): BuilderInterface;
2020
public function verbatim(): BuilderInterface;
21-
public function tagFilter(string $fieldName, array $values): BuilderInterface;
21+
public function tagFilter(string $fieldName, array $values, array $charactersToEscape = null): BuilderInterface;
2222
public function numericFilter(string $fieldName, $min, $max = null): BuilderInterface;
2323
public function geoFilter(string $fieldName, float $longitude, float $latitude, float $radius, string $distanceUnit = 'km'): BuilderInterface;
2424
public function sortBy(string $fieldName, $order = 'ASC'): BuilderInterface;

tests/RediSearch/IndexTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,17 @@ public function testAddDocumentWithTagField()
523523
'name' => 'Bar',
524524
'color' => 'blue',
525525
]);
526+
$index->add([
527+
'name' => 'Baz',
528+
'color' => 'sky blue',
529+
]);
530+
$index->add([
531+
'name' => 'Qux',
532+
'color' => 'sugar-cookie',
533+
]);
526534

527535
$result = $index
528-
->tagFilter('color', ['blue'])
536+
->tagFilter('color', ['sugar-cookie'])
529537
->search();
530538

531539
$this->assertEquals(1, $result->getCount());

0 commit comments

Comments
 (0)