我正在准备一个图形数据库(使用neo4j)来处理这种社交网络场景:
所以我提出了以下想法:
用户和帖子是图表的节点。当用户A
创建与P
和B
共享的帖子C
时,会创建以下关系:A-[:posted]->p
和p-[:shared_with]->B
以及p-[:shared_with]->C
。 Post
数据(id,text,date)存储为:posted
关系的属性。
对于类似的消息:例如A-[:messaged]->C
。
现在,如果我想在邮件中分享帖子,请在post_id
属性中加入:messaged
。它允许我通过单个Cypher查询提取所有消息(连同帖子链接):
match (a:User) match (b) where a.username = "C" match a-[m:messaged]-b
optional match (x:User)-[p:posted]->(post:Post)
where post.id = m.post_id
return distinct
m.post_id,
startnode(m).username as from,
endnode(m).username as to ,
x.username as PostAuthor,
case x when null then "Message" else "Pulled Post" end as Type,
case x when null then m.text else post.text end as Text,
m.date
order by m.date asc
虽然它对我来说并不是看起来 - 因为在图表上Post
和消息之间没有视觉联系。但是,我无法在Node和Relation之间建立关系,对吗?我该怎么做才能正确设计呢?
答案 0 :(得分:1)
在post和message都是节点的模型中,您的查询将如下所示:
match (a:User)<-[:FROM]-(m:Message)-[:TO]->(b:User)
where a.username = "C"
match (m)<-[:COMMENT]-(post:Post)<-[:POSTED]-(x:User)
return distinct
m.id,a as from, b as to,
x.username as PostAuthor,
case x when null then "Message" else "Pulled Post" end as Type,
case x when null then m.text else post.text end as Text,
m.date
order by m.date asc