cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to model relationships between two entities that change over time?

I am creating a database that maps relationships between entities. These relationships change over time.

For example, methane emissions are responsible for x% of greenhouse gas emissions in 2017 and y% of greenhouse gas emissions in 2018.

Without taking time into account, it could look something like this:

2X_3_3ea30a4b3ad161b205a717a365c6ae90b0b66be8.png

I do however, want to track changes over time. My preference would be to link a relationship to a node, but this is not possible. I have two ideas in my mind.

The first is to simply create multiple relationships between the two entities, one for each year that I'm tracking:

2X_8_813352c0a4131f9a6921236adbbde8eee5deab07.png

The other is to create a proper time tree and make a node for each year, like so:

2X_0_00fb0d378478621bb77d96f1b49557bbd1511c0b.png

I'm not really sure which one to pick (or whether there is another, more sensible alternative). I tend to like the latter option with the intermediate node a little bit better, but it also introduces complexity that the simpler solution avoids.

Most articles on this topic are about nodes that are connected to a time tree, in which case it's fairly straightforward. Here it's the relationship that should be connected to the time tree, but that's not possible.

6 REPLIES 6

can you share 25% of the sample data? let me see the cardinality / distribution of the data.

There is no sample data yet.

Nodes would likely have a high number of incoming and outgoing relationships (so high cardinality).

Ben,
Did you get any solution? I am interested to hear as well.

Thanks,
Mahendar

You can use a specific relationship type to help traverse.
Example: 2017_CONTRIBUTE_TO and 2018_CONTRIBUTE_TO

We are using something like that: https://h-omer.github.io/neo4j-versioner-core/

ameyasoft
Graph Maven

Here is my solution:

Cypher script:

MERGE (g:Emissions {name: "Methane"})
MERGE (p:Percentage {percentage: "25", date: "01/01/2019"})
MERGE (e:Effect {name: "Greenhouse Gases"})

MERGE (pr:PrevPercentage {percentage: "20", date: "11/01/2018"})
MERGE (pr1:PrevPercentage {percentage: "15", date: "01/01/2018"})

MERGE (g)-[:PERCENTAGE]->(p)
MERGE (p)-[:CONTRIBUTES_TO]->(e)
MERGE (p)-[:PREVIOUS]->(pr)
MERGE (pr)-[:PREVIOUS]->(pr1)
;

Result:
2X_a_a4225e7b131f1331c69d3d9f5296de75a96eb27b.png

Analysis:

MATCH (e:Emissions)-[:PERCENTAGE]->(p:Percentage)-[:CONTRIBUTES_TO]->(g:Effect)
OPTIONAL MATCH (p)-[:PREVIOUS*]-(h:PrevPercentage)
RETURN e.name, g.name,p.percentage as Current, p.date, h.percentage as Prev, h.date;

Percentage at a specified date:
MATCH (e:Emissions)-[:PERCENTAGE]->(p:Percentage)-[:CONTRIBUTES_TO]->(g:Effect)
OPTIONAL MATCH (p)-[:PREVIOUS*]-(h:PrevPercentage)
WHERE h.date = "01/01/2018"
RETURN e.name, g.name,p.percentage as Current, p.date, h.percentage as Prev, h.date;