Skip to content

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

Refactor: add total/returned entities/relations 2 (#19114)

Wang Qi61492c0362a7
diff --git a/api/apps/restful_apis/chunk_api.py b/api/apps/restful_apis/chunk_api.py
index 6472c9e4fd..dce0301f47 100644
--- a/api/apps/restful_apis/chunk_api.py
+++ b/api/apps/restful_apis/chunk_api.py
@@ -608,6 +608,10 @@ async def get_document_structure_graph(tenant_id, dataset_id, document_id):
Response shape::
{
+ "total_entities": 100,
+ "total_relations": 200,
+ "returned_entities": 80,
+ "returned_relations": 150,
"templates": [
{
"template_id": "<id> | 'legacy:<compile_kwd>'",
@@ -702,6 +706,61 @@ async def get_document_structure_graph(tenant_id, dataset_id, document_id):
index_name = search.index_name(dataset_tenant_id)
keywords = (request.args.get("keywords") or "").strip()
+ total_entities = 0
+ total_relations = 0
+
+ def _response(templates: list[dict]) -> dict:
+ return {
+ "templates": templates,
+ "total_entities": total_entities,
+ "total_relations": total_relations,
+ "returned_entities": sum(len(template["entities"]) for template in templates),
+ "returned_relations": sum(len(template["relations"]) for template in templates),
+ }
+
+ try:
+ _, total_entities = await sgc.graph_search(index_name, dataset_id, ["id"], {"doc_id": [document_id], "knowledge_graph_kwd": ["entity"]}, OrderByExpr(), 1)
+ _, total_relations = await sgc.graph_search(index_name, dataset_id, ["id"], {"doc_id": [document_id], "knowledge_graph_kwd": ["relation"]}, OrderByExpr(), 1)
+ except Exception as e:
+ return server_error_response(e)
+
+ # RAPTOR summary graph is stored as a standalone blob rather than raw
+ # knowledge_graph_kwd entity/relation rows, so include its arrays explicitly.
+ raptor_entities: list[dict] = []
+ raptor_relations: list[dict] = []
+ try:
+ res_raptor = await thread_pool_exec(
+ settings.docStoreConn.search,
+ ["content_with_weight", "compile_kwd"],
+ [],
+ {"doc_id": [document_id], "compile_kwd": ["raptor_graph"]},
+ [],
+ OrderByExpr(),
+ 0,
+ 16,
+ index_name,
+ [dataset_id],
+ )
+ raptor_rows = settings.docStoreConn.get_fields(res_raptor, ["content_with_weight", "compile_kwd"]) or {}
+ except Exception:
+ logging.exception("structure graph: RAPTOR blob load failed for doc=%s", document_id)
+ raptor_rows = {}
+ for row in raptor_rows.values():
+ try:
+ graph = json.loads(row.get("content_with_weight") or "{}")
+ except Exception:
+ continue
+ if not isinstance(graph, dict):
+ continue
+ r_entities = graph.get("entities") or []
+ r_relations = graph.get("relations") or []
+ if isinstance(r_entities, list):
+ raptor_entities.extend(r_entities)
+ if isinstance(r_relations, list):
+ raptor_relations.extend(r_relations)
+ total_entities += len(raptor_entities)
+ total_relations += len(raptor_relations)
+
def _row_template_id(row: dict) -> str | None:
raw = row.get("compilation_template_ids")
if isinstance(raw, list):
@@ -769,7 +828,7 @@ async def get_document_structure_graph(tenant_id, dataset_id, document_id):
embd_mdl = TenantLLMService.model_instance(model_config)
except Exception:
logging.exception("structure graph: embedding bind failed for doc=%s", document_id)
- return get_result(data={"templates": []})
+ return get_result(data=_response([]))
try:
bucket_meta, kw_entities, kw_relations = await sgc.keyword_subgraph(
index_name,
@@ -783,11 +842,11 @@ async def get_document_structure_graph(tenant_id, dataset_id, document_id):
except Exception as e:
return server_error_response(e)
if not bucket_meta or (not kw_entities and not kw_relations):
- return get_result(data={"templates": []})
+ return get_result(data=_response([]))
bucket = dict(bucket_meta)
bucket["entities"] = kw_entities
bucket["relations"] = kw_relations
- return get_result(data={"templates": [bucket]})
+ return get_result(data=_response([bucket]))
# ── normal mode: per-template subgraph sampling from the raw rows ──
# Metadata-only scan of the per-doc graph blob rows (one per
@@ -834,40 +893,12 @@ async def get_document_structure_graph(tenant_id, dataset_id, document_id):
continue
grouped[bid] = {**meta, "entities": entities, "relations": relations}
- # RAPTOR summary graph: a standalone blob (no ``knowledge_graph_kwd``, and
- # no raw entity/relation rows), so read its content directly and don't
- # sample it.
- try:
- res_raptor = await thread_pool_exec(
- settings.docStoreConn.search,
- ["content_with_weight", "compile_kwd"],
- [],
- {"doc_id": [document_id], "compile_kwd": ["raptor_graph"]},
- [],
- OrderByExpr(),
- 0,
- 16,
- index_name,
- [dataset_id],
- )
- raptor_rows = settings.docStoreConn.get_fields(res_raptor, ["content_with_weight", "compile_kwd"]) or {}
- except Exception:
- logging.exception("structure graph: RAPTOR blob load failed for doc=%s", document_id)
- raptor_rows = {}
- for row in raptor_rows.values():
- try:
- graph = json.loads(row.get("content_with_weight") or "{}")
- except Exception:
- continue
- if not isinstance(graph, dict):
- continue
- r_entities = graph.get("entities") or []
- r_relations = graph.get("relations") or []
- if not r_entities and not r_relations:
- continue
+ # RAPTOR was loaded above so its full counts are also available to keyword
+ # responses; append it only in normal mode, matching the existing behavior.
+ if raptor_entities or raptor_relations:
rb = grouped.setdefault("raptor", {"template_id": "raptor", "template_name": "RAPTOR Summary", "kind": "raptor", "entities": [], "relations": []})
- rb["entities"].extend(r_entities)
- rb["relations"].extend(r_relations)
+ rb["entities"].extend(raptor_entities)
+ rb["relations"].extend(raptor_relations)
# Order: configured templates first (in the user's chosen order),
# then any discovered / legacy / raptor buckets after.
@@ -924,7 +955,7 @@ async def get_document_structure_graph(tenant_id, dataset_id, document_id):
]
templates_out = [grouped[bid] for bid in ordered_ids if grouped[bid]["entities"] or grouped[bid]["relations"]]
- return get_result(data={"templates": templates_out})
+ return get_result(data=_response(templates_out))
@manager.route("/datasets/<dataset_id>/documents/<document_id>/structure/graph", methods=["DELETE"]) # noqa: F821