-
Notifications
You must be signed in to change notification settings - Fork 6
/
stackoverflow.neo4j-browser-guide
475 lines (419 loc) · 16.2 KB
/
stackoverflow.neo4j-browser-guide
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<style type="text/css" media="screen">
/*
.nodes-image {
margin:-100;
}
*/
@import url("//maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css");
.imageblock .content img, .image img {max-width: 100%;}
.deck h3, .deck h4 {display: block !important;margin-bottom:8px;margin-top:5px;}
.listingblock {margin:8px;}
.pull-bottom {position:relative;bottom:1em;}
.admonitionblock td.icon [class^="fa icon-"]{font-size:2.5em;text-shadow:1px 1px 2px rgba(0,0,0,.5);cursor:default}
.admonitionblock td.icon .icon-note:before{content:"\f05a";color:#19407c}
.admonitionblock td.icon .icon-tip:before{content:"\f0eb";text-shadow:1px 1px 2px rgba(155,155,0,.8);color:#111}
.admonitionblock td.icon .icon-warning:before{content:"\f071";color:#bf6900}
.admonitionblock td.icon .icon-caution:before{content:"\f06d";color:#bf3400}
.admonitionblock td.icon .icon-important:before{content:"\f06a";color:#bf0000}
.admonitionblock.note.speaker { display:none; }
</style>
<style type="text/css" media="screen">
/* #editor.maximize-editor .CodeMirror-code { font-size:24px; line-height:26px; } */
</style>
<article class="guide" ng-controller="AdLibDataController">
<carousel class="deck container-fluid">
<!--slide class="row-fluid">
<div class="col-sm-3">
<h3>Exploring Stackoverflow</h3>
<p class="lead">Information</p>
<!dl>
</dl>
</div>
<div class="col-sm-9">
<figure>
<img style="width:300px" src=""/>
</figure>
</div>
</slide-->
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Exploring Stackoverflow</h3>
<br/>
<div>
<div class="imageblock" style="float: right;">
<div class="content">
<img src="https://guides.neo4j.com/sandbox/stackoverflow/img/so_logo.svg" alt="so logo" width="200px">
</div>
</div>
<div class="paragraph">
<p>Every developer has a tab open <a href="https://stackoverflow.com/" target="_blank">Stack Overflow</a>, the massively popular question-and-answer site. This dataset explores users, questions, answers, comments, and tags related to the <code>neo4j</code>- and <code>cypher</code>- tagged questions on Stack Overflow.</p>
</div>
<div class="paragraph">
<p>This guide will show you how to:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Create: load questions, answers, comments, tags, and users into the graph</p>
</li>
<li>
<p>Find: Find unanswered questions</p>
</li>
<li>
<p>Query: discover the most engaged users and most popular questions</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Throughout the guide you’ll find Cypher statements that you can execute, by clicking on them and then executing them by hitting the run button.</p>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>The Model</h3>
<br/>
<div>
<div class="imageblock">
<div class="content">
<img src="https://guides.neo4j.com/sandbox/stackoverflow/img/model.svg" alt="model" width="100%">
</div>
</div>
<h4>Nodes</h4>
<div class="paragraph">
<p>We model the <code>Users</code>, <code>Questions</code>, <code>Answers</code>, <code>Comments</code>, and <code>Tags</code> as nodes.</p>
</div>
<h4>Relationships</h4>
<div class="paragraph">
<p>Users ask questions, comment, and provide answers.</p>
</div>
<div class="paragraph">
<p><code>(:User)-[:ASKED]→(:Question)</code>
<code>(:User)-[:COMMENTED]→(:Comment)</code>
<code>(:User)-[:PROVIDED]→(:Answer)</code></p>
</div>
<div class="paragraph">
<p>Answers attempt to answer questions, comments are made on questions, and questions are associated with tags.</p>
</div>
<div class="paragraph">
<p><code>(:Answer)-[:ANSWERED]→(:Question)</code>
<code>(:Comment)-[:COMMENTED_ON]→(:Question)</code>
<code>(:Question)-[:TAGGED]→(:Tag)</code></p>
</div>
<div class="paragraph">
<p>If you want to see it yourself, run:</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->CALL db.schema.visualization;<!--/code--></pre>
</div>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Load JSON Import</h3>
<br/>
<div>
<div class="paragraph">
<p>There is already data preloaded in this graph, if you want to extend (more pages or other tags) or update it with the most recent questions, please modify and run the statement below.</p>
</div>
<div class="paragraph">
<p>Update this dataset using <code>apoc.load.json</code>. Run the query as-is to update or add a tag to the <code>tags</code> array to extend.</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->// look for several pages of questions
WITH ["neo4j","cypher"] as tags
UNWIND tags as tagName
UNWIND range(1,2) as page
WITH "https://api.stackexchange.com/2.3/questions?page="+page+"&pagesize=25&order=desc&sort=creation&tagged="+tagName+"&site=stackoverflow&filter=!5-i6Zw8Y)4W7vpy91PMYsKM-k9yzEsSC1_Uxlf" as url
CALL apoc.load.json(url) YIELD value
CALL apoc.util.sleep(250) // careful with throttling
UNWIND value.items AS q
// create the questions
MERGE (question:Question {uuid:q.question_id})
ON CREATE SET question.title = q.title,
question.link = q.share_link,
question.creation_date = q.creation_date,
question.accepted_answer_id=q.accepted_answer_id,
question.view_count=q.view_count,
question.answer_count=q.answer_count,
question.body_markdown=q.body_markdown
// who asked the question
MERGE (owner:User {uuid:coalesce(q.owner.user_id,'deleted')})
ON CREATE SET owner.display_name = q.owner.display_name
MERGE (owner)-[:ASKED]->(question)
// what tags do the questions have
FOREACH (tagName IN q.tags |
MERGE (tag:Tag {name:tagName})
ON CREATE SET tag.link = "https://stackoverflow.com/questions/tagged/" + tag.name
MERGE (question)-[:TAGGED]->(tag))
// who answered the questions?
FOREACH (a IN q.answers |
MERGE (question)<-[:ANSWERED]-(answer:Answer {uuid:a.answer_id})
ON CREATE SET answer.is_accepted = a.is_accepted,
answer.link=a.share_link,
answer.title=a.title,
answer.body_markdown=a.body_markdown,
answer.score=a.score,
answer.favorite_score=a.favorite_score,
answer.view_count=a.view_count
MERGE (answerer:User {uuid:coalesce(a.owner.user_id,'deleted')})
ON CREATE SET answerer.display_name = a.owner.display_name
MERGE (answer)<-[:PROVIDED]-(answerer)
)
// who commented ont he question
FOREACH (c in q.comments |
MERGE (question)<-[:COMMENTED_ON]-(comment:Comment {uuid:c.comment_id})
ON CREATE SET comment.link=c.link, comment.score=c.score
MERGE (commenter:User {uuid:coalesce(c.owner.user_id,'deleted')})
ON CREATE SET commenter.display_name = c.owner.display_name
MERGE (comment)<-[:COMMENTED]-(commenter)
);<!--/code--></pre>
</div>
</div>
<div class="paragraph">
<p>Read More: <a href="https://neo4j.com/labs/apoc/4.1/import/load-json/#load-json-examples-stackoverflow" target="_blank">Import from StackOverflow API</a></p>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Basic Queries</h3>
<br/>
<div>
<div class="paragraph">
<p>Labels in the graph and counts for each:</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (n)
RETURN labels(n) as label, count(*) as freq
ORDER BY freq DESC;<!--/code--></pre>
</div>
</div>
<div class="paragraph">
<p>Relationship-types in the graph and counts for eachabel:</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH ()-[r]->()
RETURN type(r) as type, count(*) as freq
ORDER BY freq DESC;<!--/code--></pre>
</div>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Top Tags</h3>
<br/>
<div>
<div class="paragraph">
<p>Which are the most popular tags?</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (q:Question)-[:TAGGED]->(t:Tag)
RETURN t.name, count(q) AS questions
ORDER BY questions DESC
LIMIT 5;<!--/code--></pre>
</div>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Exploring Users</h3>
<br/>
<div>
<div class="paragraph">
<p>Who are the top users asking questions?</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (u:User)-[:ASKED]->(q:Question)
RETURN u.display_name, count(*) AS questions
ORDER by questions DESC
LIMIT 10;<!--/code--></pre>
</div>
</div>
<div class="paragraph">
<p>Who’s answering?</p>
</div>
<div class="paragraph">
<p>Ordered by number of answers</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (u:User)-[:PROVIDED]->(a:Answer)-[:ANSWERED]->(q:Question)
RETURN u.display_name as user,COUNT(a) AS answers, avg(a.score) as avg_score
ORDER BY answers DESC LIMIT 10;<!--/code--></pre>
</div>
</div>
<div class="paragraph">
<p>Ordered by max score, filtered for a particular tag</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (u:User)-[:PROVIDED]->(a:Answer)-[:ANSWERED]->
(q:Question)-[:TAGGED]->(:Tag {name:"cypher"})
RETURN u.display_name as user,COUNT(a) AS answers, max(a.score) as max_score
ORDER BY max_score DESC LIMIT 10;<!--/code--></pre>
</div>
</div>
<div class="paragraph">
<p>What’s the shortest path between users?</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH path = allShortestPaths(
(u1:User {display_name:"alexanoid"})-[*]-(u2:User {display_name:"InverseFalcon"})
)
RETURN path LIMIT 1;<!--/code--></pre>
</div>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>User Engagement</h3>
<br/>
<div>
<div class="paragraph">
<p>User engagement over time:</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (u:User)-[:PROVIDED]->()-[:ANSWERED]->
(q:Question)-[:TAGGED]->(t:Tag)
WHERE u.display_name = "InverseFalcon"
RETURN apoc.date.format(q.creation_date,'s','yyyy-MM') as month,
count(distinct q) as count, collect(distinct t.name) as tags
ORDER BY month asc<!--/code--></pre>
</div>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Unanswered Questions</h3>
<br/>
<div>
<div class="paragraph">
<p>What are the tags for unanswered questions?</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (q:Question)-[:TAGGED]->(t:Tag)
WHERE NOT t.name IN ['neo4j','cypher']
AND NOT (q)<-[:ANSWERED]-()
RETURN t.name as tag, count(q) AS questions
ORDER BY questions DESC LIMIT 10;<!--/code--></pre>
</div>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>How are tags related to other tags?</h3>
<br/>
<div>
<div class="paragraph">
<p>Tag correlations:</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (t1:Tag)<-[:TAGGED]-()-[:TAGGED]->(t2:Tag)
WHERE id(t1) < id(t2) and t1.name <> 'neo4j' and t2.name <> 'neo4j'
RETURN t1.name, t2.name,count(*) as freq
ORDER BY freq desc LIMIT 10;<!--/code--></pre>
</div>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Virtual Graphs: Tags</h3>
<br/>
<div>
<div class="paragraph">
<p>Project tags via co-occurrence with virtual relationships.</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (t1:Tag)<-[:TAGGED]-()-[:TAGGED]->(t2:Tag)
WHERE id(t1) < id(t2) and t1.name <> 'neo4j' and t2.name <> 'neo4j'
WITH t1, t2,count(*) as freq where freq > 3
RETURN t1,t2, apoc.create.vRelationship(t1,'OCCURRED',{freq:freq},t2) as rel<!--/code--></pre>
</div>
</div>
<div class="paragraph">
<p>If you want to you can also materialize those relationships in the graph and then explore the data in Browser or Bloom visually forming clusters.</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (t1:Tag)<-[:TAGGED]-()-[:TAGGED]->(t2:Tag)
WHERE id(t1) < id(t2) and t1.name <> 'neo4j' and t2.name <> 'neo4j'
WITH t1, t2,count(*) as freq where freq > 3
MERGE (t1)-[r:OCCURRED]-(t2) SET r.freq=freq
RETURN count(*)<!--/code--></pre>
</div>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Virtual Graphs: Social Network</h3>
<br/>
<div>
<div class="paragraph">
<p>We can do something similar for commenters - what users keep on running into each other in the comments?</p>
</div>
<div class="listingblock">
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH p1=(u1:User)-[:COMMENTED]->(c1:Comment)-[:COMMENTED_ON]-(q:Question)
MATCH p2=(u2:User)-[:COMMENTED]->(c2:Comment)-[:COMMENTED_ON]-(q)
WHERE id(u1) < id(u2)
WITH u1, u2, count(distinct q) as freq
WHERE freq > 2
RETURN u1, u2, apoc.create.vRelationship(u1,'OCCURRED',{freq:freq},u2) as rel<!--/code--></pre>
</div>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Further Reading</h3>
<br/>
<div>
<div class="ulist">
<ul>
<li>
<p><a href="https://neo4j.com/labs/apoc/4.1/import/load-json/#load-json-examples-stackoverflow" target="_blank">Import from StackOverflow API</a></p>
</li>
<li>
<p><a href="https://neo4j.com/videos/exploring-stackoverflow-data-with-michael-hunger-twitch-stream/" target="_blank">Exploring StackOverflow data with Michael Hunger – Twitch stream</a></p>
</li>
<li>
<p><a href="https://neo4j.com/blog/import-10m-stack-overflow-questions/" target="_blank">Import 10M Stack Overflow Questionsinto Neo4j In Just 3 Minutes</a></p>
</li>
</ul>
</div>
</div>
</div>
</slide>
</carousel>
</article>