22
33from __future__ import annotations
44
5- from typing import Any , Dict , List , Optional , Sequence
5+ from typing import Any , Dict , List , Optional
66
77from llama_index .core .graph_stores .types import (
88 ChunkNode ,
99 EntityNode ,
10- KG_NODES_KEY ,
11- KG_RELATIONS_KEY ,
1210 LabelledNode ,
1311 PropertyGraphStore ,
1412 Relation ,
@@ -73,9 +71,9 @@ def get(
7371 )
7472 cypher = f"MATCH (n) WHERE { where_clauses } RETURN n, id(n) AS _id LIMIT 1000"
7573 result = self ._client .cypher (cypher , params = properties )
76- for row in result . rows :
77- node_data = row [ 0 ] if row else {}
78- node_id = str (row [ 1 ]) if len ( row ) > 1 else ""
74+ for row in result :
75+ node_data = row . get ( "n" , {})
76+ node_id = str (row . get ( "_id" , "" ))
7977 nodes .append (_node_result_to_labelled (node_id , node_data ))
8078
8179 return nodes
@@ -104,22 +102,20 @@ def get_triplets(
104102 where = f"WHERE { ' AND ' .join (conditions )} " if conditions else ""
105103 cypher = (
106104 f"MATCH (n)-{ rel_pattern } ->(m) { where } "
107- "RETURN n, type(r), m, id(n) AS _src_id, id(m) AS _dst_id "
105+ "RETURN n, type(r) AS rel_type , m, id(n) AS _src_id, id(m) AS _dst_id "
108106 "LIMIT 1000"
109107 )
110108 result = self ._client .cypher (cypher , params = params )
111109
112110 triplets : List [List [LabelledNode ]] = []
113- for row in result .rows :
114- src_data , rel_type , dst_data , src_id , dst_id = (
115- row [0 ], row [1 ], row [2 ], str (row [3 ]), str (row [4 ])
116- )
111+ for row in result :
112+ src_data = row .get ("n" , {})
113+ rel_type = row .get ("rel_type" , "RELATED" )
114+ dst_data = row .get ("m" , {})
115+ src_id = str (row .get ("_src_id" , "" ))
116+ dst_id = str (row .get ("_dst_id" , "" ))
117117 src = _node_result_to_labelled (src_id , src_data )
118- rel = Relation (
119- label = str (rel_type ),
120- source_id = src_id ,
121- target_id = dst_id ,
122- )
118+ rel = Relation (label = str (rel_type ), source_id = src_id , target_id = dst_id )
123119 dst = _node_result_to_labelled (dst_id , dst_data )
124120 triplets .append ([src , rel , dst ])
125121
@@ -137,10 +133,8 @@ def get_rel_map(
137133 return []
138134
139135 ids = [n .id for n in graph_nodes ]
140- rel_filter = ""
141- if ignore_rels :
142- # We can't dynamically exclude types in OpenCypher without WHERE
143- pass
136+ # ignore_rels: OpenCypher doesn't support dynamic type exclusion in patterns;
137+ # would require WHERE NOT type(r) IN $ignore_rels — added when needed.
144138
145139 cypher = (
146140 f"MATCH (n)-[r*1..{ depth } ]->(m) "
@@ -151,13 +145,16 @@ def get_rel_map(
151145 result = self ._client .cypher (cypher , params = {"ids" : ids })
152146
153147 triplets : List [List [LabelledNode ]] = []
154- for row in result .rows :
155- src_data , rels , dst_data , src_id , dst_id = (
156- row [0 ], row [1 ], row [2 ], str (row [3 ]), str (row [4 ])
157- )
148+ for row in result :
149+ src_data = row .get ("n" , {})
150+ dst_data = row .get ("m" , {})
151+ src_id = str (row .get ("_src_id" , "" ))
152+ dst_id = str (row .get ("_dst_id" , "" ))
153+ # Variable-length path r returns a list; take first rel type as label.
154+ rels = row .get ("r" , [])
155+ rel_label = str (rels [0 ]) if isinstance (rels , list ) and rels else "RELATED"
158156 src = _node_result_to_labelled (src_id , src_data )
159157 dst = _node_result_to_labelled (dst_id , dst_data )
160- rel_label = str (rels [0 ]) if isinstance (rels , list ) and rels else "RELATED"
161158 rel = Relation (label = rel_label , source_id = src_id , target_id = dst_id )
162159 triplets .append ([src , rel , dst ])
163160
@@ -228,13 +225,14 @@ def vector_query(
228225 nodes : List [LabelledNode ] = []
229226 scores : List [float ] = []
230227 for r in results :
228+ # VectorResult has .node (NodeResult with .id/.properties) and .distance
231229 node = ChunkNode (
232- id_ = str (r .node_id ),
233- text = r .properties .get ("text" , "" ),
234- properties = r .properties ,
230+ id_ = str (r .node . id ),
231+ text = r .node . properties .get ("text" , "" ),
232+ properties = r .node . properties ,
235233 )
236234 nodes .append (node )
237- scores .append (r .score )
235+ scores .append (r .distance )
238236
239237 return nodes , scores
240238
@@ -264,12 +262,8 @@ def structured_query(
264262 param_map : Optional [Dict [str , Any ]] = None ,
265263 ) -> Any :
266264 """Execute a raw Cypher query."""
267- result = self ._client .cypher (query , params = param_map or {})
268- rows = []
269- columns = list (result .columns )
270- for row in result .rows :
271- rows .append (dict (zip (columns , row )))
272- return rows
265+ # cypher() returns List[Dict[str, Any]] — column name → value.
266+ return self ._client .cypher (query , params = param_map or {})
273267
274268
275269# ── Helpers ───────────────────────────────────────────────────────────────
0 commit comments