Reputation: 197
I need to model 'Friend' relationship in neo4j. Relationship may have types like 'Buddy' or 'Friend'. For example, A and B are friends. A considers B as a 'Buddy' and B considers A as a 'Friend'. I see two options to model this.
Create two relationships. A --> B with property 'type' = 'Buddy' and B --> A with property 'type' = 'Friend'.
Create one relationship with two properties 'Forward_Type' and 'Backward_Type'. If the relationship is from A to B, 'Forward_Type' = 'Buddy' and 'Backward_Type' will be 'Friend'
Please let me know which one is good in terms of traversing complexity/performence. Thank you.
Upvotes: 0
Views: 289
Reputation: 19373
When modelling bidirectional relationships, there are a couple of things to consider. The first is the semantic meaning of that relationship, in each direction. In your case, simply calling the relationship FRIEND is not good enough because you have types of friends.
Another consideration is whether the meaning of that relationship can be implied (in a direct or opposite fashion) from either direction. Examples of these are PARENT
and CHILD
relationships where the parent implies a child in the opposite direction. In your case, it does not. An outgoing FRIEND relationship does not imply an incoming FRIEND relationship.
There is no impact at all on performance to traverse relationships in either direction in Neo4j.
Since your relationship type is not truly bidirectional, your options depend on your use cases. If you model the FRIEND relationship to use a property on it to indicate the type of friendship, then you would have at most two relationships between any given pair of nodes, one in each direction. If your use cases involve always traversing FRIEND relationships no matter the type of friendship, then this is a good and performant model.
If you need to traverse by type of friendship, such as find all Buddies but not Friends, then the model just described may not perform as well. May not because this depends totally on the shape of your graph- if you do not have dense nodes, then this will not be an issue. If you do, then it will be since every relationship of type FRIEND from the dense node will be traversed to filter by the property on it. In this case you will be better off with two relationship types, FRIEND
and BUDDY
.
Upvotes: 2