2
votes

As Neo4j doesn't allow relationships having no direction. We modelled relationships that are naturally not having any direction, in some arbitrary direction.

(p1:Person)-[team_member_of]->(p2:Person)
(p1:Person)<-[team_member_of]<-(p3:Person)

While querying, i want to know all team members of p1. That can be done

MATCH(p1:Person)-[team_member_of]-(Person)
WHERE id(p1) = someId

For this relationship type it make sense to travese in both directions. There are other relationship types say

(p1:Person)-[driver_of]->(p2:Person)
(p1:Person)<-[driver_of]-(p3:Person)

While querying, i want to know all drivers of p1. That can be done

MATCH(p1:Person)<-[driver_of]-(Person)
WHERE id(p1) = someId

In this case, we need to query in only one direction.

In order to distinguish these two use cases, we have added a property on each relationship. saying directed: true/false (This is kind of defeating the purpose why neo4j doesn't allow relationships without direction.)

I can't think of any other way to find out if the relationship is really has direction / directionless

Any thoughts on how to do it in a nice way?

1
In which situation are you making use of the directed: true/false relationship property? - Bruno Peres
That kind of knowledge normally does not need to be stored in the DB. Developers should have data model documentation that indicate which relationships should be treated as bidirectional, and why. It is very rare that you have no choice but to write a query that decides at runtime whether a relationship should be treated as directional or not. - cybersam
@cybersam thanks a lot. i moved that knowledge to my API. Now there is not need to maintaining property on relationships. i maintain the information in cache, and for each request i check in the cache to create a cypher query that is directed/undirected. It works! - brownfox

1 Answers

0
votes

One way would be to create a Team node and have Person nodes with a "MEMBER_OF" relationship to the Team node. The Team node will be useful should you need to store properties against the Team, and gives you a way to find peers of a team member ("Bob") something like this:

MATCH (p:Person {name: "Bob"})-[:MEMBER_OF]->(Team)<-[:MEMBER_OF]-(peer:Person)
RETURN peer

Relationships always tend to have a direction, even peer to peer relationships usually have some common parent.