Counting similar paths

Given a series of unique sequential nodes of certain types: LogIn->UpdateProfile , LogIn->AddToCart->CheckOut

cypher example:

CREATE (e1:Event {type: 'LogIn', uuid: '...'}),
(e2:Event {type: 'UpdateProfile', uuid: '...'}),
(e3:Event {type: 'LogIn', uuid: '...'}),
(e4:Event {type: 'AddToCart', uuid: '...'}),
(e5:Event {type: 'CheckOut', uuid: '...'}),

(e1)-[:NEXT]->(e2)// e1 relates to e2
(e3)-[:NEXT]->(e4)
(e4)-[:NEXT]->(e5)// e3 relates to e4, and e4 to e5

How can I get a list of paths grouped by similar patterns and the number of times they appear.

Ex:

LogIn - UpdateProfile = 5 times
LogIn - AddToCart - CheckOut = 2 times

The :NEXT relation represents the sequence and the type property is the type of event.

I think you should be able to do it with a query like this:

MATCH path = (e:Event)-[:NEXT*]->(other:Event)
RETURN [event in nodes(path) | event.type], count(*) AS count
ORDER BY count DESC

And if you only want to capture longest paths, you could make sure that the last event in the chain (other) doesn't have a NEXT relationship:

MATCH path = (e:Event)-[:NEXT*]->(other:Event)
WHERE not((other)-[:NEXT]->())
RETURN [event in nodes(path) | event.type], count(*) AS count
ORDER BY count DESC
1 Like