Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-25-2023 10:38 AM - edited 01-25-2023 10:39 AM
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.
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!
Solved! Go to Solution.
01-25-2023 11:28 AM
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:
After:
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);
01-25-2023 11:04 AM
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?
01-25-2023 11:05 AM
Hello, the order of events is based on the name: event1 is first, event2 second, etc.
01-25-2023 11:06 AM
so, alphabetical order?
01-25-2023 11:07 AM
Yes
01-25-2023 11:08 AM
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.
01-25-2023 11:10 AM
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.
01-25-2023 11:28 AM
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:
After:
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);
01-25-2023 11:42 AM
This works, thank you so much. 😁
All the sessions of the conference are now available online