[英]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.