mirror of
https://github.com/langgenius/dify.git
synced 2026-01-08 07:14:14 +00:00
feat: Implement partial update for document metadata, allowing merging of new values with existing ones. (#28390)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -158,6 +158,7 @@ class MetadataDetail(BaseModel):
|
||||
class DocumentMetadataOperation(BaseModel):
|
||||
document_id: str
|
||||
metadata_list: list[MetadataDetail]
|
||||
partial_update: bool = False
|
||||
|
||||
|
||||
class MetadataOperationData(BaseModel):
|
||||
|
||||
@@ -206,7 +206,10 @@ class MetadataService:
|
||||
document = DocumentService.get_document(dataset.id, operation.document_id)
|
||||
if document is None:
|
||||
raise ValueError("Document not found.")
|
||||
doc_metadata = {}
|
||||
if operation.partial_update:
|
||||
doc_metadata = copy.deepcopy(document.doc_metadata) if document.doc_metadata else {}
|
||||
else:
|
||||
doc_metadata = {}
|
||||
for metadata_value in operation.metadata_list:
|
||||
doc_metadata[metadata_value.name] = metadata_value.value
|
||||
if dataset.built_in_field_enabled:
|
||||
@@ -219,9 +222,21 @@ class MetadataService:
|
||||
db.session.add(document)
|
||||
db.session.commit()
|
||||
# deal metadata binding
|
||||
db.session.query(DatasetMetadataBinding).filter_by(document_id=operation.document_id).delete()
|
||||
if not operation.partial_update:
|
||||
db.session.query(DatasetMetadataBinding).filter_by(document_id=operation.document_id).delete()
|
||||
|
||||
current_user, current_tenant_id = current_account_with_tenant()
|
||||
for metadata_value in operation.metadata_list:
|
||||
# check if binding already exists
|
||||
if operation.partial_update:
|
||||
existing_binding = (
|
||||
db.session.query(DatasetMetadataBinding)
|
||||
.filter_by(document_id=operation.document_id, metadata_id=metadata_value.id)
|
||||
.first()
|
||||
)
|
||||
if existing_binding:
|
||||
continue
|
||||
|
||||
dataset_metadata_binding = DatasetMetadataBinding(
|
||||
tenant_id=current_tenant_id,
|
||||
dataset_id=dataset.id,
|
||||
|
||||
Reference in New Issue
Block a user