繁体   English   中英

图形数据库-如何从关系中引用节点

[英]Graph database - how to reference Nodes from within Relationship

我正在准备一个图形数据库(使用neo4j)来处理这种社交网络场景:

  • 用户可以张贴在墙上,与任一特定用户共享帖子
  • 用户可以向其他人发送消息
  • 消息可以是文本或“链接”到帖子

所以我想出了以下想法:

用户和帖子是图的节点。 当用户A创建与BC共享的帖子P ,将创建以下关系: A-[:posted]->pp-[:shared_with]->Bp-[: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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM