Skip to content

Commit 7fafc82

Browse files
committed
Changed default dialect to 2
1 parent 8e2f2d3 commit 7fafc82

File tree

3 files changed

+78
-27
lines changed

3 files changed

+78
-27
lines changed

redis/commands/search/aggregation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __init__(self, query: str = "*") -> None:
110110
self._with_schema = False
111111
self._verbatim = False
112112
self._cursor = []
113-
self._dialect = None
113+
self._dialect = 2
114114
self._add_scores = False
115115
self._scorer = "TFIDF"
116116

redis/commands/search/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __init__(self, query_string: str) -> None:
4040
self._highlight_fields: List = []
4141
self._language: Optional[str] = None
4242
self._expander: Optional[str] = None
43-
self._dialect: Optional[int] = None
43+
self._dialect: int = 2
4444

4545
def query_string(self) -> str:
4646
"""Return the query string of this query only."""

tests/test_search.py

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,7 +2122,7 @@ def test_profile_query_params(client):
21222122
client.hset("b", "v", "aaaabaaa")
21232123
client.hset("c", "v", "aaaaabaa")
21242124
query = "*=>[KNN 2 @v $vec]"
2125-
q = Query(query).return_field("__v_score").sort_by("__v_score", True).dialect(2)
2125+
q = Query(query).return_field("__v_score").sort_by("__v_score", True)
21262126
if is_resp2_connection(client):
21272127
res, det = client.ft().profile(q, query_params={"vec": "aaaaaaaa"})
21282128
assert det["Iterators profile"]["Counter"] == 2.0
@@ -2155,7 +2155,7 @@ def test_vector_field(client):
21552155
client.hset("c", "v", "aaaaabaa")
21562156

21572157
query = "*=>[KNN 2 @v $vec]"
2158-
q = Query(query).return_field("__v_score").sort_by("__v_score", True).dialect(2)
2158+
q = Query(query).return_field("__v_score").sort_by("__v_score", True)
21592159
res = client.ft().search(q, query_params={"vec": "aaaaaaaa"})
21602160

21612161
if is_resp2_connection(client):
@@ -2191,7 +2191,7 @@ def test_text_params(client):
21912191
client.hset("doc3", mapping={"name": "Carol"})
21922192

21932193
params_dict = {"name1": "Alice", "name2": "Bob"}
2194-
q = Query("@name:($name1 | $name2 )").dialect(2)
2194+
q = Query("@name:($name1 | $name2 )")
21952195
res = client.ft().search(q, query_params=params_dict)
21962196
if is_resp2_connection(client):
21972197
assert 2 == res.total
@@ -2214,7 +2214,7 @@ def test_numeric_params(client):
22142214
client.hset("doc3", mapping={"numval": 103})
22152215

22162216
params_dict = {"min": 101, "max": 102}
2217-
q = Query("@numval:[$min $max]").dialect(2)
2217+
q = Query("@numval:[$min $max]")
22182218
res = client.ft().search(q, query_params=params_dict)
22192219

22202220
if is_resp2_connection(client):
@@ -2236,7 +2236,7 @@ def test_geo_params(client):
22362236
client.hset("doc3", mapping={"g": "29.68746, 34.94882"})
22372237

22382238
params_dict = {"lat": "34.95126", "lon": "29.69465", "radius": 1000, "units": "km"}
2239-
q = Query("@g:[$lon $lat $radius $units]").dialect(2)
2239+
q = Query("@g:[$lon $lat $radius $units]")
22402240
res = client.ft().search(q, query_params=params_dict)
22412241
_assert_search_result(client, res, ["doc1", "doc2", "doc3"])
22422242

@@ -2355,19 +2355,19 @@ def test_dialect(client):
23552355
with pytest.raises(redis.ResponseError) as err:
23562356
client.ft().explain(Query("(*)").dialect(1))
23572357
assert "Syntax error" in str(err)
2358-
assert "WILDCARD" in client.ft().explain(Query("(*)").dialect(2))
2358+
assert "WILDCARD" in client.ft().explain(Query("(*)"))
23592359

23602360
with pytest.raises(redis.ResponseError) as err:
23612361
client.ft().explain(Query("$hello").dialect(1))
23622362
assert "Syntax error" in str(err)
2363-
q = Query("$hello").dialect(2)
2363+
q = Query("$hello")
23642364
expected = "UNION {\n hello\n +hello(expanded)\n}\n"
23652365
assert expected in client.ft().explain(q, query_params={"hello": "hello"})
23662366

23672367
expected = "NUMERIC {0.000000 <= @num <= 10.000000}\n"
23682368
assert expected in client.ft().explain(Query("@title:(@num:[0 10])").dialect(1))
23692369
with pytest.raises(redis.ResponseError) as err:
2370-
client.ft().explain(Query("@title:(@num:[0 10])").dialect(2))
2370+
client.ft().explain(Query("@title:(@num:[0 10])"))
23712371
assert "Syntax error" in str(err)
23722372

23732373

@@ -2438,9 +2438,9 @@ def test_withsuffixtrie(client: redis.Redis):
24382438
@pytest.mark.redismod
24392439
def test_query_timeout(r: redis.Redis):
24402440
q1 = Query("foo").timeout(5000)
2441-
assert q1.get_args() == ["foo", "TIMEOUT", 5000, "LIMIT", 0, 10]
2441+
assert q1.get_args() == ["foo", "TIMEOUT", 5000, "DIALECT", 2, "LIMIT", 0, 10]
24422442
q1 = Query("foo").timeout(0)
2443-
assert q1.get_args() == ["foo", "TIMEOUT", 0, "LIMIT", 0, 10]
2443+
assert q1.get_args() == ["foo", "TIMEOUT", 0, "DIALECT", 2, "LIMIT", 0, 10]
24442444
q2 = Query("foo").timeout("not_a_number")
24452445
with pytest.raises(redis.ResponseError):
24462446
r.ft().search(q2)
@@ -2508,27 +2508,27 @@ def test_search_missing_fields(client):
25082508

25092509
with pytest.raises(redis.exceptions.ResponseError) as e:
25102510
client.ft().search(
2511-
Query("ismissing(@title)").dialect(2).return_field("id").no_content()
2511+
Query("ismissing(@title)").return_field("id").no_content()
25122512
)
25132513
assert "to be defined with 'INDEXMISSING'" in e.value.args[0]
25142514

25152515
res = client.ft().search(
2516-
Query("ismissing(@features)").dialect(2).return_field("id").no_content()
2516+
Query("ismissing(@features)").return_field("id").no_content()
25172517
)
25182518
_assert_search_result(client, res, ["property:2"])
25192519

25202520
res = client.ft().search(
2521-
Query("-ismissing(@features)").dialect(2).return_field("id").no_content()
2521+
Query("-ismissing(@features)").return_field("id").no_content()
25222522
)
25232523
_assert_search_result(client, res, ["property:1", "property:3"])
25242524

25252525
res = client.ft().search(
2526-
Query("ismissing(@description)").dialect(2).return_field("id").no_content()
2526+
Query("ismissing(@description)").return_field("id").no_content()
25272527
)
25282528
_assert_search_result(client, res, ["property:3"])
25292529

25302530
res = client.ft().search(
2531-
Query("-ismissing(@description)").dialect(2).return_field("id").no_content()
2531+
Query("-ismissing(@description)").return_field("id").no_content()
25322532
)
25332533
_assert_search_result(client, res, ["property:1", "property:2"])
25342534

@@ -2579,29 +2579,29 @@ def test_search_empty_fields(client):
25792579

25802580
with pytest.raises(redis.exceptions.ResponseError) as e:
25812581
client.ft().search(
2582-
Query("@title:''").dialect(2).return_field("id").no_content()
2582+
Query("@title:''").return_field("id").no_content()
25832583
)
25842584
assert "Use `INDEXEMPTY` in field creation" in e.value.args[0]
25852585

25862586
res = client.ft().search(
2587-
Query("@features:{$empty}").dialect(2).return_field("id").no_content(),
2587+
Query("@features:{$empty}").return_field("id").no_content(),
25882588
query_params={"empty": ""},
25892589
)
25902590
_assert_search_result(client, res, ["property:2"])
25912591

25922592
res = client.ft().search(
2593-
Query("-@features:{$empty}").dialect(2).return_field("id").no_content(),
2593+
Query("-@features:{$empty}").return_field("id").no_content(),
25942594
query_params={"empty": ""},
25952595
)
25962596
_assert_search_result(client, res, ["property:1", "property:3"])
25972597

25982598
res = client.ft().search(
2599-
Query("@description:''").dialect(2).return_field("id").no_content()
2599+
Query("@description:''").return_field("id").no_content()
26002600
)
26012601
_assert_search_result(client, res, ["property:3"])
26022602

26032603
res = client.ft().search(
2604-
Query("-@description:''").dialect(2).return_field("id").no_content()
2604+
Query("-@description:''").return_field("id").no_content()
26052605
)
26062606
_assert_search_result(client, res, ["property:1", "property:2"])
26072607

@@ -2643,29 +2643,80 @@ def test_special_characters_in_fields(client):
26432643

26442644
# no need to escape - when using params
26452645
res = client.ft().search(
2646-
Query("@uuid:{$uuid}").dialect(2),
2646+
Query("@uuid:{$uuid}"),
26472647
query_params={"uuid": "123e4567-e89b-12d3-a456-426614174000"},
26482648
)
26492649
_assert_search_result(client, res, ["resource:1"])
26502650

26512651
# with double quotes exact match no need to escape the - even without params
26522652
res = client.ft().search(
2653-
Query('@uuid:{"123e4567-e89b-12d3-a456-426614174000"}').dialect(2)
2653+
Query('@uuid:{"123e4567-e89b-12d3-a456-426614174000"}')
26542654
)
26552655
_assert_search_result(client, res, ["resource:1"])
26562656

2657-
res = client.ft().search(Query('@tags:{"new-year\'s-resolutions"}').dialect(2))
2657+
res = client.ft().search(Query('@tags:{"new-year\'s-resolutions"}'))
26582658
_assert_search_result(client, res, ["resource:2"])
26592659

26602660
# possible to search numeric fields by single value
2661-
res = client.ft().search(Query("@rating:[4]").dialect(2))
2661+
res = client.ft().search(Query("@rating:[4]"))
26622662
_assert_search_result(client, res, ["resource:2"])
26632663

26642664
# some chars still need escaping
2665-
res = client.ft().search(Query(r"@tags:{\$btc}").dialect(2))
2665+
res = client.ft().search(Query(r"@tags:{\$btc}"))
26662666
_assert_search_result(client, res, ["resource:1"])
26672667

26682668

2669+
@pytest.mark.redismod
2670+
@skip_ifmodversion_lt("2.4.3", "search")
2671+
def test_vector_search_with_default_dialect(client):
2672+
client.ft().create_index(
2673+
(
2674+
VectorField(
2675+
"v", "HNSW", {"TYPE": "FLOAT32", "DIM": 2, "DISTANCE_METRIC": "L2"}
2676+
),
2677+
)
2678+
)
2679+
2680+
client.hset("a", "v", "aaaaaaaa")
2681+
client.hset("b", "v", "aaaabaaa")
2682+
client.hset("c", "v", "aaaaabaa")
2683+
2684+
query = "*=>[KNN 2 @v $vec]"
2685+
q = Query(query)
2686+
2687+
assert "DIALECT" in q.get_args()
2688+
assert 2 in q.get_args()
2689+
2690+
res = client.ft().search(q, query_params={"vec": "aaaaaaaa"})
2691+
assert res.total == 2
2692+
2693+
@pytest.mark.redismod
2694+
@skip_ifmodversion_lt("2.4.3", "search")
2695+
def test_search_query_with_different_dialects(client):
2696+
client.ft().create_index(
2697+
(
2698+
TextField("name"),
2699+
TextField("lastname")
2700+
),
2701+
definition=IndexDefinition(prefix=["test:"])
2702+
)
2703+
2704+
client.hset("test:1", "name", "James")
2705+
client.hset("test:1", "lastname", "Brown")
2706+
2707+
# Query with default DIALECT 2
2708+
query = "@name: James Brown"
2709+
q = Query(query)
2710+
res = client.ft().search(q)
2711+
assert res.total == 1
2712+
2713+
# Query with explicit DIALECT 1
2714+
query = "@name: James Brown"
2715+
q = Query(query).dialect(1)
2716+
res = client.ft().search(q)
2717+
assert res.total == 0
2718+
2719+
26692720
def _assert_search_result(client, result, expected_doc_ids):
26702721
"""
26712722
Make sure the result of a geo search is as expected, taking into account the RESP

0 commit comments

Comments
 (0)