As Graph edges should be treated undirectional, fix incorrect has_edge method in MongoDBGraph

This commit is contained in:
Ken Chen 2025-06-28 20:48:30 +08:00
parent 73cc86662a
commit 5116d61eaa
2 changed files with 16 additions and 4 deletions

View file

@ -297,6 +297,8 @@ class BaseKVStorage(StorageNameSpace, ABC):
@dataclass @dataclass
class BaseGraphStorage(StorageNameSpace, ABC): class BaseGraphStorage(StorageNameSpace, ABC):
"""All operations related to edges in graph should be undirected."""
embedding_func: EmbeddingFunc embedding_func: EmbeddingFunc
@abstractmethod @abstractmethod

View file

@ -419,11 +419,21 @@ class MongoGraphStorage(BaseGraphStorage):
async def has_edge(self, source_node_id: str, target_node_id: str) -> bool: async def has_edge(self, source_node_id: str, target_node_id: str) -> bool:
""" """
Check if there's a direct single-hop edge from source_node_id to target_node_id. Check if there's a direct single-hop edge between source_node_id and target_node_id.
""" """
# Direct check if the target_node appears among the edges array.
doc = await self.edge_collection.find_one( doc = await self.edge_collection.find_one(
{"source_node_id": source_node_id, "target_node_id": target_node_id}, {
"$or": [
{
"source_node_id": source_node_id,
"target_node_id": target_node_id,
},
{
"source_node_id": target_node_id,
"target_node_id": source_node_id,
},
]
},
{"_id": 1}, {"_id": 1},
) )
return doc is not None return doc is not None
@ -680,7 +690,7 @@ class MongoGraphStorage(BaseGraphStorage):
async def delete_node(self, node_id: str) -> None: async def delete_node(self, node_id: str) -> None:
""" """
1) Remove node's doc entirely. 1) Remove node's doc entirely.
2) Remove inbound edges from any doc that references node_id. 2) Remove inbound & outbound edges from any doc that references node_id.
""" """
# Remove all edges # Remove all edges
await self.edge_collection.delete_many( await self.edge_collection.delete_many(