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.

Apoc-link with for each or group by

Hello,

I am using apoc.nodes.link to create links between nodes in chronological order. So far so good, but I want to use a group by or for each to create chains depending on an attribute of the node. 

 

The query I use to generate the links for events of type A, B and C is: 

match (e:myevents{Eventtype:'A' }) with e order by e.timestamp
WITH collect (e) as chain
CALL apoc.nodes.link(chain, 'NEXT_EVENTS',{avoidDuplicates: true}) RETURN chain;
match (e:myevents{Eventtype:'B' }) with e order by e.timestamp
WITH collect (e) as chain
CALL apoc.nodes.link(chain, 'NEXT_EVENTS',{avoidDuplicates: true}) RETURN chain;
match (e:myevents{Eventtype:'C' }) with e order by e.timestamp
WITH collect (e) as chain
CALL apoc.nodes.link(chain, 'NEXT_EVENTS',{avoidDuplicates: true}) RETURN chain;

Any suggestions on how I can make this dynamic so that the chains are generated for each of the eventtypes? 

1 REPLY 1

I am not sure this is what you meant, but I tried what I thought you meant. 

with ['A','B','C'] as eventTypes
unwind eventTypes as eventType
match (e:myevents{Eventtype:eventType}) 
with eventType, e 
order by e.timestamp
with eventType, collect (e) as chain
call apoc.nodes.link(chain,'NEXT_EVENTS',{avoidDuplicates: true}) 
return *

Before:

Screen Shot 2022-12-08 at 12.57.58 AM.png 

After:

Screen Shot 2022-12-08 at 12.58.57 AM.png

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online