Lior Goldemberg
Lior Goldemberg

Reputation: 876

Neo4j similar paths

i want to do a query that will take all users (without a pre-condition like user ids) , and to find the common similar paths. (for example top 10 users flows)

For example:

User u1 has events: a,b,c,d

User u2 has events: b,d,e

Each event is a node with property event-type

the result should look like:

[a,b,e] - 100 users

[a,c,f] -80 users

[b,d,t]- 50 users

.......

the data the generated the 1st aggregated row in the result can be for example:

user 1: a,b,c,e

user 2: a,b,e,f

.........

user 100: a,c,t,b,g,e

i wonder if this link can help: http://neo4j.com/docs/stable/rest-api-graph-algos.html#rest-api-execute-a-dijkstra-algorithm-with-equal-weights-on-relationships

Upvotes: 0

Views: 184

Answers (1)

cybersam
cybersam

Reputation: 67044

Here is a Cypher query that returns all the Event nodes that user 1 and user 2 have in common (in a single row):

MATCH (u1:User {id: 1}) -[:HAS]-> (e:Event) <-[:HAS]- (u2:User {id: 2})
RETURN u1, u2, COLLECT(e);

[Added by MichaelHunger; modified by cybersam] For your additional question try:

// Specify the user ids of interest. This would normally be a query parameter.
WITH [1,2,3] as ids
MATCH (u1:User) -[:HAS]-> (e:Event)
// Only match events for users with one of the specified ids.
WHERE u1.id IN ids
// Count # of distinct user ids per event, and count # of input ids
WITH e, size(collect(distinct u1.id)) as n_users, size(ids) AS n_ids
// Only match when the 2 counts are the same
WHERE n_users = n_ids
RETURN e;

Upvotes: 3

Related Questions