Cypher查询,同一关系上的多个匹配

时间:2015-12-01 11:00:13

标签: neo4j cypher

假设我有一个用户模型,并发布模型和评论模型。

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

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

我想只返回一个或多个查询是否有数据。

这是一些伪密码

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.;

这可以在cypher中执行这样的复杂查询吗?理想情况下,我也喜欢收集符合一个或多个条件的用户,但不符合任何标准的用户。

或者最好将这些分解为单独的查询并处理cypher之外的逻辑?

1 个答案:

答案 0 :(得分:0)

您应该可以使用多个OPTIONAL MATCH子句。请务必将WHEREOPTIONAL 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 * 匹配:

CREATE TYPE book_score AS (
   book_id int,
   score int
);

create table score(
   critic_id int primary key,
   scores book_score[]
);

等...