Skip to content

fix: escape RediSearch-special characters in store delete queries#197

Open
jerry153fish wants to merge 1 commit into
redis-developer:mainfrom
jerry153fish:fix/store-delete-escape-key
Open

fix: escape RediSearch-special characters in store delete queries#197
jerry153fish wants to merge 1 commit into
redis-developer:mainfrom
jerry153fish:fix/store-delete-escape-key

Conversation

@jerry153fish

Copy link
Copy Markdown

Fixes #196

Problem

store.delete() / store.adelete() raise RedisSearchError: ... SEARCH_SYNTAX Syntax error ... for any key containing characters RediSearch treats as query syntax (-, ., #, @, …). put()/get() accept such keys, so filename-derived keys like my-doc.md#0 could be written but never deleted.

Cause / fix

The delete branch of _prepare_batch_PUT_queries (base.py) and _aprepare_batch_PUT_queries (aio.py) interpolated the raw key into the FT.SEARCH query:

query = f"(@prefix:{prefix} @key:{{{op.key}}})"

This PR escapes the key with _token_escaper, matching the already-correct query in _batch_put_ops:

query = f"(@prefix:{prefix} @key:{{{_token_escaper.escape(op.key)}}})"

Two one-line changes (async + sync stores).

Tests

tests/test_issue_196_store_delete_escaping.py — sync and async put → get → delete → get-is-None round-trips over five representative keys (my-doc.md#0, a-b, a.b, a#1, user@example.com). Both tests fail with the SEARCH_SYNTAX error before the fix and pass after; also verified manually against a redis:8 container.

…re delete queries

The delete branch of _prepare_batch_PUT_queries / _aprepare_batch_PUT_queries
interpolated the raw item key into the FT.SEARCH query, so delete()/adelete()
raised 'SEARCH_SYNTAX Syntax error' for keys containing '-', '.', '#', etc. —
put()/get() accept such keys, so filename-derived keys like 'my-doc.md#0'
could be written but never deleted.

Escape the key with _token_escaper, matching the already-escaped query in
_batch_put_ops.

Fixes redis-developer#196

@limjoobin limjoobin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Good catch, thanks!

@limjoobin limjoobin added the bug Something isn't working label Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

store: adelete() raises RediSearch SEARCH_SYNTAX error for keys containing '-', '.', '#' — delete query does not escape op.key

2 participants