[英]Graph database - how to reference Nodes from within Relationship
I'm preparing a graph database (using neo4j) to handle the kind of social network scenario: 我正在准备一个图形数据库(使用neo4j)来处理这种社交网络场景:
So I came up with the following idea: 所以我想出了以下想法:
Users and Posts are the Nodes of the graph. 用户和帖子是图的节点。 When the user
A
creates a post P
sharing it with both B
and C
, the following relationships are created: A-[:posted]->p
and p-[:shared_with]->B
and p-[:shared_with]->C
. 当用户
A
创建与B
和C
共享的帖子P
,将创建以下关系: A-[:posted]->p
和p-[:shared_with]->B
和p-[:shared_with]->C
The Post
data (id, text, date) are stored as properties of the :posted
relation. Post
数据(id,文本,日期)存储为:posted
关系的属性。
For messages it's similar: A-[:messaged]->C
for example. 对于消息,它类似于:例如
A-[:messaged]->C
Now, if I want to share the post in a message, I include the post_id
in :messaged
properties. 现在,如果我想在消息中共享该帖子,请在
:messaged
属性中包含post_id
。 It allows me to pull all the messages (together with the posts linked) with a single Cypher query: 它允许我通过单个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
It doesn't look right to me though - since on the graph there's no visual connection between the Post
and the message. 不过,在我看来,这并不正确-因为在图表上,“
Post
和消息之间没有视觉联系。 But, I can't set a relation between Node and Relation, right? 但是,我无法在Node和Relation之间设置关系,对吗? How should I do it in order to have it designed properly?
我应该如何做才能正确设计它?
In a model where post and message are both a node, your query would look like this: 在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.