-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queries.txt
198 lines (152 loc) · 4.91 KB
/
Queries.txt
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
Noe4j Queries
========================================================================
//load answers file
LOAD CSV WITH HEADERS FROM 'file:///Questions.csv' AS line
CREATE (:Questions { questionID: line.Id, qUserId: line.OwnerUserId,qCreationDate:line.CreationDate,qClosedDate:line.ClosedDate,qScore: line.Score, questTitle: line.Title,qBody:line.Body })
//load answers file
LOAD CSV WITH HEADERS FROM 'file:///Answers.csv' AS line
CREATE (:Answers { answerId: line.Id, answerUserId: line.OwnerUserId, CreationDate: line.CreationDate, ParentQuestionId: line.ParentId, AnswerScore: line.Score, AnswerBody: line.Body })
//Load tags csv
LOAD CSV WITH HEADERS FROM 'file:///TagsNew.csv' AS line
CREATE (:Tags { tagID:line.tagID, tagName: line.tagName})
//Relation between question and tags
LOAD CSV WITH HEADERS FROM 'file:///TagsQuestionsRelation.csv' AS edges
MATCH (a:Question{questionID: edges.QuestionID})
MATCH (b:Tags{tagID: edges.tagID})
CREATE (a)-[:HAS_TAG]->(b);
===========================================================================================
Usecases:
===========================================================================================
1. Top 5 tags of all time
MATCH (t:Tags)<-[r:HAS_TAG]-(q:Questions)
WITH t
RETURN t.tagName,count(*) AS count
ORDER BY count DESC LIMIT 5;
=========================================================================
2. The Top Answerers for the javascript Tag
MATCH (:Tags {tagName:'javascript'})<-[:HAS_TAG]-()-[:HAS_ANSWER]->(a:Answers)
RETURN a.answerUserId,count(*) as count, collect(a.ParentQuestionId) AS QuestionIDs
ORDER BY count DESC LIMIT 10;
=========================================================================
3. All Answers to each Java Questions According to Question Score
MATCH (:Tags {tagName:'java'})<-[:HAS_TAG]-(q:Questions)-[:HAS_ANSWER]->(a:Answers)
RETURN q.questionID AS QID ,q.questTitle AS QTitle,q.qScore AS qScore,count(*) AS TotalAnwers, collect(a.AnswerBody) AS Answers
ORDER BY q.qScore DESC;
=============================================================================
4. Where Else Were the Top Answerers of Java Also Active?
MATCH (c:Tags {tagName:"java"})<-[:HAS_TAG]-()-[:HAS_ANSWER]->(a:Answers)
WITH c,a,count(*) as freq
Order BY freq desc limit 10
MATCH (a)<-[:HAS_ANSWER]->(q:Questions)-[:HAS_TAG]->(other:Tags)
WITH a,other,count(*) AS freq2
ORDER BY freq2 DESC
RETURN a.answerUserId, collect(other.Tag)[1..5] as list
===================================================================================
5. People Who Posted the Most Questions about javascript
MATCH (t:Tags {tagName:'javascript'})<-[:HAS_TAG]-(q:Questions)
RETURN q.qUserId, count(*) as count
ORDER BY count DESC;
================================================================================
Mongo Queries
==================================================================================
Database Load Operations
==================================================================================
Aggregation to combine QA and Answer
db.QA.aggregate([
{"$limit":2},
{$lookup:
{
from: "Tags",
localField: "Id",
foreignField: "Id",
as: "tags"
}}
]).pretty();
=====================================================================================
Use cases
=====================================================================================
1. Top 5 tags of all time
db.QATags.aggregate([
{
$unwind: "$tags"
},
{
$group:
{
_id:"$tags.Tag",
total:{ $sum:1 }
}
},
{
$sort: {total: -1}
},
{
"$limit": 5
}
])
=====================================================================================================
2) The Top Answerers for the javascript Tag
db.QATags.aggregate([
{
$unwind: "$ans"
},
{
"$match": {"tags.Tag":"javascript" }
},
{
$group:
{
_id:"$ans.OwnerUserId",
total:{ $sum:1 }
}
},
{
$sort: {total: -1}
},
{
"$limit": 10
}
])
=======================================================================================
3. All Answers to each Java Questions According to Question Score
db.QATags.aggregate([
{
$unwind: "$ans"
},
{
"$match": {"tags.Tag":"java" }
},
{
$group:
{
_id:"$ans.questionID",
total:{ $sum:1 }
}
},
{
"$limit": 10
}
])
===================================================================================
4. People Who Posted the Most Questions about javascript
db.QATags.aggregate([
{
$unwind: "$ans"
},
{
"$match": {"tags.Tag":"javascript" }
},
{
$group:
{
_id:"$ans.questionID",
total:{ $sum:1 }
}
},
{
$sort: {total: -1}
},
{
"$limit": 10
}
])