如何使用计数过滤WHERE?

时间:2017-04-01 00:43:31

标签: neo4j

从这个有效的查询开始:

  MATCH (post:Post)<-[:AUTHOR]-(author:User)
  WHERE post.createdAt > { hotAfter } AND post.text =~ { keyword }
  MATCH (post)-[:HAS_COMMENT]->(comment:Comment)<-[:AUTHOR]-(commentAuthor:User)
  WITH post, author, count(DISTINCT commentAuthor) as participantsCount, count(comment) as commentsCount 
  ORDER BY (participantsCount * commentsCount) DESC
  RETURN collect(post {.*, author, participantsCount, commentsCount})[0..{ LIMIT }] as posts

我还想用符合标准的评论数来过滤帖子:

WHERE (count(DISTINCT commentAuthor) * count(comment)) <= { someNumber }

但我不确定在何处/如何应用此逻辑,例如这是不正确的:

  MATCH (post:Post)<-[:AUTHOR]-(author:User)
  WHERE post.createdAt > { hotAfter } AND post.text =~ { keyword }
  MATCH (post)-[:HAS_COMMENT]->(comment:Comment)<-[:AUTHOR]-(commentAuthor:User)
  WHERE count(DISTINCT commentAuthor) * count(comment) <= 10
  WITH post, author, count(DISTINCT commentAuthor) as participantsCount, count(comment) as commentsCount 
  ORDER BY (participantsCount * commentsCount) DESC
  RETURN collect(post {.*, author, participantsCount, commentsCount})[0..{ LIMIT }] as posts

2 个答案:

答案 0 :(得分:1)

您可以在执行ORDER BY之前将其添加为WHERE子句:

MATCH (post:Post)<-[:AUTHOR]-(author:User)
WHERE post.createdAt > { hotAfter } AND post.text =~ { keyword }
MATCH (post)-[:HAS_COMMENT]->(comment:Comment)<-[:AUTHOR]-(commentAuthor:User)
WITH post, author, count(DISTINCT commentAuthor) as participantsCount, count(comment) as commentsCount 
WITH post, author, participantsCount, commentsCount, participantsCount * commentsCount as countProduct
ORDER BY countProduct DESC
WHERE countProduct <= { someNumber }
RETURN collect(post {.*, author, participantsCount, commentsCount})[0..{ LIMIT }]

答案 1 :(得分:0)

最后对集合进行过滤似乎有效。

  MATCH (post:Post)<-[:AUTHOR]-(author:User)
  WHERE post.createdAt > { hotAfter } AND post.text =~ { keyword }
  MATCH (post)-[:HAS_COMMENT]->(comment:Comment)<-[:AUTHOR]-(commentAuthor:User)
  WITH post, author, count(DISTINCT commentAuthor) as participantsCount, count(comment) as commentsCount 
  ORDER BY (participantsCount * commentsCount) DESC
  RETURN filter (
    x in collect(post {.*, author, participantsCount, commentsCount}) 
    WHERE (x.participantsCount * x.commentsCount) <= { someNumber }
  )[0..{ LIMIT }] as posts