Skip to content

Commit

Permalink
Rewrite ic 10/14 complex query
Browse files Browse the repository at this point in the history
  • Loading branch information
yixinglu committed Nov 24, 2022
1 parent 8503c01 commit 00acf3f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
10 changes: 5 additions & 5 deletions nebula/queries/interactive-complex-10.ngql
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
MATCH (person:Person)-[:KNOWS*2]-(friend)-[:IS_LOCATED_IN]->(city:Place)
WHERE id(person) == $personId AND id(friend) != $personId AND NOT (friend)-[:KNOWS]-(person)
WITH person, city, friend, datetime({friend.Person.birthday}) as birthday
WITH person, city, friend, datetime(friend.Person.birthday) as birthday
WHERE (birthday.month==$month AND birthday.day>=21) OR
(birthday.month==($month%12)+1 AND birthday.day<22)
WITH DISTINCT friend, city, person
OPTIONAL MATCH (friend)<-[:POST_HAS_CREATOR]-(post:Post)
WITH friend, city, collect(post) AS posts, person
OPTIONAL MATCH (person)-[:HAS_INTEREST]->()<-[:HAS_TAG]-(post)
WITH friend,
city,
size(posts) AS postCount,
size([p IN posts WHERE (p)-[:HAS_TAG]->()<-[:HAS_INTEREST]-(person)]) AS commonPostCount
count(*) AS postCount,
count(post) AS commonPostCount
RETURN toInteger(substr(id(friend), 2)) AS personId,
friend.Person.firstName AS personFirstName,
friend.Person.lastName AS personLastName,
commonPostCount - (postCount - commonPostCount) AS commonInterestScore,
friend.Person.gender AS personGender,
city.Place.name AS personCityName
ORDER BY commonInterestScore DESC, personId ASC
LIMIT 10
LIMIT 10
15 changes: 12 additions & 3 deletions nebula/queries/interactive-complex-14.ngql
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
MATCH p = allShortestPaths((person1)-[:KNOWS*..15]-(person2))
WHERE id(person1) == $person1 and id(person2) == $person2
WITH nodes(p) AS pathNodes
RETURN
WITH
[n IN pathNodes | id(n)] AS personIdsInPath,
reduce(weight=0.0, idx IN range(1,size(pathNodes)-1) | [prev IN [pathNodes[idx-1]] | [curr IN [pathNodes[idx]] | weight + size((curr)<-[:COMMENT_HAS_CREATOR]-(:`Comment`)-[:REPLY_OF_POST]->(:Post)-[:POST_HAS_CREATOR]->(prev))*1.0 + size((prev)<-[:COMMENT_HAS_CREATOR]-(:`Comment`)-[:REPLY_OF_POST]->(:Post)-[:POST_HAS_CREATOR]->(curr))*1.0 + size((prev)-[:COMMENT_HAS_CREATOR]-(:`Comment`)-[:REPLY_OF_COMMENT]-(:`Comment`)-[:COMMENT_HAS_CREATOR]-(curr))*0.5] ][0][0]) AS pathWight
ORDER BY pathWight DESC
[idx IN range(1, size(pathNodes)-1) | [prev IN [pathNodes[idx-1]] | [curr IN [pathNodes[idx]] | [prev, curr]]]] AS vertList
UNWIND vertList AS c
WITH c[0][0][0] AS prev, c[0][0][1] AS curr, personIdsInPath
MATCH (curr)<-[:COMMENT_HAS_CREATOR]-(:`Comment`)-[:REPLY_OF_POST]->(:Post)-[:POST_HAS_CREATOR]->(prev)
WITH count(*) AS cnt1, prev, curr, personIdsInPath
MATCH (prev)<-[:COMMENT_HAS_CREATOR]-(:`Comment`)-[:REPLY_OF_POST]->(:Post)-[:POST_HAS_CREATOR]->(curr)
WITH count(*) AS cnt2, cnt1, prev, curr, personIdsInPath
MATCH (prev)-[:COMMENT_HAS_CREATOR]-(:`Comment`)-[:REPLY_OF_COMMENT]-(:`Comment`)-[:COMMENT_HAS_CREATOR]-(curr)
WITH count(*) AS cnt3, cnt1, cnt2, personIdsInPath
RETURN personIdsInPath, sum(cnt1 + cnt2 + 0.5 * cnt3) AS pathWeight
ORDER BY pathWeight DESC

0 comments on commit 00acf3f

Please sign in to comment.