簡體   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