如何在cypher查询中使用两个匹配语句

时间:2013-05-09 15:41:39

标签: neo4j cypher

我想将两个请求合并到一个查询中,我不确定在单个cypher查询中使用2个匹配语句会发生什么。

说我有一个朋友列表,我希望看到我的朋友列表,他们的每个叔叔和兄弟姐妹都列在一个集合中。我可以使用两个匹配语句来完成这项工作吗? e.g。

match friends-[:childOf]->parents-[:brother]->uncles
    , friends-[:childOf]->parents<-[:childOf]-siblings
return friends, collect(siblings), collect(uncles)

但是,如果我这样做一个查询,它总是不返回任何结果。

2 个答案:

答案 0 :(得分:7)

由于您已经在第一场比赛课程中选择了父母,您可以这样做 -

match friends-[:childOf]->parents-[:brother]->uncles
with friends, parents, uncles
match parents<-[:childOf]-siblings
return friends, collect(siblings), collect(uncles)

答案 1 :(得分:1)

您可能希望将其中一些关系设为可选。例如,如果您找到兄弟但没有找到任何叔叔,则此查询将返回null,因为它不满足两个匹配子句。如果使结束关系成为可选,那么您不必完全满足两个子句以返回数据。所以:

match friends-[:childOf]->parents-[?:brother]->uncles
    , friends-[:childOf]->parents<-[?:childOf]-siblings
return friends, collect(siblings), collect(uncles)
相关问题