RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Feat: deduplicate metadata lists during updates (#12125)
Yongteng Lei49dbfdbfb040
diff --git a/api/db/services/document_service.py b/api/db/services/document_service.pyindex cc992585c8..b096e7c8b4 100644--- a/api/db/services/document_service.py+++ b/api/db/services/document_service.py@@ -33,6 +33,7 @@ from api.db.db_models import DB, Document, Knowledgebase, Task, Tenant, UserTenafrom api.db.db_utils import bulk_insert_into_dbfrom api.db.services.common_service import CommonServicefrom api.db.services.knowledgebase_service import KnowledgebaseService+from common.metadata_utils import dedupe_listfrom common.misc_utils import get_uuidfrom common.time_utils import current_timestamp, get_format_timefrom common.constants import LLMType, ParserType, StatusEnum, TaskStatus, SVR_CONSUMER_GROUP_NAME@@ -799,7 +800,10 @@ class DocumentService(CommonService):match_provided = "match" in updif isinstance(meta[key], list):if not match_provided:- meta[key] = new_value+ if isinstance(new_value, list):+ meta[key] = dedupe_list(new_value)+ else:+ meta[key] = new_valuechanged = Trueelse:match_value = upd.get("match")@@ -812,7 +816,7 @@ class DocumentService(CommonService):else:new_list.append(item)if replaced:- meta[key] = new_list+ meta[key] = dedupe_list(new_list)changed = Trueelse:if not match_provided:diff --git a/common/metadata_utils.py b/common/metadata_utils.pyindex 4d4cceb4a2..d7bb3c818f 100644--- a/common/metadata_utils.py+++ b/common/metadata_utils.py@@ -151,6 +151,18 @@ async def apply_meta_data_filter(return doc_ids+def dedupe_list(values: list) -> list:+ seen = set()+ deduped = []+ for item in values:+ key = str(item)+ if key in seen:+ continue+ seen.add(key)+ deduped.append(item)+ return deduped++def update_metadata_to(metadata, meta):if not meta:return metadata@@ -162,11 +174,13 @@ def update_metadata_to(metadata, meta):return metadataif not isinstance(meta, dict):return metadata+for k, v in meta.items():if isinstance(v, list):v = [vv for vv in v if isinstance(vv, str)]if not v:continue+ v = dedupe_list(v)if not isinstance(v, list) and not isinstance(v, str):continueif k not in metadata:@@ -177,6 +191,7 @@ def update_metadata_to(metadata, meta):metadata[k].extend(v)else:metadata[k].append(v)+ metadata[k] = dedupe_list(metadata[k])else:metadata[k] = v@@ -208,4 +223,4 @@ def metadata_schema(metadata: list|None) -> Dict[str, Any]:}json_schema["additionalProperties"] = False- return json_schema\ No newline at end of file+ return json_schema