fix: unEscapeSpecialChars handles all RediSearch special characters#6636
fix: unEscapeSpecialChars handles all RediSearch special characters#6636zzhzhangzhihao wants to merge 1 commit into
Conversation
- Previously only escaped/unescaped '-' character
- Now handles all RediSearch reserved characters: , . < > { } [ ] " ' : ; ! @ # $ % ^ & * ( ) - + = ~
- Fixes JSON.parse failure when metadata contains special characters (FlowiseAI#6598)
There was a problem hiding this comment.
Code Review
This pull request updates the unEscapeSpecialChars function in the Redis vector store utilities to handle a comprehensive list of RediSearch special characters. The reviewer noted that the dot (.) and pipe (|) characters are missing from this list, which could lead to failures during JSON.parse if they remain escaped, and provided a suggestion to include them.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| // RediSearch special characters that need to be unescaped | ||
| // https://redis.io/docs/latest/develop/interact/search-and-query/advanced/concepts/special-characters/ | ||
| const specialChars = [ | ||
| '\\-', '-', | ||
| '\\,', ',', | ||
| '\\<', '<', | ||
| '\\>', '>', | ||
| '\\{', '{', | ||
| '\\}', '}', | ||
| '\\[', '[', | ||
| '\\]', ']', | ||
| '\"', '"', | ||
| "\'", "'", | ||
| '\\:', ':', | ||
| '\\;', ';', | ||
| '\\!', '!', | ||
| '\\@', '@', | ||
| '\\#', '#', | ||
| '\\$', '$', | ||
| '\\%', '%', | ||
| '\\^', '^', | ||
| '\\&', '&', | ||
| '\\*', '*', | ||
| '\\(', '(', | ||
| '\\)', ')', | ||
| '\\+', '+', | ||
| '\\=', '=', | ||
| '\\~', '~', | ||
| ] | ||
|
|
||
| for (let i = 0; i < specialChars.length; i += 2) { | ||
| str = str.replaceAll(specialChars[i], specialChars[i + 1]) | ||
| } | ||
| return str |
There was a problem hiding this comment.
Missing Special Characters
The implementation is missing . (dot) and | (pipe), which are official RediSearch reserved characters (as documented in the Redis Special Characters documentation). If metadata contains these characters, they will remain escaped, causing JSON.parse to fail.
Recommended Solution
Add unescaping for . and | to the existing chain of replacements. To maintain code readability and understandability, we should prefer a series of simple, chained operations over a single complex regular expression, as it reduces the potential for future errors.
| // RediSearch special characters that need to be unescaped | |
| // https://redis.io/docs/latest/develop/interact/search-and-query/advanced/concepts/special-characters/ | |
| const specialChars = [ | |
| '\\-', '-', | |
| '\\,', ',', | |
| '\\<', '<', | |
| '\\>', '>', | |
| '\\{', '{', | |
| '\\}', '}', | |
| '\\[', '[', | |
| '\\]', ']', | |
| '\"', '"', | |
| "\'", "'", | |
| '\\:', ':', | |
| '\\;', ';', | |
| '\\!', '!', | |
| '\\@', '@', | |
| '\\#', '#', | |
| '\\$', '$', | |
| '\\%', '%', | |
| '\\^', '^', | |
| '\\&', '&', | |
| '\\*', '*', | |
| '\\(', '(', | |
| '\\)', ')', | |
| '\\+', '+', | |
| '\\=', '=', | |
| '\\~', '~', | |
| ] | |
| for (let i = 0; i < specialChars.length; i += 2) { | |
| str = str.replaceAll(specialChars[i], specialChars[i + 1]) | |
| } | |
| return str | |
| str.replaceAll('\\.', '.').replaceAll('\\|', '|') |
References
- Prioritize code readability and understandability over conciseness. A series of simple, chained operations can be preferable to a single, more complex one (e.g., a complex regex with a replacer function) if it improves understandability and reduces the potential for future errors.
What
Fixes
unEscapeSpecialCharsin Redis vector store utility to handle all RediSearch special characters, not just-.Why
Previously,
unEscapeSpecialCharsonly unescaped the-character. When document metadata contained other RediSearch reserved characters (:",.<>{}[];!@#$%^&*()-+=~), the JSON parsing of metadata would fail with errors like:This affected any document whose metadata contained these special characters.
Fix
Updated
unEscapeSpecialCharsinpackages/components/nodes/vectorstores/Redis/utils.tsto iterate over all RediSearch special characters and unescape them, matching the full set of reserved characters documented at https://redis.io/docs/latest/develop/interact/search-and-query/advanced/concepts/special-characters/Related