[英]Neo4J Cypher Exclude nodes that connect to a specific node
I'm trying to find all nodes that don't connect to a specific node. 我正在尝试查找未连接到特定节点的所有节点。 I have an app where students doing an assignment discover themes in a story, and then write explications. 我有一个应用程序,学生可以在作业中发现故事中的主题,然后撰写摘要。 Then, other students do peer reviews of these explications. 然后,其他学生对这些说明进行同行评议。 My data looks like this: 我的数据如下所示:
Assignment-hasTheme->Theme-hasChild->Theme
Annotation-theme->Theme
Explication-owner->User
Explication-annotation->Annotation
PeerReview-explication->Explication
As part of the application, when a user has to do a peer review, I have to find all the explications written by other users. 作为应用程序的一部分,当用户必须进行同行评审时,我必须找到其他用户编写的所有说明。 It seems to me like this query should work: 在我看来,这个查询应该可以工作:
MATCH
(u),
(a)-[:hasTheme]->(:Theme)
-[:hasChild*]->(:Theme)
<-[:theme]-(ann:Annotation)
<-[:annotation]-(e:Explication)
OPTIONAL MATCH
(e)<-[:explication]-(p:PeerReview)
WHERE id(a)=7 AND id(u)=4
AND (e)-[:owner]->(u)
RETURN e, count(e) AS explicationCount
ORDER BY explicationCount ASC
The problem is that it doesn't: I get all the explications that all users have written. 问题是它没有:我得到了所有用户编写的所有说明。 That includes the explications the user wrote. 这包括用户编写的说明。 Can anyone tell me how to exclude those? 谁能告诉我如何排除那些?
The problem is that the WHERE
clause is only associated with one other clause...the preceding MATCH
, OPTIONAL MATCH
, or WITH
. 问题在于WHERE
子句仅与另一个子句相关联...前面的MATCH
, OPTIONAL MATCH
或WITH
。 In your query, it's associated with the OPTIONAL MATCH
. 在您的查询中,它与OPTIONAL MATCH
关联。
If you re-read your query knowing this, you can see that the first MATCH
has no WHERE
clause, so it's matching on all assignments and all users, finding all explications. 如果您知道这一点重新阅读了查询,则可以看到第一个MATCH
没有WHERE
子句,因此它与所有分配和所有用户都匹配,找到所有重复项。
THEN it does the optional match to get :PeerReviews
matching on the given assignment and user ids where the explication owner is the user with the given id. 然后,它进行可选匹配以在给定的分配和用户ID上获得:PeerReviews
匹配,其中:PeerReviews
所有者是具有给定ID的用户。 The WHERE
is only affecting which :PeerReviews
(variable p
) are matched. WHERE
仅影响匹配的:PeerReviews
(变量p
)。
A couple other things I can see...you're introducing a variable ann
on the :Annotations
matched in the pattern, and a variable p
for the : PeerReview
, but you're not actually doing anything with these in the query. 我可以看到其他几件事...您在模式中匹配的:Annotations
上引入了变量ann
,对于: PeerReview
了变量p
,但实际上在查询中并没有对它们进行任何处理。 This also makes your OPTIONAL MATCH
useless, you're not returning or operating on the matched :PeerReviews
. 这也使您的OPTIONAL MATCH
无法使用,您不会返回或操作匹配的:PeerReviews
。
My recommendation is to remove those variables and remove your OPTIONAL MATCH
completely. 我的建议是删除这些变量并完全删除您的OPTIONAL MATCH
。
MATCH
(u),
(a)-[:hasTheme]->(:Theme)
-[:hasChild*]->(:Theme)
<-[:theme]-(:Annotation)
<-[:annotation]-(e:Explication)
WHERE id(a)=7 AND id(u)=4
AND (e)-[:owner]->(u)
RETURN e, count(e) AS explicationCount
ORDER BY explicationCount ASC
If you do want to add in the OPTIONAL MATCH
and use the matched :PeerReview
, ensure that it's below the WHERE
affecting the MATCH
, like so: 如果您确实想添加OPTIONAL MATCH
并使用匹配的:PeerReview
,请确保它位于影响MATCH
的WHERE
下方,如下所示:
MATCH
(u),
(a)-[:hasTheme]->(:Theme)
-[:hasChild*]->(:Theme)
<-[:theme]-(:Annotation)
<-[:annotation]-(e:Explication)
WHERE id(a)=7 AND id(u)=4
AND (e)-[:owner]->(u)
OPTIONAL MATCH
(e)<-[:explication]-(p:PeerReview)
RETURN e, count(e) AS explicationCount, p
ORDER BY explicationCount ASC
EDIT 编辑
In response to the comments where the desired result is each :Explication and the count of all linked :PeerReviews, you would use this query: 响应评论时,期望的结果是每个:Explication和所有链接的:PeerReviews的计数,您可以使用以下查询:
MATCH
(u),
(a)-[:hasTheme]->(:Theme)
-[:hasChild*0..]->(:Theme)
<-[:theme]-(:Annotation)
<-[:annotation]-(e:Explication)
WHERE id(a)=7 AND id(u)=4
AND (e)-[:owner]->(u)
OPTIONAL MATCH
(e)<-[:explication]-(p:PeerReview)
RETURN e, count(p) as peerReviewCount
ORDER BY peerReviewCount ASC
EDIT 编辑
Updated the above query so it will find annotations on the parent theme as well instead of just its children. 更新了上面的查询,因此它将在父主题上找到注释,而不仅仅是子主题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.