dragonflydb / dragonfly

A modern replacement for Redis and Memcached
https://www.dragonflydb.io/
Other
25.87k stars 947 forks source link

Search: Escape sequences #3145

Open dranikpg opened 5 months ago

dranikpg commented 5 months ago

Test escaping and direct qutoes to be on the safe side

CodeToFreedom commented 3 months ago

@dranikpg Unfortunately just escaping the sequences works only in dragonflydb but not in redis. As this syntax difference causes major query problems/crashes in our efforts to migrate to this otherwhise really awesome dragonfly library, I added the solution here as well hoping it gets seen by one of the contributors: https://github.com/dragonflydb/dragonfly/issues/3258#issuecomment-2292055368

The problem:

This works in redis (but fails in dragonflydb): "FT.SEARCH" ":Language:index" "(@code:{ZH\\-CN})" "LIMIT" "0" "1"

This works in dragonflydb (but fails in redis): "FT.SEARCH" ":Language:index" "(@code:{'ZH-CN'})" "LIMIT" "0" "1"

Here is the fix:

To fix that and make dragonflydb behave like the redis tokenizer please escape the following regex matches with two backslashes. DEFAULT_ESCAPED_CHARS = r"[,.<>{}\[\]\\\"\':;!@#$%^&*()\-+=~\/ ]"

The matching regex groups need to be ESCAPED like that: f"\\{value}"

References: Here is a completely working token escaper from a redis client library: Source: https://github.com/redis/redis-om-python/blob/main/aredis_om/model/token_escaper.py

Many thanks for your efforts.

CodeToFreedom commented 2 months ago

@dranikpg I added more detailed test script and syntax examples: https://github.com/dragonflydb/dragonfly/issues/3258#issuecomment-2307025879