You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用纯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 the KNOWS relationship 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 the name property from the start and end nodes of each relationship, formatting them into your required from and to columns.

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:

fromto
NeoMorpheus
MorpheusTrinity
MorpheusCypher
CypherAgent 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:16:56