Need help to structure nodes and build cypher query

My node structure is like this -
User -> FOLLOWS -> User
User -> FOLLOWS_TEAM -> Team

By using following code I can get Messages POSTED by User and Team I am following.

MATCH (me:User)-[u_rel_source:FOLLOWS_TEAM|:FOLLOWS*0..1]->(users)-[u_rel:POSTED]->(msg:Message)

Question -
Now I want to add one more node :Organisation above Team like
Team<-[r:HAS]-Oraganisation
and User can follow Organisation, in that case User should get messages posted by Teams of followed Organisation.
How can I do it in one query and how relationships will be?

I am not able to find logic for same.
Thanks in advance

What data do you want to return? Just the messages, or do you need the relationships returned too? Are you interested only in the graph result view or do you have a specific tabular output as a requirement?

I want to return relationships and messages both in graph result view.

Give this a try:

MATCH (me:User)-[:FOLLOWS_TEAM|:FOLLOWS*0..1]->(userOrTeamOrOrg)-[:HAS*0..1]->(userOrTeam)-[:POSTED]->(msg:Message)

This way, if the thing being followed is an :Organization, then there's a :HAS relationship to traverse to get to the :Team. Otherwise, if the node is a :Team or :User there will be no outgoing :HAS relationship.

EDIT

Fixed direction of :HAS relationship.

1 Like

I tried same,
Its not working.
Here its not checking if me following org.

Ah, you didn't mention you were using a separate relationship type for that. Add that in, see if it works.

yes I tried that as well.
Like this -

MATCH (me:User)-[post_rel_source:FOLLOWS_TEAM|:FOLLOWS*0..1]->(u)<-[:EVENTS_OF_ORG*0..1]-(users)<-[f:EVENTS_OF_ORG]-(me)

its not showing result of followed teams and users also.

Again, you never mentioned this relationship. Can you provide the relationships in use around the :Organization node and how they're used?

I had assumed you use something like

(:User)-[:FOLLOWS_ORG]->(:Organization)
(:Team)<-[:HAS]-(:Organization)

Which would mean we have to traverse the following paths:

(:User)-[:FOLLOWS_ORG]->(:Organization)-[:HAS]->(:Team)-[:POSTED]->(:Message)
(:User)-[:FOLLOWS_TEAM]->(:Team)-[:POSTED]->(:Message)
(:User)-[:FOLLOWS]->(:User)-[:POSTED]->(:Message)

So the patterns to traverse would be as previously posted (with a quick fix to the direction of :HAS):

MATCH (me:User)-[:FOLLOWS_TEAM|FOLLOWS_ORG|FOLLOWS*0..1]->(userOrTeamOrOrg)-[:HAS*0..1]->(userOrTeam)-[:POSTED]->(msg:Message)