Refactor:improve dify retrieval logic (#15036)

### What problem does this PR solve?

improve dify retrieval logic for o(n) io to o(1)

### Type of change
- [x] Refactoring
This commit is contained in:
Stephen Hu
2026-05-21 14:07:15 +08:00
committed by Jin Hai
parent e7544562cc
commit da112e3db0

View File

@ -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"]