Reputation: 87261
I'm preparing a graph database (using neo4j) to handle the kind of social network scenario:
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
. The Post
data (id, text, date) are stored as properties of the :posted
relation.
For messages it's similar: A-[:messaged]->C
for example.
Now, if I want to share the post in a message, I include the post_id
in :messaged
properties. It allows me to pull all the messages (together with the posts linked) with a single Cypher query:
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. But, I can't set a relation between Node and Relation, right? How should I do it in order to have it designed properly?
Upvotes: 1
Views: 738
Reputation: 41706
In a model where post and message are both a node, your query would look like this:
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
Upvotes: 1