David Thomas
David Thomas

Reputation: 21

Nesting cypher queries with a collection

I have a linked list of Events in Neo4J with a :NEXT relationship. I can retrieve a aggregated collection of the next 3 Events with:

MATCH (e:Event {eventType:”123”}),
path  = (e)-[:NEXT*3]->()
RETURN [node IN nodes(path) | node.eventType] as et, count(*) as count 
ORDER BY count DESC LIMIT 10

I have the textual descriptions of the event types in EventDetail nodes connected via:

MATCH (e:Event {eventType:”123”})-[:DETAILS]->(ed:EventDetails) RETURN ed.description LIMIT 1

Is there a way of nesting these and getting the ed.description property back from the first query? thanks.

Upvotes: 2

Views: 1498

Answers (2)

Mark Needham
Mark Needham

Reputation: 2128

Maybe something like this:

MATCH (e:Event {eventType:”123”}), path = (e)-[:NEXT*3]->() 
WITH [node IN nodes(path) | id(node)] as et, count(*) as count 
MATCH (e)-[:DETAILS]->(ed) WHERE id(e) IN et
RETURN et, COLECT(ed), count 
ORDER BY count DESC LIMIT 10

Upvotes: 1

Dave Bennett
Dave Bennett

Reputation: 11216

This should return the description for every Event node with eventType: '123' in a single query.

// if every event has EventDetails then match up front
MATCH (e:Event {eventType:”123”})-[:DETAILS]->(ed:EventDetails)
// you can use the with clause to pass the nodes to teh next section where you match the path
WITH e, ed
MATCH path=e-[:NEXT*3]->() 
RETURN [node IN nodes(path) | node.eventType] as et
, ed.description
,count(*) as count 
ORDER BY count DESC LIMIT 10

Adjusting the above query to get return event details for every node in the path rather than just the first node.

// match paths of events in chains of four nodes 
// that start with eventtype 123
MATCH p=(:Event {eventType:”123”})-[:NEXT*3]->(:Event)
WITH nodes(p) as events
// loop through the events in the path
UNWIND events AS e 
// find the event details for each event
MATCH e-[:DETAILS]->(ed:EventDetails)
WITH [e.eventType, ed.description] as event_detail    
RETURN event_detail, count(*) as count 
ORDER BY count DESC LIMIT 10

Upvotes: 1

Related Questions