-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.lua
554 lines (458 loc) · 18 KB
/
queries.lua
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
-- Sample IS 1
local properties = NodeGetProperties("Person", "933")
local city = NodeGetNeighbors("Person", "933", Direction.OUT, "IS_LOCATED_IN")[1]
local result = {
["person.firstName"] = properties["firstName"],
["person.lastName"] = properties["lastName"],
["person.birthday"] = properties["birthday"],
["person.locationIP"] = properties["locationIP"],
["person.browserUsed"] = properties["browserUsed"],
["city.id"] = city:getProperty("id"),
["person.gender"] = properties["gender"],
["person.creationDate"] = date(properties["creationDate"]):fmt("${iso}Z")
}
result
-- Sample IS 2
local person = NodeGet("Person", "21990232564424")
local messages = NodeGetNeighbors(person:getId(), Direction.IN, "HAS_CREATOR")
table.sort(messages, function(a, b)
if a:getProperty("creationDate") > b:getProperty("creationDate") then
return true
elseif a:getProperty("creationDate") == b:getProperty("creationDate") then
return a:getProperty("id") > b:getProperty("id")
end
end)
local smaller = table.move(messages, 1, 10, 1, {})
results = {}
for i, message in pairs(smaller) do
local properties = message:getProperties()
local result = {
["message.id"] = properties["id"],
["message.creationDate"] = date(properties["creationDate"]):fmt("${iso}Z")
}
if (properties["content"] == '') then
result["message.imageFile"] = properties["imageFile"]
else
result["message.content"] = properties["content"]
end
if (properties["type"] == "post") then
result["post.id"] = properties["id"]
result["originalPoster.id"] = person:getProperty("id")
result["originalPoster.firstName"] = person:getProperty("firstName")
result["originalPoster.lastName"] = person:getProperty("lastName")
else
local node_id = message:getId()
local hasReply = NodeGetLinks(node_id, Direction.OUT, "REPLY_OF")
while (#hasReply > 0) do
node_id = hasReply[1]:getNodeId()
hasReply = NodeGetLinks(node_id, Direction.OUT, "REPLY_OF")
end
local poster = NodeGetNeighbors(node_id, Direction.OUT, "HAS_CREATOR")[1]
local post_id = NodeGetProperty(node_id, "id")
result["post.id"] = post_id
result["originalPoster.id"] = poster:getProperty("id")
result["originalPoster.firstName"] = poster:getProperty("firstName")
result["originalPoster.lastName"] = poster:getProperty("lastName")
end
table.insert(results, result)
end
results
-- Sample IS 3
local knows = NodeGetLinks("Person", "17592186055119", "KNOWS")
local friendships = {}
for i, know in pairs(knows) do
creation = RelationshipGetProperty(know:getRelationshipId(),"creationDate")
friend = NodeGetProperties(know:getNodeId())
friendship = {
["friend.id"] = friend["id"],
["friend.firstName"] = friend["firstName"],
["friend.lastName"] = friend["lastName"],
["knows.creationDate"] = creation
}
table.insert(friendships, friendship)
end
table.sort(friendships, function(a, b)
if a["knows.creationDate"] > b["knows.creationDate"] then
return true
end
if (a["knows.creationDate"] == b["knows.creationDate"]) then
return (a["friend.id"] < b["friend.id"] )
end
end)
for i = 1, #friendships do
friendships[i]["knows.creationDate"] = date(friendships[i]["knows.creationDate"]):fmt("${iso}Z")
end
friendships
-- Sample IS 4 - (content)
local properties = NodeGetProperties("Message", "4947802324992")
local result = {
["message.creationDate"] = date(properties["creationDate"]):fmt("${iso}Z")
}
if (properties["content"] == '') then
result["message.imageFile"] = properties["imageFile"]
else
result["message.content"] = properties["content"]
end
result
-- Sample IS 4 - (image)
local properties = NodeGetProperties("Message", "1649267441795")
local result = {
["message.creationDate"] = date(properties["creationDate"]):fmt("${iso}Z")
}
if (properties["content"] == '') then
result["message.imageFile"] = properties["imageFile"]
else
result["message.content"] = properties["content"]
end
result
-- Sample IS 5
local person = NodeGetNeighbors("Message", "4947802324992", Direction.OUT, "HAS_CREATOR")[1]
local result = {
["person.id"] = person:getProperty("id"),
["person.firstName"] = person:getProperty("firstName"),
["person.lastName"] = person:getProperty("lastName")
}
result
-- Sample IS 6
local message_id = "8246337208331"
local node_id = NodeGetId("Message", message_id)
local links = NodeGetLinks(node_id, Direction.IN, "CONTAINER_OF")
while (#links == 0) do
links = NodeGetLinks(node_id, Direction.OUT, "REPLY_OF")
node_id = links[1]:getNodeId()
links = NodeGetLinks(node_id , Direction.IN, "CONTAINER_OF")
end
node_id = links[1]:getNodeId()
local forum = NodeGet(node_id)
local moderator = NodeGetNeighbors(node_id, Direction.OUT, "HAS_MODERATOR")[1]
local properties = moderator:getProperties()
local result = {
["forum.id"] = forum:getProperty("id"),
["forum.title"] = forum:getProperty("title"),
["moderator.id"] = properties["id"],
["moderator.firstName"] = properties["firstName"],
["moderator.lastName"] = properties["lastName"]
}
result
-- Sample IS 7
local message_id = "8246337208329"
local message_node_id = NodeGetId("Message", message_id)
local author = NodeGetNeighbors(message_node_id, Direction.OUT, "HAS_CREATOR")[1]
local knows = NodeGetLinks(author:getId(), "KNOWS")
local knows_ids = {}
for i, know in pairs (knows) do
table.insert(knows_ids, know:getNodeId())
end
local comments = {}
local replies = NodeGetNeighbors(message_node_id, Direction.IN, "REPLY_OF")
for i, reply in pairs (replies) do
local replyAuthor = NodeGetNeighbors(reply:getId(), Direction.OUT, "HAS_CREATOR")[1]
local properties = replyAuthor:getProperties()
local comment = {
["replyAuthor.id"] = properties["id"],
["replyAuthor.firstName"] = properties["firstName"],
["replyAuthor.lastName"] = properties["lastName"],
["knows"] = not knows_ids[replyAuthor:getId()] == nil,
["comment.id"] = reply:getProperty("id"),
["comment.content"] = reply:getProperty("content"),
["comment.creationDate"] = reply:getProperties()["creationDate"]
}
table.insert(comments, comment)
end
table.sort(comments, function(a, b)
if a["comment.creationDate"] > b["comment.creationDate"] then
return true
end
if (a["comment.creationDate"] == b["comment.creationDate"]) then
return (a["replyAuthor.id"] < b["replyAuthor.id"] )
end
end)
for i = 1, #comments do
comments[i]["comment.creationDate"] = date(comments[i]["comment.creationDate"]):fmt("${iso}Z")
end
comments
-- Add a Filter method to just see if the node ids found have the first name equal to Chen
-- instead of looking at all of them
-- Sample IC 1 - Variation find fist
-- Given a start *Person*, find *Persons* with a given first name (`firstName`) that the
-- start *Person* is connected to (excluding start *Person*) by at most 3 steps via the *knows* relationships.
-- Return *Persons*, including the distance (1..3), summaries of the *Persons* workplaces and places of study.
local node_ids = Roar.new()
local targets = FindNodeIds("Person", "firstName", Operation.EQ, "Chen", 0, 999999)
node_ids:addIds(targets)
local node_id = NodeGetId("Person", "1129")
local people = NodeGetLinks(node_id, "KNOWS")
local seen1 = Roar.new()
local seen2 = Roar.new()
local seen3 = Roar.new()
seen1:addNodeIds(people)
local people2 = LinksGetLinks(people, "KNOWS")
for i,links in pairs(people2) do
seen2:addNodeIds(links)
end
seen2:inplace_difference(seen1)
seen2:remove(node_id)
if(seen2:intersection(node_ids):cardinality() < 20) then
local people3 = LinksGetLinks(seen2:getNodeHalfLinks(), "KNOWS")
for i,links2 in pairs(people3) do
seen3:addNodeIds(links2)
end
seen3:inplace_difference(seen2)
seen3:inplace_difference(seen1)
seen3:remove(node_id)
end
seen1:inplace_intersection(node_ids)
seen2:inplace_intersection(node_ids)
seen3:inplace_intersection(node_ids)
local known = {}
local found = {seen1, seen2, seen3}
for i = 1, #found do
if (found[i]:cardinality() > 0) then
local lastNames = NodesGetProperty(found[i]:getIds(), "lastName")
local ids = NodesGetProperty(found[i]:getIds(), "id")
for j = 1, found[i]:cardinality() do
otherPerson = {
["otherPerson.id"] = ids[j],
["otherPerson.lastName"] = lastNames[j],
["distanceFromPerson"] = i
}
table.insert(known, otherPerson)
end
end
end
function sort_on_values(t,...)
local a = {...}
table.sort(t, function (u,v)
for i = 1, #a do
if u[a[i]] > v[a[i]] then return false end
if u[a[i]] < v[a[i]] then return true end
end
end)
end
sort_on_values(known,"distanceFromPerson","otherPerson.lastName", "otherPerson.id")
local smaller = table.move(known, 1, 20, 1, {})
local results = {}
for j, person in pairs(smaller) do
local studied_list = {}
local worked_list = {}
local studied = NodeGetRelationships("Person", tostring(person["otherPerson.id"]), Direction.OUT, "STUDY_AT" )
local worked = NodeGetRelationships("Person", tostring(person["otherPerson.id"]), Direction.OUT, "WORK_AT" )
for s = 1, #studied do
table.insert(studied_list, NodeGetProperty(studied[s]:getEndingNodeId(), "name"))
table.insert(studied_list, RelationshipGetProperty(studied[s]:getId(), "classYear"))
end
for s = 1, #worked do
table.insert(worked_list, NodeGetProperty(worked[s]:getEndingNodeId(), "name"))
table.insert(worked_list, RelationshipGetProperty(worked[s]:getId(), "workFrom"))
end
local properties = NodeGetProperties("Person", tostring(person["otherPerson.id"]) )
otherPerson = {
["otherPerson.id"] = person["otherPerson.id"],
["otherPerson.lastName"] = properties["lastName"],
["distanceFromPerson"] = person["distanceFromPerson"],
["otherPerson.birthday"] = properties["birthday"],
["otherPerson.creationDate"] = date(properties["creationDate"]):fmt("${iso}Z"),
["otherPerson.gender"] = properties["gender"],
["otherPerson.browserUsed"] = properties["browserUsed"],
["otherPerson.locationIP"] = properties["locationIP"],
["otherPerson.email"] = properties["email"],
["otherPerson.speaks"] = properties["speaks"],
["universities"] = table.concat(studied_list, ", "),
["companies"] = table.concat(worked_list, ", ")
}
table.insert(results, otherPerson)
end
results
-- Sample IC 1 - Variation filter as we go
-- Given a start *Person*, find *Persons* with a given first name (`firstName`) that the
-- start *Person* is connected to (excluding start *Person*) by at most 3 steps via the *knows* relationships.
-- Return *Persons*, including the distance (1..3), summaries of the *Persons* workplaces and places of study.
local node_id = NodeGetId("Person", "1129")
local people = NodeGetLinks(node_id, "KNOWS")
local seen1 = Roar.new()
seen1:addNodeIds(people)
local named1 = FilterNodes(seen1:getIds(), "Person", "firstName", Operation.EQ, "Chen")
local named2 = {}
local named3 = {}
if(#named1 < 20) then
local seen2 = Roar.new()
local people2 = LinksGetLinks(people, "KNOWS")
for i,links in pairs(people2) do
seen2:addNodeIds(links)
end
seen2:inplace_difference(seen1)
seen2:remove(node_id)
named2 = FilterNodes(seen2:getIds(), "Person", "firstName", Operation.EQ, "Chen")
if((#named1 + #named2) < 20) then
local seen3 = Roar.new()
local people3 = LinksGetLinks(seen2:getNodeHalfLinks(), "KNOWS")
for i,links2 in pairs(people3) do
seen3:addNodeIds(links2)
end
seen3:inplace_difference(seen2)
seen3:inplace_difference(seen1)
seen3:remove(node_id)
named3 = FilterNodes(seen3:getIds(), "Person", "firstName", Operation.EQ, "Chen")
end
end
local known = {}
local found = {named1, named2, named3}
for i = 1, #found do
if (#found[i] > 0) then
for j, person in pairs(found[i]) do
local properties = person:getProperties()
otherPerson = {
["otherPerson.id"] = properties["id"],
["otherPerson.lastName"] = properties["lastName"],
["otherPerson.birthday"] = properties["birthday"],
["otherPerson.creationDate"] = properties["creationDate"],
["otherPerson.gender"] = properties["gender"],
["otherPerson.browserUsed"] = properties["browserUsed"],
["otherPerson.locationIP"] = properties["locationIP"],
["otherPerson.email"] = properties["email"],
["otherPerson.speaks"] = properties["speaks"],
["distanceFromPerson"] = i
}
table.insert(known, otherPerson)
end
end
end
function sort_on_values(t,...)
local a = {...}
table.sort(t, function (u,v)
for i = 1, #a do
if u[a[i]] > v[a[i]] then return false end
if u[a[i]] < v[a[i]] then return true end
end
end)
end
sort_on_values(known,"distanceFromPerson","otherPerson.lastName", "otherPerson.id")
local smaller = table.move(known, 1, 20, 1, {})
local results = {}
for j, person in pairs(smaller) do
local studied_list = {}
local worked_list = {}
local studied = NodeGetRelationships("Person", tostring(person["otherPerson.id"]), Direction.OUT, "STUDY_AT" )
local worked = NodeGetRelationships("Person", tostring(person["otherPerson.id"]), Direction.OUT, "WORK_AT" )
for s = 1, #studied do
table.insert(studied_list, NodeGetProperty(studied[s]:getEndingNodeId(), "name"))
table.insert(studied_list, RelationshipGetProperty(studied[s]:getId(), "classYear"))
end
for s = 1, #worked do
table.insert(worked_list, NodeGetProperty(worked[s]:getEndingNodeId(), "name"))
table.insert(worked_list, RelationshipGetProperty(worked[s]:getId(), "workFrom"))
end
person["universities"] = table.concat(studied_list, ", ")
person["companies"] = table.concat(worked_list, ", ")
person["otherPerson.creationDate"] = date(person["otherPerson.creationDate"]):fmt("${iso}Z")
table.insert(results, person)
end
results
-- Sample IC 1 - Variation bi-directional
-- Given a start *Person*, find *Persons* with a given first name (`firstName`) that the
-- start *Person* is connected to (excluding start *Person*) by at most 3 steps via the *knows* relationships.
-- Return *Persons*, including the distance (1..3), summaries of the *Persons* workplaces and places of study.
local target_ids = Roar.new()
local targets = FindNodeIds("Person", "firstName", Operation.EQ, "Carmen", 0, 999999)
target_ids:addIds(targets)
table.sort(targets)
local target_knows = LinksGetLinks(target_ids:getNodeHalfLinks(), "KNOWS")
local target_knows_ids = Roar.new()
local backwards = {}
for i,links in pairs(target_knows) do
target_knows_ids:addNodeIds(links)
local neighbors = {}
for j, neighbor in pairs(links) do
table.insert(neighbors, neighbor:getNodeId())
end
table.insert(backwards, neighbors)
end
local forwards = {}
for i, id in pairs(target_knows_ids:getIds()) do
forwards[tostring(id)] = {}
end
for i, ids in pairs(backwards) do
for j, neigh in pairs(ids) do
table.insert(forwards[tostring(neigh)], targets[i])
end
end
local node_id = NodeGetId("Person", "30786325583618")
local people = NodeGetLinks(node_id, "KNOWS")
local seen1 = Roar.new()
local seen2 = Roar.new()
local near2 = Roar.new()
local seen3 = Roar.new()
seen1:addNodeIds(people)
local people2 = LinksGetLinks(people, "KNOWS")
for i,links in pairs(people2) do
seen2:addNodeIds(links)
end
seen2:inplace_difference(seen1)
seen2:remove(node_id)
near2:inplace_union(seen2)
seen1:inplace_intersection(target_ids)
seen2:inplace_intersection(target_ids)
near2:inplace_intersection(target_knows_ids)
for i, id in pairs(near2:getIds()) do
seen3:addIds(forwards[tostring(id)])
end
local known = {}
local found = {seen1, seen2, seen3}
for i = 1, #found do
if (found[i]:cardinality() > 0) then
local seen_ids = found[i]:getIds()
local lastNames = NodesGetProperty(seen_ids, "lastName")
local ids = NodesGetProperty(seen_ids, "id")
for j = 1, found[i]:cardinality() do
otherPerson = {
["otherPerson.id"] = ids[j],
["otherPerson.lastName"] = lastNames[j],
["distanceFromPerson"] = i
}
table.insert(known, otherPerson)
end
end
end
function sort_on_values(t,...)
local a = {...}
table.sort(t, function (u,v)
for i = 1, #a do
if u[a[i]] > v[a[i]] then return false end
if u[a[i]] < v[a[i]] then return true end
end
end)
end
sort_on_values(known,"distanceFromPerson","otherPerson.lastName", "otherPerson.id")
local smaller = table.move(known, 1, 20, 1, {})
local results = {}
for j, person in pairs(smaller) do
local studied_list = {}
local worked_list = {}
local studied = NodeGetRelationships("Person", tostring(person["otherPerson.id"]), Direction.OUT, "STUDY_AT" )
local worked = NodeGetRelationships("Person", tostring(person["otherPerson.id"]), Direction.OUT, "WORK_AT" )
for s = 1, #studied do
table.insert(studied_list, NodeGetProperty(studied[s]:getEndingNodeId(), "name"))
table.insert(studied_list, RelationshipGetProperty(studied[s]:getId(), "classYear"))
end
for s = 1, #worked do
table.insert(worked_list, NodeGetProperty(worked[s]:getEndingNodeId(), "name"))
table.insert(worked_list, RelationshipGetProperty(worked[s]:getId(), "workFrom"))
end
local properties = NodeGetProperties("Person", tostring(person["otherPerson.id"]) )
otherPerson = {
["otherPerson.id"] = person["otherPerson.id"],
["otherPerson.lastName"] = properties["lastName"],
["distanceFromPerson"] = person["distanceFromPerson"],
["otherPerson.birthday"] = properties["birthday"],
["otherPerson.creationDate"] = date(properties["creationDate"]):fmt("${iso}Z"),
["otherPerson.gender"] = properties["gender"],
["otherPerson.browserUsed"] = properties["browserUsed"],
["otherPerson.locationIP"] = properties["locationIP"],
["otherPerson.email"] = properties["email"],
["otherPerson.speaks"] = properties["speaks"],
["universities"] = table.concat(studied_list, ", "),
["companies"] = table.concat(worked_list, ", ")
}
table.insert(results, otherPerson)
end
results