Skip to content

Commit c3db173

Browse files
htemelski-redisvladvildanov
authored andcommitted
Added epsilon property to the vsim command (#3723)
* added epsilon property to the vsim command * fixed linter errors * added async client test * modified test to use async
1 parent 4f126ae commit c3db173

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

redis/commands/vectorset/commands.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def vsim(
129129
filter_ef: Optional[str] = None,
130130
truth: Optional[bool] = False,
131131
no_thread: Optional[bool] = False,
132+
epsilon: Optional[Number] = None,
132133
) -> Union[
133134
Awaitable[Optional[List[Union[List[EncodableT], Dict[EncodableT, Number]]]]],
134135
Optional[List[Union[List[EncodableT], Dict[EncodableT, Number]]]],
@@ -152,6 +153,9 @@ def vsim(
152153
``no_thread`` when enabled forces the command to execute the search
153154
on the data structure in the main thread.
154155
156+
``epsilon`` floating point between 0 and 1, if specified will return
157+
only elements with distance no further than the specified one.
158+
155159
For more information see https://redis.io/commands/vsim
156160
"""
157161

@@ -176,6 +180,9 @@ def vsim(
176180
if count:
177181
pieces.extend(["COUNT", count])
178182

183+
if epsilon:
184+
pieces.extend(["EPSILON", epsilon])
185+
179186
if ef:
180187
pieces.extend(["EF", ef])
181188

tests/test_asyncio/test_vsets.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,21 @@ async def test_vsim_truth_no_thread_enabled(d_client):
423423
assert isinstance(sim_no_thread, dict)
424424

425425

426+
@skip_if_server_version_lt("8.2.0")
427+
async def test_vsim_epsilon(d_client):
428+
await d_client.vset().vadd("myset", [2, 1, 1], "a")
429+
await d_client.vset().vadd("myset", [2, 0, 1], "b")
430+
await d_client.vset().vadd("myset", [2, 0, 0], "c")
431+
await d_client.vset().vadd("myset", [2, 0, -1], "d")
432+
await d_client.vset().vadd("myset", [2, -1, -1], "e")
433+
434+
res1 = await d_client.vset().vsim("myset", [2, 1, 1])
435+
assert 5 == len(res1)
436+
437+
res2 = await d_client.vset().vsim("myset", [2, 1, 1], epsilon=0.5)
438+
assert 3 == len(res2)
439+
440+
426441
@skip_if_server_version_lt("7.9.0")
427442
async def test_vdim(d_client):
428443
float_array = [1, 4.32, 0.11, 0.5, 0.9, 0.1, 0.2]

tests/test_vsets.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,21 @@ def test_vsim_truth_no_thread_enabled(d_client):
425425
assert isinstance(sim_no_thread, dict)
426426

427427

428+
@skip_if_server_version_lt("8.2.0")
429+
def test_vsim_epsilon(d_client):
430+
d_client.vset().vadd("myset", [2, 1, 1], "a")
431+
d_client.vset().vadd("myset", [2, 0, 1], "b")
432+
d_client.vset().vadd("myset", [2, 0, 0], "c")
433+
d_client.vset().vadd("myset", [2, 0, -1], "d")
434+
d_client.vset().vadd("myset", [2, -1, -1], "e")
435+
436+
res1 = d_client.vset().vsim("myset", [2, 1, 1])
437+
assert 5 == len(res1)
438+
439+
res2 = d_client.vset().vsim("myset", [2, 1, 1], epsilon=0.5)
440+
assert 3 == len(res2)
441+
442+
428443
@skip_if_server_version_lt("7.9.0")
429444
def test_vdim(d_client):
430445
float_array = [1, 4.32, 0.11, 0.5, 0.9, 0.1, 0.2]

0 commit comments

Comments
 (0)