0

我从这个答案中得到了帮助,它工作正常,但不适用于孤立节点意味着没有关系的单个节点(子节点)。在这种情况下,我得到的甚至不是那个节点。请帮助我,我是 neo4j 的初学者,这将是很大的帮助

回答链接

OPTIONAL MATCH path = (x)-[*0..100]->()       
WHERE ID(x) = 65
UNWIND nodes(path) as node
UNWIND rels(path) as rel

WITH collect(distinct node) as nodes,collect(distinct rel) as rels
// WITH apoc.coll.flatten(collect(nodes(path))) as nodes, apoc.coll.flatten(collect(relationships(path))) 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;

谢谢

4

1 回答 1

1

问题是这样的:UNWIND rels(path) as rel

展开集合意味着获取该记录,并将其更改为集合中每个元素的记录,它将记录相乘。当集合为空时(孤立节点将没有关系),即乘以零......它会清除具有空集合的记录。

您可以使用 CASE 语句将集合替换为 null 而不是空集合(再次收集时 null 将被清除)。当您 UNWIND 时,它会保留记录。

UNWIND case when size(rels(path)) = 0 then [null] else rels(path) end  as rel
于 2017-11-22T08:07:38.933 回答