cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 1: replicate users.

Apply relationship to nodes in a sequence

henry007
Node Link

Hello, I am importing from CSV data with Sessions and Events. Sessions point to Events, where each Event name is in sequential order (event1, event2, event3, etc). I would like each event to point to the next event in the sequence, like event1-->event2-->event3. Attached is a graph of this desired effect. 

henry007_0-1674671965997.png

I've been able to get Sessions to point to events with HAS_EVENT just fine. But I've not been able to get the NEXT to work for events in sequential order. 

I would be very thankful if anyone could help me achieve this 🙂. Thank you!

1 ACCEPTED SOLUTION

Try this. You can adjust to your property that equals 'rank'.

match(s:Sequence)-[:HAS_EVENT]->(e:Event)
with s, e
order by e.rank
with s, collect(e) as events
unwind range(0,size(events)-2) as index
with events[index] as e0, events[index+1] as e1
create(e0)-[:NEXT]->(e1)

Before:

Screen Shot 2023-01-25 at 2.23.46 PM.png

After:

Screen Shot 2023-01-25 at 2.24.30 PM.png

Test Data:

create(s0:Sequence{id:0}), (e0:Event{rank:0}), (e1:Event{rank:1}), (e2:Event{rank:2})
create(s0)-[:HAS_EVENT]->(e0),(s0)-[:HAS_EVENT]->(e1),(s0)-[:HAS_EVENT]->(e2);
create(s0:Sequence{id:1}), (e0:Event{rank:0}), (e1:Event{rank:1}), (e2:Event{rank:2})
create(s0)-[:HAS_EVENT]->(e0),(s0)-[:HAS_EVENT]->(e1),(s0)-[:HAS_EVENT]->(e2);

View solution in original post

8 REPLIES 8

glilienfield
Ninja
Ninja

What determines the order of the events in the chain?  Is there a timestamp or a property that has an order value in each event?  

Hello, the order of events is based on the name: event1 is first, event2 second, etc. 

so, alphabetical order?

Yes

will you have more than 10 events, as that will not work alphabetically unless you zero pad the numbers, such as event01, event02....event11, etc. With one zero, you would be limited to 100 events, with two zeros 1000 events, etc. 

The events are actually numbered like "1", "2", "3" without "event". I just put the "event" in there to make demonstration easier, but I did not foresee the case you just discussed. So it can be sorted with toInteger. 

Try this. You can adjust to your property that equals 'rank'.

match(s:Sequence)-[:HAS_EVENT]->(e:Event)
with s, e
order by e.rank
with s, collect(e) as events
unwind range(0,size(events)-2) as index
with events[index] as e0, events[index+1] as e1
create(e0)-[:NEXT]->(e1)

Before:

Screen Shot 2023-01-25 at 2.23.46 PM.png

After:

Screen Shot 2023-01-25 at 2.24.30 PM.png

Test Data:

create(s0:Sequence{id:0}), (e0:Event{rank:0}), (e1:Event{rank:1}), (e2:Event{rank:2})
create(s0)-[:HAS_EVENT]->(e0),(s0)-[:HAS_EVENT]->(e1),(s0)-[:HAS_EVENT]->(e2);
create(s0:Sequence{id:1}), (e0:Event{rank:0}), (e1:Event{rank:1}), (e2:Event{rank:2})
create(s0)-[:HAS_EVENT]->(e0),(s0)-[:HAS_EVENT]->(e1),(s0)-[:HAS_EVENT]->(e2);

This works, thank you so much. 😁