Cypher查询在特定条件后停止

时间:2017-03-16 12:51:39

标签: neo4j cypher

我是Neo4j和Cypher的新手。我有一个由友谊相关的人的大图,我想从1个人开始所有的路径,但停在另一个州的朋友。我需要向其他州展示第一位朋友,但不要展示他/她的朋友。基本上,我希望路径在找到某个节点的相关朋友不是来自指定状态时结束。 任何帮助将不胜感激。

Jack {st:'MI'}<-[Friend]-Jill {st:'MI'}<-[Friend]-John {st:'OH'}<-[Friend]-Tim {st:'OH'}

查询应该只返回Jack,Jill和John。不是蒂姆。

编辑/添加

John <-[Friend]-Joan {st:'MI'}

Joan也不应该在查询中返回。

谢谢!

1 个答案:

答案 0 :(得分:0)

这样的事情怎么样?匹配处于相同状态的所有朋友,然后在该路径的末尾匹配并匹配来自不同状态的所有朋友。

MATCH path=(start:Person {name: 'Jack'})<-[:FRIEND*]-(end:Person)
WHERE end.st = start.st
AND all(p in nodes(path) where p.st = start.st)
WITH path, end
RETURN nodes(path), [(end)<-[:FRIEND]-(other:Person) WHERE other.st <> end.st | other]  as other_state

此查询仅返回存在other_state个节点的行。

MATCH path=(start:Person {name: 'Joe'})<-[:FRIEND*..10]-(end:Person)
WHERE end.st = start.st
AND all(p in nodes(path) where p.st = start.st)
WITH nodes(path) as same_state, [(end)<-[:FRIEND]-(other:Person) WHERE other.st <> end.st | other]  as other_state
WHERE size(other_state) > 0
RETURN same_state, other_state

更改以便容纳John,Joan和Tim用例

MATCH path=(start:Person {name: 'John'})-[:FRIEND*0..]-(end:Person)
WHERE end.st = start.st
AND all(p in nodes(path) where p.st = start.st)
WITH nodes(path) as same_state, [(end)-[:FRIEND]-(other:Person) WHERE other.st <> end.st | other]  as other_state
WHERE size(other_state) > 0
RETURN same_state, other_state