在我的社交媒体应用程序中,我有一个neo4j数据库用于FRIEND,POSTED,LIKE,COMMENT,SHARE这样的关系
节点
(User{user_id})
(Post{title, text, created, picture})
边
(User)-[:FRIEND]->(User)
(User)-[:POSTED]->(Post)
(Post)-[:PINNED]->(User)
(User)-[:LIKE]->(Post)
(User)-[:SHARE]->(Post)
我正在寻找一个返回所有帖子中所有数据的查询 由我张贴或 给我打字或 张贴并钉在了我的朋友身上 包括每个帖子的LIKE关系和SHARE关系的数量。除此之外,我想将每个具有POSTED关系的用户的user_id返回到其中一个帖子。 所有这些ORDER BY都创建了DESC。
我正在寻找的结果将是那样的
{
{
user_id:56,
text:'This is a test',
title:'This is a title',
created:'2016.12.01.18.00.00'
post_picture:'',
likecount:9,
sharecount:3
},
{
user_id:52,
text:'This is a test',
title:'This is a title',
created:'2016.12.01.17.00.00'
post_picture:'',
likecount:12,
sharecount:4
}
}
这不起作用
MATCH (post)-[:POSTED]-(user:User)-[:FRIEND]-(friend:User)
WHERE (user{user_id:'97'})-[:POSTED]-(post)
OR (user{user_id:'97'})-[:PINNED]-(post)
OR (friend{user_id:'97'})-[:FRIEND]-(user)-[:PINNED]-(post)
AND(friend{user_id:'97'})-[:FRIEND]-(user)-[:POSTED]-(post)
OPTIONAL MATCH (post)-[liked:LIKE]-(b:User)
OPTIONAL MATCH (post)-[share:SHARE]-(d:User)
WITH post, user, count(liked) as likecount,
count(share) as sharecount
RETURN DISTINCT
user.user_id as user_id,
post.title as title,
post.text as text,
post.picture as post_picture,
post.created as created,
likecount,
sharecount,
ORDER BY created DESC
有没有办法在一个查询中获得所有这些?
答案 0 :(得分:2)
此查询应该有效:
MATCH (me:User {user_id: 123})
OPTIONAL MATCH (me)-[:POSTED]->(p1)
WITH me, COLLECT(p1) AS posts
OPTIONAL MATCH (p2)-[:PINNED]->(me)
WITH me, posts + COLLECT(p2) AS posts
OPTIONAL MATCH (me)-[:FRIEND]-(friend)-[:POSTED]->(p3)-[:PINNED]->(friend)
WITH me, posts + COLLECT(p3) AS posts
UNWIND posts AS post
WITH DISTINCT post
OPTIONAL MATCH (poster)-[:POSTED]->(post)
RETURN post, COUNT(()-[:LIKE]->(post)) AS likes, COUNT(()-[:SHARE]->(post)) AS shares, COLLECT(DISTINCT poster.user_id) AS posters
ORDER BY post.created DESC;
它收集符合posts
集合中每个条件的帖子,过滤掉重复项,查找发布帖子的用户,并按所需顺序返回结果。
答案 1 :(得分:0)
什么不起作用?你有什么回来的?这将是一个无效的密码查询,因为在ORDERing之前有一个悬空的逗号。
这是您的密码查询的已清理版本。
MATCH (post)-[:POSTED]-(user:User)-[:FRIEND]-(friend:User)
WHERE (user{user_id:'97'})-[:POSTED]-(post)
OR (user{user_id:'97'})-[:PINNED]-(post)
OR (friend{user_id:'97'})-[:FRIEND]-(user)-[:PINNED]-(post)
AND(friend{user_id:'97'})-[:FRIEND]-(user)-[:POSTED]-(post)
OPTIONAL MATCH (post)-[liked:LIKE]-(b:User)
OPTIONAL MATCH (post)-[share:SHARE]-(d:User)
WITH post, user, count(liked) as likecount,
count(share) as sharecount
RETURN DISTINCT
user.user_id as user_id,
post.title as title,
post.text as text,
post.picture as post_picture,
post.created as created,
likecount,
sharecount,
ORDER BY created DESC
如果这不起作用,您是否可以提供测试数据库或cypher语句来创建您要操作的图形?在为您的问题找到一个好的解决方案时,这将非常有用。