Skip to content

Commit 335adc2

Browse files
committed
Test special characters escaping in search (#3276)
With RediSearch dialect 5 less special characters need escaping when searching, given that TAG fields are used. Add a test to show some examples.
1 parent 4f9801c commit 335adc2

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

tests/test_search.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2465,6 +2465,65 @@ def test_search_empty_fields(client):
24652465
_assert_search_result(client, res, ["property:1", "property:2"])
24662466

24672467

2468+
@pytest.mark.redismod
2469+
def test_special_characters_in_fields(client):
2470+
definition = IndexDefinition(prefix=["resource:"], index_type=IndexType.HASH)
2471+
2472+
fields = [
2473+
TagField("uuid"),
2474+
TagField("tags", separator="|"),
2475+
TextField("description"),
2476+
NumericField("rating"),
2477+
]
2478+
2479+
client.ft().create_index(fields, definition=definition)
2480+
2481+
client.hset(
2482+
"resource:1",
2483+
mapping={
2484+
"uuid": "123e4567-e89b-12d3-a456-426614174000",
2485+
"tags": "finance|crypto|$btc|blockchain",
2486+
"description": "Analysis of blockchain technologies & Bitcoin's potential.",
2487+
"rating": 5,
2488+
},
2489+
)
2490+
2491+
client.hset(
2492+
"resource:2",
2493+
mapping={
2494+
"uuid": "987e6543-e21c-12d3-a456-426614174999",
2495+
"tags": "health|well-being|fitness|new-year's-resolutions",
2496+
"description": "Health trends for the new year, including fitness regimes.",
2497+
"rating": 4,
2498+
},
2499+
)
2500+
2501+
# no need to escape - when using params
2502+
res = client.ft().search(
2503+
Query("@uuid:{$uuid}").dialect(2),
2504+
query_params={"uuid": "123e4567-e89b-12d3-a456-426614174000"},
2505+
)
2506+
_assert_search_result(client, res, ["resource:1"])
2507+
2508+
# with dialect 5 no need to escape the - even without params
2509+
res = client.ft().search(
2510+
Query("@uuid:{123e4567-e89b-12d3-a456-426614174000}").dialect(5)
2511+
)
2512+
_assert_search_result(client, res, ["resource:1"])
2513+
2514+
# also no need to escape ' with dialect 5
2515+
res = client.ft().search(Query("@tags:{new-year's-resolutions}").dialect(5))
2516+
_assert_search_result(client, res, ["resource:2"])
2517+
2518+
# possible to search numeric fields by single value
2519+
res = client.ft().search(Query("@rating:[4]").dialect(2))
2520+
_assert_search_result(client, res, ["resource:2"])
2521+
2522+
# some chars still need escaping
2523+
res = client.ft().search(Query(r"@tags:{\$btc}").dialect(5))
2524+
_assert_search_result(client, res, ["resource:1"])
2525+
2526+
24682527
def _assert_search_result(client, result, expected_doc_ids):
24692528
"""
24702529
Make sure the result of a geo search is as expected, taking into account the RESP

0 commit comments

Comments
 (0)