在 neo4j 中没有得到孤立的节点
Not getting isolated node in neo4j
我从这个答案中得到了帮助它工作正常但不适用于孤立节点意味着没有关系的单个节点(子节点)。
在这种情况下我得到的甚至不是那个节点。请帮助我,我是 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;
谢谢
问题是这样的:UNWIND rels(path) as rel
UNWINDing 集合意味着获取该记录,并将其更改为集合中每个元素的记录,从而使记录成倍增加。当集合为空时(孤立节点将没有关系),乘以零...它会清除集合为空的记录。
您可以使用 CASE 语句将集合替换为 null 而不是空集合(再次收集时 null 将被清除)。当您放松时,这会保留记录。
UNWIND case when size(rels(path)) = 0 then [null] else rels(path) end as rel
我从这个答案中得到了帮助它工作正常但不适用于孤立节点意味着没有关系的单个节点(子节点)。 在这种情况下我得到的甚至不是那个节点。请帮助我,我是 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;
谢谢
问题是这样的:UNWIND rels(path) as rel
UNWINDing 集合意味着获取该记录,并将其更改为集合中每个元素的记录,从而使记录成倍增加。当集合为空时(孤立节点将没有关系),乘以零...它会清除集合为空的记录。
您可以使用 CASE 语句将集合替换为 null 而不是空集合(再次收集时 null 将被清除)。当您放松时,这会保留记录。
UNWIND case when size(rels(path)) = 0 then [null] else rels(path) end as rel