如何用纯Cypher从Neo4j可变长度路径生成边列表
Pure Cypher Query to Generate Edge List for visNetwork
If you need a direct, pure Cypher way to get the from/to edge list you want (no pre-generated node list required), this query will do exactly that:
MATCH path = (n:Crew)-[:KNOWS*]-(m) WHERE n.name = 'Neo' UNWIND relationships(path) AS rel RETURN startNode(rel).name AS from, endNode(rel).name AS to
How it works:
MATCH path = (n:Crew)-[:KNOWS*]-(m): Captures all paths of any length using theKNOWSrelationship starting from Neo.UNWIND relationships(path) AS rel: Breaks down each full path into individual relationships (so we get every direct edge along the paths).RETURN startNode(rel).name AS from, endNode(rel).name AS to: Extracts thenameproperty from the start and end nodes of each relationship, formatting them into your requiredfromandtocolumns.
Optional: Remove duplicate edges
If you want to avoid duplicate entries (e.g., if the same edge is traversed in multiple paths), add DISTINCT to the return clause:
MATCH path = (n:Crew)-[:KNOWS*]-(m) WHERE n.name = 'Neo' UNWIND relationships(path) AS rel RETURN DISTINCT startNode(rel).name AS from, endNode(rel).name AS to
This will output exactly the format you're looking for:
| from | to |
|---|---|
| Neo | Morpheus |
| Morpheus | Trinity |
| Morpheus | Cypher |
| Cypher | Agent Smith |
You can directly export this result as a CSV or load it into R's DataFrame for use with visNetwork—no extra steps needed.
内容的提问来源于stack exchange,提问作者dopple
相关产品推荐
相关产品推荐

