Tony Ennis
Tony Ennis

Reputation: 12299

In Neo4j, when is it appropriate to use a relationship?

I am modeling an event system. Say I have an event called EVENT_A. When I create an event, I capture some information - who caused the event (person A), who was affected by the event (person B), and how person B was affected (B's property (a car, house, etc) is identified.)

(EVENT_A)->[:AFFECTS]->(PERSON {is:b})

Now the crux - Do I:

  1. Put "Person A" and "B's property" into EVENT_A? There will be hundreds of EVENT_A's, eventually... Or do I
  2. Make them properties on the AFFECTS relation?

Either will work.

pros/cons for #1:

  1. There must be an event node per event; no sharing. This is annoying because I have information in EVENT_A that could be shared between all EVENT_A nodes; namely, a text description of what the event means.
  2. I can relate person A to the appropriate PERSON node and not even have that as a property on EVENT_A.

pros/cons for #2:

  1. Many of the same EVENT_A node could relate to via :AFFECTS to multiple PERSONs.
  2. AFFECTS' attributes can't be related to other nodes and thus I could lose some query goodness.
  3. The AFFECTS relationship looks empty, like I am under-utilizing it.

So what's a guideline for what goes into the relationships and which things get piled into nodes?

Upvotes: 1

Views: 42

Answers (1)

MicTech
MicTech

Reputation: 45023

Is better to have different relationship types instead of one property on the relationship.

(PERSON {name: 'Rebeca'})-[:CAUSED]->(EVENT_A {id: 1})->[:AFFECTS_CAR]->(PERSON {name: 'John'})
(PERSON {name: 'Joe'})-[:CAUSED]->(EVENT_A {id: 2})->[:AFFECTS_HOUSE]->(PERSON {name: 'Rebeca'})
(PERSON {name: 'Simon'})-[:CAUSED]->(EVENT_A {id: 3})->[:AFFECTS_ETC]->(PERSON {name: 'William'})

Also for sharing information between nodes you can have node for that.

(EVENT_A {id: 1})-[:HAS]->(DESCRIPTION {id: 1})
(EVENT_A {id: 2})-[:HAS]->(DESCRIPTION {id: 1})
(EVENT_A {id: 3})-[:HAS]->(DESCRIPTION {id: 1}) 

Upvotes: 1

Related Questions