Skip to content

Escape punctuation in Redis TAG filter values - #7445

Open
r0h1tb wants to merge 1 commit into
spring-projects:mainfrom
r0h1tb:fix/redis-tag-filter-punctuation
Open

r0h1tb wants to merge 1 commit into
spring-projects:mainfrom
r0h1tb:fix/redis-tag-filter-punctuation

Conversation

@r0h1tb

@r0h1tb r0h1tb commented Nov 22, 2026

Copy link
Copy Markdown

Fixes #2945.

RedisFilterExpressionConverter.escapeTagValue escapes only \ $ | { } ( ) [ ] - ' *. RediSearch reads the rest of ASCII punctuation in a TAG query as syntax: ., ,, / and similar characters split the value, and :, @, ; and % make the query invalid. delete(filter) builds the same clause.

On Redis Stack, with one document per source value:

Filter Expected Before this change
source == 'report.pdf' report.pdf the report pdf document
delete(source == 'report.pdf') removes report.pdf removes report pdf, keeps report.pdf
delete(source != 'report.pdf') keeps only report.pdf keeps only report pdf, removes report.pdf
source == 'v1.2-rc' v1.2-rc nothing
source == '[email protected]' [email protected] JedisDataException: Syntax error
source == 'https://example.com/a?b=1' that document JedisDataException: Syntax error

File names are probably the most common case, since document readers put them in metadata.

The fix escapes every ASCII character other than a letter, a digit or _. Non-ASCII characters are left as they are, because escaping them stops the value from matching. #3202 tried escaping . and \ while parsing the filter text, which affects every store. This change stays in the Redis converter and also covers the builder API.

Three existing converter tests asserted output that Redis rejects, for example @description:{test@value\{with\}special\|chars} (syntax error at offset 8). I updated them.

Tests:

  • RedisFilterExpressionConverterTests: the three updated expectations plus tagValueEscapesPunctuation and tagValueKeepsLettersDigitsAndUnderscores. 4 fail without the fix.
  • RedisVectorStoreTagFilterIT (new, Testcontainers only, no embedding provider needed): search and delete for 22 values with punctuation, plus IN and a != delete. 40 of 46 fail without the fix.
  • Module: 35 unit tests, RedisVectorStoreTagFilterIT 46/46, RedisVectorStoreUpsertIT 19/19 and RedisVectorStoreDistanceMetricIT 3/3 pass. Checkstyle and spring-javaformat are clean.

RediSearch reads unescaped ASCII punctuation in a TAG query as syntax.
Characters such as '.', ',' and '/' split the value, so
`source == 'report.pdf'` matched a document tagged "report pdf" and not
the one tagged "report.pdf". Others such as ':', '@' and ';' made the
query invalid, so filters on e-mail addresses, URLs or Windows paths
failed. delete(filter) uses the same clause, so it removed the wrong
documents or failed.

Escape every ASCII character other than a letter, a digit or '_'.
Non-ASCII characters are left as they are, since escaping them stops
the value from matching.

Fixes spring-projects#2945

Signed-off-by: Rohit Behera <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redis Vector Library Filtering Problem

2 participants