编辑: 跳到下面的最后一个 Cypher 语句以获得最终答案。否则,请随意阅读发现的传奇
我认为问题在于,在第一个查询中,整个 WHERE 子句仅适用于 WITH。在下面的 Cypher 中,我将第一个查询中的 WHERE 子句分成两个 WHERE,一个用于 MATCH,一个用于 WITH。希望这会产生预期的结果。
MATCH
(n:User {userId: 1234}),
(n_0:User {userId: 3345}),
(n_1:Group {groupId: 8765}),
(n_1_0:Event {eventId:3456})
WHERE (n)-[:PING {someProp:true}]->(n_0)
AND (n)-[:JOIN {someProp:"cool"}]->(n_1)
AND (n_1)-[:PUBLISH {otherProp: "Hello"}]->(n_1_0)
WITH n, n_0, n_1, n_1_0
OPTIONAL MATCH
(n)-->(n_rel),
(n_0)-->(n_0_rel),
(n_1)-->(n_1_rel),
(n_1_0)-->(n_1_0_rel)
WITH
n, count(n_rel) AS n_count,
n_0, count(n_0_rel) AS n_0_count,
n_1, count(n_1_rel) AS n_1_count,
n_1_0, count(n_1_0_rel) AS n_1_0_count
WHERE
n_count = 2
AND n_0_count = 0
AND n_1_count = 1
AND n_1_0_count = 0
RETURN n
使用电影图表我想出了这个查询。我相信它大致相当于您的查询,并遵循我最初建议的相同模式。它与您发现的问题相同。当 OPTIONAL MATCH 没有找到任何结果时,则不返回任何内容。
MATCH
(jamest:Person {name: "James Thompson"}),
(jessicat:Person {name: "Jessica Thompson"})
WHERE
(jamest)-[:FOLLOWS]->(jessicat)
WITH jamest, jessicat
OPTIONAL MATCH
(jamest)-[:REVIEWED]->(jamest_rev_rel), // James Thompson has 2 REVIEWED relationships
(jessicat)-[:FOLLOWS]->(jessicat_fol_rel) // Jessica Thompson has no outbound FOLLOWS relationships
WITH
jamest, count(jamest_rev_rel) AS jamest_rev_rel_count,
jessicat, count(jessicat_fol_rel) AS jessicat_fol_rel_count
WHERE
jamest_rev_rel_count = 2
AND jessicat_fol_rel_count = 0
RETURN jamest, jessicat // No results returned
我将查询改写成这个。这个返回预期的结果。感觉太麻烦了,但希望它能给你一些工作。我会继续修改它。
MATCH
(jamest:Person {name: "James Thompson"}),
(jessicat:Person {name: "Jessica Thompson"})
WHERE
(jamest)-[:FOLLOWS]->(jessicat)
WITH jamest, jessicat
OPTIONAL MATCH
(jamest)-[:REVIEWED]->(jamest_rev_rel)
WITH
jessicat, jamest, count(jamest_rev_rel) as jamest_rev_rel_count
WHERE
jamest_rev_rel_count = 2 // James Thompson has 2 REVIEWED relationships
WITH jamest, jessicat
OPTIONAL MATCH
(jessicat)-[:FOLLOWS]->(jessicat_fol_rel)
WITH
jamest, jessicat, count(jessicat_fol_rel) AS jessicat_fol_rel_count
WHERE
jessicat_fol_rel_count = 0 // Jessica Thompson has no outbound FOLLOWS relationships
RETURN jamest, jessicat // The two nodes are returned as expected
问题的根源在于一个 OPTIONAL MATCH 具有多个逗号分隔的模式,而不是多个 OPTIONAL MATCH 语句。在前者中,所有单独的模式都被认为是一个单一的模式。而在后者中,它们是不同的,这正是该查询所需要的。这个SO question 提供了更多细节。
查询可以稍微倾斜一点。这个版本给出了与上面相同的结果,并且在我看来更具可读性
MATCH
(jamest:Person {name: "James Thompson"}),
(jessicat:Person {name: "Jessica Thompson"})
OPTIONAL MATCH
(jamest)-[:REVIEWED]->(jamest_rev_rel)
OPTIONAL MATCH
(jessicat)-[:FOLLOWS]->(jessicat_fol_rel)
WITH
jessicat, jamest,
count(jamest_rev_rel) as jamest_rev_rel_count,
count(jessicat_fol_rel) as jessicat_fol_rel_count
WHERE
jamest_rev_rel_count = 2 // James Thompson has 2 outbount REVIEWED relationships
AND jessicat_fol_rel_count = 0 // Jessica Thompson has 0 outbound FOLLOWS relationships
RETURN
jamest, jessicat // The two nodes are returned as expected