如何返回交错的节点和关系?使用Matrix电影数据库查询
MATCH p=(a1:Actor {name:"Keanu Reeves"})-[r *0..5]-(a2:Actor {name: "Carrie-Anne Moss"})
return [n in nodes(p)|coalesce(n.title,n.name)], [rel in relationships(p)|type(rel)]
返回两列,一列包含节点,另一列包含关系
Keanu Reeves, The Matrix, Laurence Fishburne, The Matrix Reloaded, Carrie-Anne Moss | ACTS_IN, ACTS_IN, ACTS_IN, ACTS_IN
...
但我想要
Keanu Reeves, ACTS_IN, The Matrix, ACTS_IN, Laurence Fishburne, ACTS_IN, The Matrix Reloaded, ACTS_IN, Carrie-Anne Moss
...
答案 0 :(得分:1)
这曾经更容易,但是当他们在Cypher中制作Paths not Collections时,他们打破了2.0-RC1中的“简单方法”。
match p= shortestPath((kevin)-[:ACTED_IN*]-(charlize))
where kevin.name="Kevin Bacon"
and charlize.name="Charlize Theron"
with nodes(p) as ns, rels(p) as rs, range(0,length(nodes(p))+length(rels(p))-1) as idx
return [i in idx | case i % 2 = 0 when true then coalesce((ns[i/2]).name, (ns[i/2]).title) else type(rs[i/2]) end];
旧方法是:
match p= shortestPath((kevin)-[:ACTED_IN*]-(charlize))
where kevin.name="Kevin Bacon"
and charlize.name="Charlize Theron"
return [x in p | coalesce(x.name,x.title, type(x))]
这一变化带来的好处是Paths现在更加安全,尽管我很同意它们同意它们。这种查询的真实用例很少见。
答案 1 :(得分:1)
这是另一种解决方案:
MATCH (kevin:Person {name="Kevin Bacon"}), (charlize:Person {name:"Charlize Theron"})
MATCH p= shortestPath( (kevin)-[:ACTED_IN*]-(charlize) )
WITH nodes(p)+rels(p) AS c, length(p) AS l
RETURN reduce(r=[], x IN range(0,l) | r + (c[x]).name + type(c[l+x+1]))
将路径重新聚合到集合中,然后使用路径长度作为偏移量来访问下半部分。
Reduce用于“展平”集合,如果你不需要,它也可以。
MATCH (kevin:Person {name="Kevin Bacon"}), (charlize:Person {name:"Charlize Theron"})
MATCH p= shortestPath( (kevin)-[:ACTED_IN*]-(charlize) )
WITH nodes(p)+rels(p) AS c, length(p) AS l
RETURN [x IN range(0,l) | [c[x]).name + type(c[l+x+1])]]