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

_discard_invalid_meta #33

Merged
merged 1 commit into from
Oct 17, 2024
Merged
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
33 changes: 32 additions & 1 deletion src/milvus_haystack/document_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def write_documents(self, documents: List[Document], policy: DuplicatePolicy = D

from pymilvus import Collection, MilvusException

documents_cp = deepcopy(documents)
documents_cp = [MilvusDocumentStore._discard_invalid_meta(doc) for doc in deepcopy(documents)]
if len(documents_cp) > 0 and not isinstance(documents_cp[0], Document):
err_msg = "param 'documents' must contain a list of objects of type Document"
raise ValueError(err_msg)
Expand Down Expand Up @@ -905,3 +905,34 @@ def _convert_sparse_to_dict(self, sparse_embedding: SparseEmbedding) -> Dict:

def _convert_dict_to_sparse(self, sparse_dict: Dict) -> SparseEmbedding:
return SparseEmbedding(indices=list(sparse_dict.keys()), values=list(sparse_dict.values()))

@staticmethod
def _discard_invalid_meta(document: Document):
"""
Remove metadata fields with unsupported types from the document.
"""
from pymilvus import DataType
from pymilvus.orm.types import infer_dtype_bydata

if not isinstance(document, Document):
msg = f"Invalid document type: {type(document)}"
raise ValueError(msg)
if document.meta:
discarded_keys = []
new_meta = {}
for key, value in document.meta.items():
dtype = infer_dtype_bydata(value)
if dtype in (DataType.UNKNOWN, DataType.NONE):
discarded_keys.append(key)
else:
new_meta[key] = value

if discarded_keys:
msg = (
f"Document {document.id} has metadata fields with unsupported types: {discarded_keys}. "
f"Supported types refer to Pymilvus DataType. The values of these fields will be discarded."
)
logger.warning(msg)
document.meta = new_meta

return document
Loading