Skip to content

Commit fd07070

Browse files
fix: plugging in the self.namespace into the Cypher commands causes bugs, it needs ' ' (#132)
1 parent 84fd0f5 commit fd07070

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

nano_graphrag/_storage/gdb_neo4j.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
def make_path_idable(path):
15-
return path.replace(".", "_").replace("/", "__").replace("-", "_")
15+
return path.replace(".", "_").replace("/", "__").replace("-", "_").replace(":", "_")
1616

1717

1818
@dataclass
@@ -69,7 +69,7 @@ async def index_start_callback(self):
6969
async def has_node(self, node_id: str) -> bool:
7070
async with self.async_driver.session() as session:
7171
result = await session.run(
72-
f"MATCH (n:{self.namespace}) WHERE n.id = $node_id RETURN COUNT(n) > 0 AS exists",
72+
f"MATCH (n:`{self.namespace}`) WHERE n.id = $node_id RETURN COUNT(n) > 0 AS exists",
7373
node_id=node_id,
7474
)
7575
record = await result.single()
@@ -78,7 +78,7 @@ async def has_node(self, node_id: str) -> bool:
7878
async def has_edge(self, source_node_id: str, target_node_id: str) -> bool:
7979
async with self.async_driver.session() as session:
8080
result = await session.run(
81-
f"MATCH (s:{self.namespace})-[r]->(t:{self.namespace}) "
81+
f"MATCH (s:`{self.namespace}`)-[r]->(t:`{self.namespace}`) "
8282
"WHERE s.id = $source_id AND t.id = $target_id "
8383
"RETURN COUNT(r) > 0 AS exists",
8484
source_id=source_node_id,
@@ -90,8 +90,8 @@ async def has_edge(self, source_node_id: str, target_node_id: str) -> bool:
9090
async def node_degree(self, node_id: str) -> int:
9191
async with self.async_driver.session() as session:
9292
result = await session.run(
93-
f"MATCH (n:{self.namespace}) WHERE n.id = $node_id "
94-
f"RETURN COUNT {{(n)-[]-(:{self.namespace})}} AS degree",
93+
f"MATCH (n:`{self.namespace}`) WHERE n.id = $node_id "
94+
f"RETURN COUNT {{(n)-[]-(:`{self.namespace}`)}} AS degree",
9595
node_id=node_id,
9696
)
9797
record = await result.single()
@@ -100,9 +100,9 @@ async def node_degree(self, node_id: str) -> int:
100100
async def edge_degree(self, src_id: str, tgt_id: str) -> int:
101101
async with self.async_driver.session() as session:
102102
result = await session.run(
103-
f"MATCH (s:{self.namespace}), (t:{self.namespace}) "
103+
f"MATCH (s:`{self.namespace}`), (t:`{self.namespace}`) "
104104
"WHERE s.id = $src_id AND t.id = $tgt_id "
105-
f"RETURN COUNT {{(s)-[]-(:{self.namespace})}} + COUNT {{(t)-[]-(:{self.namespace})}} AS degree",
105+
f"RETURN COUNT {{(s)-[]-(:`{self.namespace}`)}} + COUNT {{(t)-[]-(:`{self.namespace}`)}} AS degree",
106106
src_id=src_id,
107107
tgt_id=tgt_id,
108108
)
@@ -112,7 +112,7 @@ async def edge_degree(self, src_id: str, tgt_id: str) -> int:
112112
async def get_node(self, node_id: str) -> Union[dict, None]:
113113
async with self.async_driver.session() as session:
114114
result = await session.run(
115-
f"MATCH (n:{self.namespace}) WHERE n.id = $node_id RETURN properties(n) AS node_data",
115+
f"MATCH (n:`{self.namespace}`) WHERE n.id = $node_id RETURN properties(n) AS node_data",
116116
node_id=node_id,
117117
)
118118
record = await result.single()
@@ -137,7 +137,7 @@ async def get_edge(
137137
) -> Union[dict, None]:
138138
async with self.async_driver.session() as session:
139139
result = await session.run(
140-
f"MATCH (s:{self.namespace})-[r]->(t:{self.namespace}) "
140+
f"MATCH (s:`{self.namespace}`)-[r]->(t:`{self.namespace}`) "
141141
"WHERE s.id = $source_id AND t.id = $target_id "
142142
"RETURN properties(r) AS edge_data",
143143
source_id=source_node_id,
@@ -151,7 +151,7 @@ async def get_node_edges(
151151
) -> Union[list[tuple[str, str]], None]:
152152
async with self.async_driver.session() as session:
153153
result = await session.run(
154-
f"MATCH (s:{self.namespace})-[r]->(t:{self.namespace}) WHERE s.id = $source_id "
154+
f"MATCH (s:`{self.namespace}`)-[r]->(t:`{self.namespace}`) WHERE s.id = $source_id "
155155
"RETURN s.id AS source, t.id AS target",
156156
source_id=source_node_id,
157157
)
@@ -164,7 +164,7 @@ async def upsert_node(self, node_id: str, node_data: dict[str, str]):
164164
node_type = node_data.get("entity_type", "UNKNOWN").strip('"')
165165
async with self.async_driver.session() as session:
166166
await session.run(
167-
f"MERGE (n:{self.namespace}:{node_type} {{id: $node_id}}) "
167+
f"MERGE (n:`{self.namespace}`:`{node_type}` {{id: $node_id}}) "
168168
"SET n += $node_data",
169169
node_id=node_id,
170170
node_data=node_data,
@@ -176,7 +176,7 @@ async def upsert_edge(
176176
edge_data.setdefault("weight", 0.0)
177177
async with self.async_driver.session() as session:
178178
await session.run(
179-
f"MATCH (s:{self.namespace}), (t:{self.namespace}) "
179+
f"MATCH (s:`{self.namespace}`), (t:`{self.namespace}`) "
180180
"WHERE s.id = $source_id AND t.id = $target_id "
181181
"MERGE (s)-[r:RELATED]->(t) " # Added relationship type 'RELATED'
182182
"SET r += $edge_data",
@@ -257,8 +257,8 @@ async def community_schema(self) -> dict[str, SingleCommunitySchema]:
257257
# Fetch community data
258258
result = await session.run(
259259
f"""
260-
MATCH (n:{self.namespace})
261-
WITH n, n.communityIds AS communityIds, [(n)-[]-(m:{self.namespace}) | m.id] AS connected_nodes
260+
MATCH (n:`{self.namespace}`)
261+
WITH n, n.communityIds AS communityIds, [(n)-[]-(m:`{self.namespace}`) | m.id] AS connected_nodes
262262
RETURN n.id AS node_id, n.source_id AS source_id,
263263
communityIds AS cluster_key,
264264
connected_nodes
@@ -317,10 +317,10 @@ async def _debug_delete_all_node_edges(self):
317317
async with self.async_driver.session() as session:
318318
try:
319319
# Delete all relationships in the namespace
320-
await session.run(f"MATCH (n:{self.namespace})-[r]-() DELETE r")
320+
await session.run(f"MATCH (n:`{self.namespace}`)-[r]-() DELETE r")
321321

322322
# Delete all nodes in the namespace
323-
await session.run(f"MATCH (n:{self.namespace}) DELETE n")
323+
await session.run(f"MATCH (n:`{self.namespace}`) DELETE n")
324324

325325
logger.info(
326326
f"All nodes and edges in namespace '{self.namespace}' have been deleted."

0 commit comments

Comments
 (0)