Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Refactor:improve dify retrieval logic (#15036)

Stephen Huda112e3db03d
diff --git a/api/apps/sdk/dify_retrieval.py b/api/apps/sdk/dify_retrieval.py
index b4520685ca..59ea268453 100644
--- a/api/apps/sdk/dify_retrieval.py
+++ b/api/apps/sdk/dify_retrieval.py
@@ -294,9 +294,15 @@ async def retrieval(tenant_id):
if ck["content_with_weight"]:
ranks["chunks"].insert(0, ck)
+ doc_ids = list(set([c["doc_id"] for c in ranks["chunks"]]))
+ docs = DocumentService.get_by_ids(doc_ids)
+ doc_map = {doc.id: doc for doc in docs}
+
records = []
for c in ranks["chunks"]:
- e, doc = DocumentService.get_by_id(c["doc_id"])
+ doc = doc_map.get(c["doc_id"])
+ if not doc:
+ continue
c.pop("vector", None)
meta = getattr(doc, 'meta_fields', {})
meta["doc_id"] = c["doc_id"]