Skip to content

Commit b402648

Browse files
gerzsevladvildanov
authored andcommitted
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 d8b6490 commit b402648

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
@@ -2463,6 +2463,65 @@ def test_search_empty_fields(client):
24632463
_assert_search_result(client, res, ["property:1", "property:2"])
24642464

24652465

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

0 commit comments

Comments
 (0)