这样的事情呢。匹配处于相同状态的所有朋友,然后结束该路径,并匹配来自不同状态的结尾旁边的所有朋友。
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
更改,以便将容纳约翰,琼和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
这样的作品,但有一个转折。如果琼是MI的John's的朋友 - 她也不应该表演。我会更新数据集更清晰。对不起,我错过了。 – Mark
第二个查询适用于Jack,但如果我从John开始,我想让Joan和Tim回来。它不会为约翰返回任何东西。这可能比我想象的更难。 – Mark
添加了新的查询。必须改变一些事情。为仅包含单个节点的路径增加了容量,并取消了关系中的定向性。 –