计算每个帖子的喜欢数量

时间:2017-01-30 22:24:42

标签: neo4j

我有以下查询来获得一个计算喜欢数量的帖子:

MATCH (user:User)-[:CREATED]->(post:Post)
WHERE post.uuid = {postUUID}
RETURN post,
       user.username AS `createdBy`,
       size((post)<-[:LIKES]-(:User)) AS `likes`

我需要的是单独计算每个帖子的喜欢数量。

我当前的查询如下:

MATCH (user:User)-[:CREATED]->(posts:Post)
RETURN posts, user.username AS `createdBy`
ORDER BY posts.createdAt DESC
SKIP {skip}
LIMIT {limit}

谢谢!

3 个答案:

答案 0 :(得分:0)

如果要返回特定帖子列表的结果(在postUUIDs参数中传递列表):

MATCH (user:User)-[:CREATED]->(post:Post)
WHERE post.uuid IN {postUUIDs}
RETURN
  post,
  user.username AS `createdBy`,
  size((post)<-[:LIKES]-(:User)) AS `likes`

答案 1 :(得分:0)

您可以使用简单聚合:

MATCH (user:User)-[:CREATED]->(post:Post)<-[L:LIKES]-(:User)
WHERE post.uuid = {postUUID}
RETURN user.username AS `createdBy`,
       post,
       count(L) as countOfLikes

答案 2 :(得分:0)

您需要重新排序查询,以便首先获得相关的帖子子集,然后对该子集执行其他匹配和计数操作。这样可以避免在首先甚至不返回的节点上执行操作。

.ts