简体   繁体   English

可选配的Neo4j

[英]Optional Match Neo4j

here is my cypher query : 这是我的密码查询:

MATCH (a : User), (user : User {username : 'lebron'}) 
WHERE a.phoneNumber IN ['757488374748','+9035115','+90390320303'] 
  AND a.username <> 'lebron' 
OPTIONAL MATCH (user)-[f:FOLLOWS]->(a), (a)-[p : POSTS]->(b) 
RETURN DISTINCT COLLECT(b.postId) AS postId, a.username AS membername, 
  f.followRequestStatus AS followRequestStatus, user.private AS userPrivate;

'a' node is user node, 'a' can posts photo, follow other users, like other users photo, pretty much like instagram. “ a”节点是用户节点,“ a”可以发布照片,关注其他用户,就像其他用户的照片一样,就像instagram。 What's happening here is that it would give followRequestStatus (f.followRequestStatus) correct only if the relation - [p : POSTS] between nodes a and b > exists. 这里发生的事情是,只有在节点a和b>之间存在关系-[p:POSTS]时,它才会使followRequestStatus(f.followRequestStatus)正确。 If relation 'p' does not exists, it returns null for relation > 'f' also, even if relation f does exist. 如果不存在关系“ p”,则即使关系f确实存在,也为关系>“ f”返回null。

Have you tried separating your two OPTIONAL MATCH patterns into two separate OPTIONAL MATCHes? 您是否尝试过将两个OPTIONAL MATCH模式分成两个单独的OPTIONAL MATCH?

...
OPTIONAL MATCH (user)-[f:FOLLOWS]->(a)
OPTIONAL MATCH (a)-[p : POSTS]->(b) 
...

In the OPTIONAL MATCH description from the developer documentation: 在开发人员文档的可选匹配说明中:

The difference [compared to MATCH] is that if no matches are found, OPTIONAL MATCH will use a null for missing parts of the pattern. [与MATCH相比]的区别在于,如果找不到匹配项,则OPTIONAL MATCH将对模式的缺失部分使用null。

When you use two separate patterns separated by a comma, my assumption is that if both matches are not found, null will be used for all parts of the pattern. 当您使用由逗号分隔的两个单独的模式时,我的假设是,如果找不到两个匹配项,则模式的所有部分都将使用null。 Since you want to :FOLLOWS relationships even if there are no :POSTS from a, you need separate OPTIONAL MATCHes. 由于即使没有来自:POSTS的情况也要:FOLLOWS关系,因此需要单独的可选匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM