简体   繁体   English

Neo4J Cypher排除连接到特定节点的节点

[英]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子句仅与另一个子句相关联...前面的MATCHOPTIONAL MATCHWITH 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 ,请确保它位于影响MATCHWHERE下方,如下所示:

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.

相关问题 Neo4j Cypher 排除缺少特定关系的节点 - Neo4j Cypher exclude nodes where a specific relationship is missing 如何从Neo4j Cypher搜索结果中排除连接到特定节点的节点 - How to exclude nodes connected to a specific node from Neo4j Cypher search results Neo4J / Cypher需要帮助编写查询,以显示通过特定数量的路径连接到初始节点的所有结果节点 - Neo4J / Cypher need hel writing a query that shows all the result nodes that connect to an initial node by a specific number of paths Neo4j Cypher:从结果中排除某些节点 - Neo4j Cypher: exclude certain nodes from result Neo4j cypher查询以获取与特定其他节点无关的所有节点 - Neo4j cypher query to get all nodes without a relationship to specific other node Neo4j Cypher Query 获取所有子节点,直到到达具有特定关系的节点 - Neo4j Cypher Query to get all sub nodes until reaching a node with a specific relationship Neo4j cypher 查询删除a特定节点的子节点和孙节点 - Neo4j cypher query to delete child and grandchild nodes of a a specific node Neo4j Cypher:通过与另一个节点的关系对节点进行分组 - Neo4j Cypher: Group nodes by the relation to another node Neo4j Cypher查询以查找节点数组的节点ID - Neo4j Cypher query to find node IDs for array of nodes Neo4j 合并节点 cypher 单节点关系 - Neo4j consolidate nodes in cypher single node relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM