Skip to content

Commit

Permalink
milvus: fixed bug when using partition key and dynamic fields together (
Browse files Browse the repository at this point in the history
#25028)

**Description:**

This PR fixes a bug where if `enable_dynamic_field` and
`partition_key_field` are enabled at the same time, a pymilvus error
occurs.

Milvus requires the partition key field to be a full schema defined
field, and not a dynamic one, so it will throw the error "the specified
partition key field {field} not exist" when creating the collection.

When `enabled_dynamic_field` is set to `True`, all schema field creation
based on `metadatas` is skipped. This code now checks if
`partition_key_field` is set, and creates the field.

Integration test added.

**Twitter handle:** StuartMarshUK

---------

Co-authored-by: Stuart Marsh <[email protected]>
Co-authored-by: Erick Friis <[email protected]>
  • Loading branch information
3 people authored Aug 5, 2024
1 parent 6890daa commit 16bd069
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
9 changes: 8 additions & 1 deletion libs/partners/milvus/langchain_milvus/vectorstores/milvus.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,14 @@ def _create_collection(
# ...
# ```
if self.enable_dynamic_field:
pass
# If both dynamic fields and partition key field are enabled
if self._partition_key_field is not None:
# create the partition field
fields.append(
FieldSchema(
self._partition_key_field, DataType.VARCHAR, max_length=65_535
)
)
elif self._metadata_field is not None:
fields.append(FieldSchema(self._metadata_field, DataType.JSON))
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test Milvus functionality."""

from typing import Any, List, Optional

import pytest
Expand Down Expand Up @@ -274,6 +275,34 @@ def test_milvus_metadata_field() -> None:
}


def test_milvus_enable_dynamic_field_with_partition_key() -> None:
"""
Test end to end construction and enable dynamic field
with partition_key_field
"""
texts = ["foo", "bar", "baz"]
metadatas = [{"id": i, "namespace": f"name_{i}"} for i in range(len(texts))]

docsearch = _milvus_from_texts(
metadatas=metadatas, enable_dynamic_field=True, partition_key_field="namespace"
)

# filter on a single namespace
output = docsearch.similarity_search("foo", k=10, expr="namespace == 'name_2'")
assert len(output) == 1

# without namespace filter
output = docsearch.similarity_search("foo", k=10)
assert len(output) == 3

assert set(docsearch.fields) == {
docsearch._primary_field,
docsearch._text_field,
docsearch._vector_field,
docsearch._partition_key_field,
}


# if __name__ == "__main__":
# test_milvus()
# test_milvus_vector_search()
Expand Down

0 comments on commit 16bd069

Please sign in to comment.