返回shortestPath中的节点

时间:2018-01-04 11:51:10

标签: neo4j cypher

给出以下数据库架构:

movie-db schema

我想使用shortestPath()函数返回将演员Bruce Wilis连接到导演Oliver Stone的电影。

我的尝试:

match p = shortestPath((d:Director{director_name:'Oliver Stone'})-[*]- (a:Actor{actor_name:'Bruce Willis'})) 
with nodes(p) as n 
where type(n) = 'Movie' 
return n

1 个答案:

答案 0 :(得分:1)

您尝试查询的问题是nodes(p)是节点的集合。尝试在节点集合上使用type()将不起作用。此外,type()仅用于获取关系类型。节点没有类型,它们有标签。

此外,您不知道在起始节点之间的最短路径中是否只有一个电影节点或多个电影节点,因此最好的方法是过滤集合,这样您只能将电影节点保留在路径:

match p = shortestPath((d:Director{director_name:'Oliver Stone'})-[*]- (a:Actor{actor_name:'Bruce Willis'})) 
return [n in nodes(p) where n:Movie] as movies

这使用list comprehension来执行集合的过滤。或者,您可以使用filter()函数:

return filter(n in nodes(p) where n:Movie) as movies