Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MilvusVectorStore: Add support for nested MetadataFilters and FilterOperator.IS_EMPTY #16329

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ def parse_standard_filters(standard_filters: MetadataFilters = None):
return filters, ""

for filter in standard_filters.filters:
if isinstance(filter, MetadataFilters):
filters.append(f"({parse_standard_filters(filter)[1]})")
continue
filter_value = parse_filter_value(filter.value)
if filter_value is None:
if filter_value is None and filter.operator != FilterOperator.IS_EMPTY:
continue

if filter.operator == FilterOperator.NIN:
Expand All @@ -122,6 +125,9 @@ def parse_standard_filters(standard_filters: MetadataFilters = None):
filters.append(
f"{filter.key!s} like {parse_filter_value(filter.value, True)}"
)
elif filter.operator == FilterOperator.IS_EMPTY:
# in Milvus, array_length(field_name) returns 0 if the field does not exist or is not an array
filters.append(f"array_length({filter.key!s}) == 0")
elif filter.operator in [
FilterOperator.EQ,
FilterOperator.GT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exclude = ["**/BUILD"]
license = "MIT"
name = "llama-index-vector-stores-milvus"
readme = "README.md"
version = "0.2.6"
version = "0.2.7"

[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ def test_to_milvus_filter_with_various_operators():
expr = _to_milvus_filter(filters)
assert expr == "array_contains_all(a, [1, 2])"

filters = MetadataFilters(
filters=[MetadataFilter(key="a", value=None, operator=FilterOperator.IS_EMPTY)]
)
expr = _to_milvus_filter(filters)
assert expr == "array_length(a) == 0"


def test_to_milvus_filter_with_string_value():
filters = MetadataFilters(filters=[MetadataFilter(key="a", value="hello")])
Expand Down Expand Up @@ -181,6 +187,24 @@ def test_to_milvus_filter_with_multiple_filters():
assert expr == "(a < 1 or a > 10)"


def test_milvus_filter_with_nested_filters():
filters = MetadataFilters(
filters=[
MetadataFilter(key="a", value=1, operator=FilterOperator.EQ),
MetadataFilters(
filters=[
MetadataFilter(key="b", value=2, operator=FilterOperator.EQ),
MetadataFilter(key="c", value=3, operator=FilterOperator.EQ),
],
condition=FilterCondition.OR,
),
],
condition=FilterCondition.AND,
)
expr = _to_milvus_filter(filters)
assert expr == "(a == 1 and (b == 2 or c == 3))"


def test_milvus_vector_store():
vector_store = MilvusVectorStore(
dim=1536,
Expand Down
Loading