【问题标题】:Altering Neo4j Cypher Query to Include nodes not connected to graph?更改 Neo4j Cypher 查询以包括未连接到图形的节点?
【发布时间】:2017-05-21 01:14:22
【问题描述】:

我正在使用以下代码从 Neo4j 返回所有节点和边(3 级深度),并将响应格式化为 GraphJSON。然后我使用一些 D3 代码在我的前端可视化图表。

这适用于至少有一个关系或边的节点。但是,我也有一些节点没有连接到任何其他节点。我还想在查询响应中包含这些节点。

任何建议我可以如何修改下面的代码,以便它也可以拾取未通过关系连接到图表的浮动节点?

OPTIONAL MATCH path = (x:entity)-[*..3 {current:true}]->(:entity)

UNWIND nodes(path) as node
UNWIND rels(path) as rel

WITH collect(distinct node) as nodes,
     collect(distinct rel) as rels
WITH apoc.coll.toSet( [n in nodes WHERE n is not null | { id: id(n),label: labels(n),type:"",metadata: properties(n)  } ]) as nodes, 

apoc.coll.toSet([r in rels WHERE r is not null  | { id: id(r),source: id(startNode(r)),relation: type(r),target: id(endNode(r)), directed: "true"  } ]) as rels

RETURN { graph: { type:"",label: "",directed: "true",nodes: nodes,edges: rels, metadata:{ countNodes: size(nodes),countEdges: size(rels) } } } as graph;

非常感谢,

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    您可以使用+ 运算符将集合添加到一起,因此在您收集了nodesrels 之后,匹配到您想要包含的浮动节点,收集它们,将集合添加到您的nodes收集,并完成您的查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-17
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多