Skip to content

Commit 1505f5d

Browse files
authored
Fix null numeric filter conditions (#164)
Ensures that the Num class correctly handles 0 as a valid value. This PR modifies the `__str__` method to check if the value is `None` explicitly instead of using a condition that treats 0 as false. Context: ``` Q: Is there a way possible to set the filter to a num 0. Below is the client query, filter_conditions: FilterExpression = ((Tag("doc_base_id") == doc_base.id) & (Tag("file_id") == file_id) & (Num("chunk_number") == chunk_num)) When I instantiate the query with a non-zero chunk_number, the generated query looks as expected and the result only contains the requested chunk: [doc_base.id](http://doc_base.id/) 'AIGuildDemo' file_id 'e9ffbac9ff6f67cc' chunk_num 1 str(filter_conditions) '((@doc_base_id:{AIGuildDemo} @file_id:{e9ffbac9ff6f67cc}) @chunk_number:[1 1])' However, when I set chunk_num to 0, chunk_number gets left out of the query entirely, and the result of the query is all chunks with that doc_base_id and file_id: [doc_base.id](http://doc_base.id/) 'AIGuildDemo' file_id 'e9ffbac9ff6f67cc' chunk_num 0 str(filter_conditions) '(@doc_base_id:{AIGuildDemo} @file_id:{e9ffbac9ff6f67cc})' Based on a look through redisvl documentation, it seems that this issue might be related to this feature where passing “None” will drop the filter Is there way way to use redisvl to filter entries where chunk_number is 0? ```
1 parent 1c7d2a0 commit 1505f5d

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

redisvl/query/filter.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,8 @@ def __le__(self, other: int) -> "FilterExpression":
375375

376376
def __str__(self) -> str:
377377
"""Return the Redis Query string for the Numeric filter"""
378-
if not self._value:
378+
if self._value is None:
379379
return "*"
380-
381380
if self._operator == FilterOperator.EQ or self._operator == FilterOperator.NE:
382381
return self.OPERATOR_MAP[self._operator] % (
383382
self._field,

tests/integration/test_query.py

+14
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,17 @@ def test_sort_vector_query(index, sorted_vector_query):
400400
def test_sort_range_query(index, sorted_range_query):
401401
t = Text("job") % ""
402402
search(sorted_range_query, index, t, 7, sort=True)
403+
404+
def test_query_with_chunk_number_zero():
405+
doc_base_id = "8675309"
406+
file_id = "e9ffbac9ff6f67cc"
407+
chunk_num = 0
408+
409+
filter_conditions = (
410+
(Tag("doc_base_id") == doc_base_id) &
411+
(Tag("file_id") == file_id) &
412+
(Num("chunk_number") == chunk_num)
413+
)
414+
415+
expected_query_str = '((@doc_base_id:{8675309} @file_id:{e9ffbac9ff6f67cc}) @chunk_number:[0 0])'
416+
assert str(filter_conditions) == expected_query_str, "Query with chunk_number zero is incorrect"

tests/unit/test_filter.py

+4
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,7 @@ def test_filters_combination():
285285
tf3 = Text("text_field") == None
286286
tf4 = Geo("geo_field") == GeoRadius(1.0, 2.0, 3, "km")
287287
assert str(tf1 & tf2 & tf3 & tf4) == str(tf1 & tf4)
288+
289+
def test_num_filter_zero():
290+
num_filter = Num("chunk_number") == 0
291+
assert str(num_filter) == "@chunk_number:[0 0]", "Num filter should handle zero correctly"

0 commit comments

Comments
 (0)