Skip to content

Commit

Permalink
refactor: enhance filter conditions in Qdrant tests by implementing N…
Browse files Browse the repository at this point in the history
…OT and AND with NOT logic in MetadataFilters
  • Loading branch information
iamarunbrahma committed Dec 13, 2024
1 parent 29d64a6 commit 6e4986d
Showing 1 changed file with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
PointsList,
PointStruct,
Filter,
FieldCondition,
MatchText,
)


Expand Down Expand Up @@ -162,32 +160,42 @@ def test_filter_conditions():
assert filter_or.should[1].key == "category"
assert filter_or.should[1].match.value == "electronics"

# Test NOT condition by creating a Filter directly
not_filter = Filter(
must_not=[
FieldCondition(
key="content",
match=MatchText(text="unwanted text"),
)
]
# Test NOT condition
not_filter = MetadataFilters(
filters=[
MetadataFilter(key="category", value="books", operator=FilterOperator.EQ),
],
condition="not",
)
assert not_filter.must_not is not None
assert len(not_filter.must_not) == 1
assert not_filter.must_not[0].key == "content"
assert not_filter.must_not[0].match.text == "unwanted text"

# Test complex nested condition (AND with NOT)
complex_filter = MetadataFilters(
filter_not = vector_store._build_subfilter(not_filter)
assert filter_not.must_not is not None
assert len(filter_not.must_not) == 1
assert filter_not.must_not[0].key == "category"
assert filter_not.must_not[0].match.value == "books"

# Test AND with NOT condition
and_not_filter = MetadataFilters(
filters=[
MetadataFilter(key="category", value="books", operator=FilterOperator.EQ),
MetadataFilter(key="price", value=10, operator=FilterOperator.GT),
MetadataFilters(
filters=[
MetadataFilter(key="price", value=50, operator=FilterOperator.EQ),
],
condition="not",
),
],
condition=FilterCondition.AND,
)
filter_complex = vector_store._build_subfilter(complex_filter)
assert filter_complex.must is not None
assert len(filter_complex.must) == 2
assert filter_complex.must[0].key == "category"
assert filter_complex.must[0].match.value == "books"
assert filter_complex.must[1].key == "price"
assert filter_complex.must[1].range.gt == 10
filter_and_not = vector_store._build_subfilter(and_not_filter)
assert filter_and_not.must is not None
assert (
len(filter_and_not.must) == 2
) # One for category and one for the nested filter
assert filter_and_not.must[0].key == "category"
assert filter_and_not.must[0].match.value == "books"
# The second must element is a Filter object with must_not condition
assert isinstance(filter_and_not.must[1], Filter)
assert filter_and_not.must[1].must_not is not None
assert len(filter_and_not.must[1].must_not) == 1
assert filter_and_not.must[1].must_not[0].key == "price"
assert filter_and_not.must[1].must_not[0].match.value == 50

0 comments on commit 6e4986d

Please sign in to comment.