@@ -460,6 +460,8 @@ def _sync_get_knowledge_graph():
460460 result = KnowledgeGraph ()
461461 seen_nodes = set ()
462462 seen_edges = set ()
463+ # Map Neo4j internal id -> entity_id for edges (unified semantics: source/target = entity_id)
464+ internal_id_to_entity_id : dict [int , str ] = {}
463465
464466 with Neo4jSyncConnectionManager .get_session (database = self ._DATABASE ) as session :
465467 if node_label == "*" :
@@ -476,18 +478,22 @@ def _sync_get_knowledge_graph():
476478
477479 for record in node_results :
478480 node = record ["n" ]
479- node_id = node .id
480- if node_id not in seen_nodes :
481+ internal_id = node .id
482+ if internal_id not in seen_nodes :
483+ entity_id = node .get ("entity_id" )
484+ node_id = str (entity_id ) if entity_id is not None else f"{ internal_id } "
485+ internal_id_to_entity_id [internal_id ] = node_id
486+ entity_type = node .get ("entity_type" )
481487 result .nodes .append (
482488 KnowledgeGraphNode (
483- id = f" { node_id } " ,
484- labels = [node . get ( "entity_id" ) ],
489+ id = node_id ,
490+ labels = [entity_type ] if entity_type else [ node_id ],
485491 properties = dict (node ),
486492 )
487493 )
488- seen_nodes .add (node_id )
494+ seen_nodes .add (internal_id )
489495
490- # Get edges between these nodes
496+ # Get edges between these nodes; source/target must be entity_id
491497 edge_query = """
492498 MATCH (a)-[r]-(b)
493499 WHERE id(a) IN $node_ids AND id(b) IN $node_ids
@@ -499,15 +505,18 @@ def _sync_get_knowledge_graph():
499505 rel = record ["r" ]
500506 edge_id = rel .id
501507 if edge_id not in seen_edges :
502- result .edges .append (
503- KnowledgeGraphEdge (
504- id = f"{ edge_id } " ,
505- type = rel .type ,
506- source = f"{ record ['a' ].id } " ,
507- target = f"{ record ['b' ].id } " ,
508- properties = dict (rel ),
508+ src_entity = internal_id_to_entity_id .get (record ["a" ].id )
509+ tgt_entity = internal_id_to_entity_id .get (record ["b" ].id )
510+ if src_entity is not None and tgt_entity is not None :
511+ result .edges .append (
512+ KnowledgeGraphEdge (
513+ id = f"{ edge_id } " ,
514+ type = rel .type ,
515+ source = src_entity ,
516+ target = tgt_entity ,
517+ properties = dict (rel ),
518+ )
509519 )
510- )
511520 seen_edges .add (edge_id )
512521 else :
513522 # BFS from specific node
@@ -527,30 +536,37 @@ def _sync_get_knowledge_graph():
527536 for record in results :
528537 if record ["nodes" ]:
529538 for node in record ["nodes" ]:
530- node_id = node .id
531- if node_id not in seen_nodes :
539+ internal_id = node .id
540+ if internal_id not in seen_nodes :
541+ entity_id = node .get ("entity_id" )
542+ node_id = str (entity_id ) if entity_id is not None else f"{ internal_id } "
543+ internal_id_to_entity_id [internal_id ] = node_id
544+ entity_type = node .get ("entity_type" )
532545 result .nodes .append (
533546 KnowledgeGraphNode (
534- id = f" { node_id } " ,
535- labels = [node . get ( "entity_id" ) ],
547+ id = node_id ,
548+ labels = [entity_type ] if entity_type else [ node_id ],
536549 properties = dict (node ),
537550 )
538551 )
539- seen_nodes .add (node_id )
552+ seen_nodes .add (internal_id )
540553
541554 if record ["rels" ]:
542555 for rel in record ["rels" ]:
543556 edge_id = rel .id
544557 if edge_id not in seen_edges :
545- result .edges .append (
546- KnowledgeGraphEdge (
547- id = f"{ edge_id } " ,
548- type = rel .type ,
549- source = f"{ rel .start_node .id } " ,
550- target = f"{ rel .end_node .id } " ,
551- properties = dict (rel ),
558+ src_entity = internal_id_to_entity_id .get (rel .start_node .id )
559+ tgt_entity = internal_id_to_entity_id .get (rel .end_node .id )
560+ if src_entity is not None and tgt_entity is not None :
561+ result .edges .append (
562+ KnowledgeGraphEdge (
563+ id = f"{ edge_id } " ,
564+ type = rel .type ,
565+ source = src_entity ,
566+ target = tgt_entity ,
567+ properties = dict (rel ),
568+ )
552569 )
553- )
554570 seen_edges .add (edge_id )
555571
556572 logger .info (f"Retrieved subgraph with { len (result .nodes )} nodes and { len (result .edges )} edges" )
0 commit comments