有没有办法计算(评论)以获得正确的总数,然后筛选WHERE评论=?

时间:2017-04-22 03:14:04

标签: neo4j

我正在添加功能,只返回用WHERE comment.text =~ { mention }提及用户的评论。虽然这正确地返回了所提到的注释,但当我想要总共#个注释时,commentsCount成为#提到的注释。在应用commentsCount之前,我有没有办法将WHERE comment.text =~ { mention }计为#gotal评论?

// MENTIONS

  MATCH (user:User {user_id: { user_id }})

  MATCH (post:Post)<-[:AUTHOR]-(author:User)
  WHERE post.createdAt < { before } AND post.text =~ { keyword }

  MATCH (post)-[:HAS_COMMENT]->(comment:Comment)<-[:AUTHOR]-(commentAuthor:User)
  WHERE NOT user.user_id = commentAuthor.user_id AND comment.text =~ { mention }  // filter

  WITH
    post,
    author,
    commentAuthor,
    max(comment.createdAt) as commentCreatedAt,
    count(comment) as commentsPerCommenter
  ORDER BY commentCreatedAt DESC

  WITH
    post,
    author,
    sum(commentsPerCommenter) as commentsCount,
    collect(commentAuthor {.*, commentCreatedAt, commentsCount: commentsPerCommenter}) as commentAuthors

  WITH
    post,
    author,
    commentsCount,  // incorrect # mentioned comments only, want # total comments
    size(commentAuthors) as participantsCount,
    commentAuthors

  UNWIND commentAuthors as commentAuthor

  RETURN collect(post {
    .*,
    author,
    commentAuthor,
    commentsCount,
    participantsCount,
    notificationType: 'mention'
  })[0..{ LIMIT }] as posts

1 个答案:

答案 0 :(得分:2)

是的,这个查询可能会解决问题:

修改

在过滤之前添加了参与者的计算。这使用pattern comprehension,与来自APOC Proceduresapoc.coll.toSet()配对,以确保列表中只有不同的值(否则您将获得同一评论者的多次出现,一次针对帖子中的每个评论)。

  // MENTIONS

  MATCH (user:User {user_id: { user_id }})

  MATCH (post:Post)<-[:AUTHOR]-(author:User)
  WHERE post.createdAt < { before } AND post.text =~ { keyword }

  // get total comments per post
  WITH 
   post, 
   author, 
   user, 
   size((post)-[:HAS_COMMENT]->()) as commentsCount,
   size(apoc.coll.toSet(
     [(post)-[:HAS_COMMENT]->()<-[:AUTHOR]-(commentAuthor) 
       WHERE author <> commentAuthor | commentAuthor])) as participantsCount

  MATCH (post)-[:HAS_COMMENT]->(comment)<-[:AUTHOR]-(commentAuthor)
  WHERE user <> commentAuthor AND comment.text =~ { mention }  // filter

  WITH
    post,
    author,
    commentsCount,
    participantsCount,
    commentAuthor,
    max(comment.createdAt) as commentCreatedAt,
    count(comment) as commentsPerCommenter

  ORDER BY commentCreatedAt DESC

  WITH
    post,
    author,
    commentsCount,
    participantsCount,
    collect(commentAuthor {.*, commentCreatedAt, commentsCount: commentsPerCommenter}) as commentAuthors

  WITH
    post,
    author,
    commentsCount,  
    participantsCount,
    commentAuthors

  UNWIND commentAuthors as commentAuthor

  RETURN collect(post {
    .*,
    author,
    commentAuthor,
    commentsCount,
    participantsCount,
    notificationType: 'mention'
  })[0..{ LIMIT }] as posts