[英]Graph database - how to reference Nodes from within Relationship
我正在准备一个图形数据库(使用neo4j)来处理这种社交网络场景:
所以我想出了以下想法:
用户和帖子是图的节点。 当用户A
创建与B
和C
共享的帖子P
,将创建以下关系: A-[:posted]->p
和p-[:shared_with]->B
和p-[:shared_with]->C
Post
数据(id,文本,日期)存储为:posted
关系的属性。
对于消息,它类似于:例如A-[:messaged]->C
现在,如果我想在消息中共享该帖子,请在:messaged
属性中包含post_id
。 它允许我通过单个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之间设置关系,对吗? 我应该如何做才能正确设计它?
在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.