我在neo4j中有一个带有多个路径的子图,通过以下方式生成:
match p=((n:Actor)-[*1..3]->(m:film)) where n.surname='Craig' and m.name='Minions' and ALL(x in nodes(p)[1..length(p)-1] where labels(x)[0]='Director') return p
现在,从这个子图我想要一个元组列表,其中每个元组是子图中的一对连接节点:
node0, node1
node1, node3
node0, node2
node2, node26
我试过了:
match p=((n:Actor)-[*1..3]->(m:film))
where n.surname='Craig' and m.name='Minions' and ALL(x in nodes(p)[1..length(p)-1] where labels(x)[0]='Director')
with nodes(p) as np
match p2=((nn)-[]-()) where nn IN np
return p2
但这只返回了p中每个节点的最近邻居。在子图中包含到节点而不是。
这似乎有用
MATCH p=((n:Actor)-[*1..3]->(m:Film))
WHERE n.surname='Craig' AND m.name='minions' AND ALL(x in nodes(p)[1..length(p)-1] WHERE labels(x)[0]='Director')
MATCH p2=(n2)-[r]-(m2)
WHERE n2 IN nodes(p) AND m2 IN nodes(p)
RETURN
n2,r,m2
然而非常慢,任何加速建议?
答案 0 :(得分:1)
不是从路径中获取所有数据并将其与图形重新匹配,而是可以处理内存中对的路径。如果从每个路径获取节点并一次处理两个节点,则可以按顺序对它们进行收集。
...
// for each path grab the nodes
// and an index for them less the last one
//
with nodes(p) as node_list, range(0, size(nodes(p)) - 2, 1) as idx
//
// put the tuples in ordered pairs
//
unwind idx as i
with node_list[i] as a , node_list[i+1] as b
with
case
when id(a) < id(b) then [id(a), id(b)]
else [id(b), id(a)]
end as tuple
return tuple, count(*)
order by count(*) desc