【问题标题】:Cypher queries, multiple matches on the same relationship密码查询,同一关系上的多个匹配项
【发布时间】:2016-03-05 06:38:15
【问题描述】:

假设我有一个用户模型、一个帖子模型和一个评论模型。

我必须创建每周更新电子邮件。

我对多个不同的数据点感兴趣,例如用户帖子上的新 cmets。用户“组”中的新用户、用户朋友的新帖子、用户朋友的帖子中的新 cmet 等。

我只想在一个或多个查询有数据时返回。

这是一些伪密码

MATCH
 (u:User)-->(friend:User)-->(friend_post:Post)-->(friend_post_comment:Comment)
 (u:User)-->(p:Post)-->(c:Comment)
WHERE
 c.created_at > ...
       ## new comments on users posts
 friend.created_at > ...
       ## This is friends who were created since ...
 friend_post.created_at > ...
       ## This is posts that were created since ... by ALL friends, not just new ones
 ...
      ## Other queries
RETURN
 u, collect(friend) as friends, collect(c) as new_comments, etc.;

这可以在密码中进行这样的复杂查询吗?理想情况下,我也希望它收集符合 1 个或多个条件的用户,而不是不符合任何条件的用户。

或者将它们分解为单独的查询并处理密码之外的逻辑是否更好?

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    您应该能够使用多个OPTIONAL MATCH 子句。确保将任何WHERE 子句与每个OPTIONAL MATCH 匹配:

    MATCH (u:User)
    
    OPTIONAL MATCH (u)-->(friend:User)
    WHERE friend.created_at > {now}
    
    OPTIONAL MATCH (u)-->(friend:User)-->(friend_post:Post)-->(friend_post_comment:Comment)
    WHERE friend_post.created_at > {now}
    
    OPTIONAL MATCH (u:User)-->(p:Post)-->(c:Comment)
    WHERE c.created_at > {now}
    
    WITH
      u,
      collect(friend) as friends,
      collect(friend_post_comment) AS new_fof_comments,
      collect(c) as new_comments;
    
    WHERE length(friends) > 0 OR length(new_fof_comments) > 0 OR length(new_comments) > 0
    
    RETURN *
    

    等等……

    【讨论】:

    • 这非常接近我想要的,但它会为数据库中的每个用户返回一个结构,即使他们没有“朋友”、“new_fof_comment”或“new_cmets”。我只想返回那些有 1 个或多个匹配项的用户。这可能吗?还是应该将结果映射到数据库之外?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    相关资源
    最近更新 更多