-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeetup-tutorial.cypher
211 lines (155 loc) · 5.64 KB
/
meetup-tutorial.cypher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//////////////////////////
// Query di riscaldamento:
//////////////////////////
// Get all users in the dataset
MATCH (u:User)
RETURN u
// Get all Meetups in the dataset
MATCH (m:Meetup)
RETURN m
// Show Meetups e Tags
MATCH (m:Meetup)-[r:TAGGED]->(t:Tag)
RETURN m, r, t
// Show relations for a specific User
MATCH (u:User {name:"<insert user name>"})-[r]->(o)
RETURN u, r, o
// Get all users who joined a Meetup after 1 November 2017
MATCH (u:User)-[r:JOINED]->(m:Meetup)
WHERE r.when > apoc.date.parse('01/11/2017', 'ms', 'dd/MM/yyyy')
RETURN u, r, m
//////////////////
// Query avanzate:
//////////////////
// Schema:
CALL db.schema();
// Show top 5 Meetups by popularity
MATCH (m:Meetup)
WITH m, size(()-[:JOINED]->(m)) as degree
ORDER BY degree DESC
RETURN m.name, degree
LIMIT 5
// Show top 10 active users by partecipation
MATCH (u:User)
WITH u, size((u)-[:JOINED]->(:Meetup)) as degree
ORDER BY degree DESC
RETURN u.name, degree
LIMIT 10
// Top 10 users by interests
MATCH (u:User)-[:JOINED]->(:Meetup)-[:TAGGED]->(t:Tag)
WITH u, SIZE(COLLECT(DISTINCT t)) as interests
ORDER BY interests DESC
RETURN u.name, interests
LIMIT 10
// Shortest path Eros -> Piero
MATCH (u1:User {name:"Piero S."}), (u2:User {name:"Eros B."}), p=shortestPath( (u1)-[*]-(u2) )
RETURN u1, u2, p
/////////////////////////
// Query raccomandazione:
/////////////////////////
// Prendere colleghi di Eros
// Prendere "meetup" frequentati dai colleghi di Eros
// Rimuovere "meetup" già frequentati da Eros
MATCH (eros:User {name: "Eros B."}),
(eros)-[:JOINED]->(:Meetup)<-[:JOINED]-(colleague:User),
(colleague)-[:JOINED]->(newMeetup:Meetup)
WHERE NOT (eros)-[:JOINED]->(newMeetup)
RETURN newMeetup.name, collect(distinct colleague.name) AS joiners, count(distinct colleague.name) as occurrences
ORDER BY occurrences DESC;
// Raccomandazioni by topic
MATCH (eros:User {name:"Eros B."}),
(eros)-[:JOINED]->(meetup:Meetup),
(meetup:Meetup)-[:TAGGED]->(tag:Tag)<-[:TAGGED]-(newMeetup:Meetup)
WHERE NOT (eros)-[:JOINED]->(newMeetup)
RETURN eros, meetup, tag, newMeetup
///////////////////////
// Funzioni di supporto
///////////////////////
// Export the graph as Cypher query to a file
CALL apoc.export.cypher.all('/tmp/meetup.cypher', {format: 'cypher-shell'})
// Empty the DB relationships
MATCH ()-[r]-() DELETE r
// Empty the DB nodes
MATCH (n) DELETE n
// Import all Meetup data from our proxy
WITH "https://meetup-proxy-zthscriext.now.sh/membership?meetups=GraphRM,RomaJS,Rust-Roma,Lambda-Roma,Blockchain-Roma,Machine-Learning-Data-Science-Meetup,DotNetCode,RomaWordPress,Agile_Talks" AS url
CALL apoc.load.json(url) YIELD value
UNWIND value.items AS e
MERGE (meetup:Meetup {id:e.meetup.id})
ON CREATE SET
meetup.name = e.meetup.name,
meetup.link = e.meetup.link
MERGE (user:User {id:e.member.id})
ON CREATE SET
user.name = e.member.name,
user.status = e.member.status
MERGE (user)-[:JOINED {as: e.meetup.who, when: e.member.joined}]->(meetup)
MERGE (organizer:User {id: e.meetup.organizer.id})
MERGE (organizer)-[:CREATED {when: e.meetup.created}]->(meetup)
FOREACH (tagName IN e.meetup.topics |
MERGE (tag:Tag {id: tagName.id, name: tagName.name})
MERGE (meetup)-[:TAGGED]->(tag)
)
// Import all Meetup event from our proxy
WITH "https://meetup-proxy-zthscriext.now.sh/attendance?meetups=GraphRM,RomaJS,Rust-Roma,Lambda-Roma,Blockchain-Roma,Machine-Learning-Data-Science-Meetup,DotNetCode,RomaWordPress,Agile_Talks" AS url
CALL apoc.load.json(url) YIELD value
UNWIND value.items AS e
MERGE (event:Event {id:e.event.id})
ON CREATE SET
event.name = e.event.name,
event.local_date = e.event.local_date,
event.local_time = e.event.local_time,
event.time = e.event.time,
event.description = e.event.description ,
event.link = e.event.link
MERGE (user:User {id:e.member.id})
ON CREATE SET
user.name = e.member.name
MERGE (meetup:Meetup {id:e.meetupId})
MERGE (meetup)-[:HAS_EVENT]->(event)
MERGE (user)-[:PARTICIPATED]->(event)
// Test for duplicates
MATCH (n1:Meetup) WHERE NOT (n1)-[:HAS_EVENT]->() RETURN n1
// More queries with single events and partecipation
// Show top 5 Meetups Events by popularity
MATCH (m:Meetup)-[:HAS_EVENT]->(e:Event)
WITH m,e, size(()-[:PARTICIPATED]->(e)) as degree
ORDER BY degree DESC
RETURN m.name,e.name, degree
LIMIT 5
// Show top 10 active users by partecipation to events
MATCH (u:User)
WITH u, size((u)-[:PARTICIPATED]->(:Event)) as degree
ORDER BY degree DESC
RETURN u.name, degree
LIMIT 10
// Show users with no events
MATCH (n:User)
WITH n,size((n)-[:PARTICIPATED]->(:Event)) as rel_count
WHERE rel_count = 0
RETURN n.name
limit 5
// Show missing event for Enrico R.
MATCH (n:User)-[:JOINED]->(m:Meetup),
(m)-[:HAS_EVENT]->(e:Event)
WHERE NOT (n)-[:PARTICIPATED]->(e) AND n.name = "Enrico R."
RETURN n.name,m.name,e.name
limit 40
// Show GraphRM meetup trends
MATCH (m:Meetup)-[:HAS_EVENT]->(e:Event)
WITH m,e,SIZE(()-[:PARTICIPATED]->(e)) as participants
WHERE m.name ="GraphRM"
RETURN m.name,participants,e.local_date
ORDER BY m.name ASC,e.time
// Show Users that joined a meetup 7 days before the next event but didn't go
MATCH(u:User)-[j:JOINED]->(m:Meetup),
(m)-[:HAS_EVENT]->(e:Event)
WHERE NOT (u)-[:PARTICIPATED]-(e) and (e.time - j.when) > 0 AND (e.time - j.when)/ (1000*60*60*24) < 7
RETURN u.name,m.name,(e.time - j.when)/ (1000* 60*60*24) as days
ORDER BY m.name,u.name, days
// Show Users that joined a meetup but never partecipated to an event
MATCH (u:User)-[j:JOINED]->(m:Meetup),
(m)-[:HAS_EVENT]->(e:Event)
WITH u, m, COLLECT(e) AS events
WHERE ALL(e IN events WHERE NOT (u)-[:PARTICIPATED]-(e))
RETURN u, m
// Show Users who changed their interest during the years