-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathneo4j_query.txt
41 lines (32 loc) · 1.42 KB
/
neo4j_query.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
# 유저 2명의 영화평점 교집합
MATCH (p1:User {user_id:1})-[r1:Has_rated]->(m:Movie)<-[r2:Has_rated]-(p2:User {user_id:2})
RETURN m.title AS Movie, r1.rating AS `u1's Rating`, r2.rating AS `u2's Rating`
# 유저 2명의 코사인 유사도 Insert
MATCH (p1:User {user_id:1})-[x:Has_rated]->(m:Movie)<-[y:Has_rated]-(p2:User {user_id:2})
WITH SUM(x.rating * y.rating) AS xyDotProduct,
SQRT(REDUCE(xDot = 0.0, a IN COLLECT(x.rating) | xDot + a^2)) AS xLength,
SQRT(REDUCE(yDot = 0.0, b IN COLLECT(y.rating) | yDot + b^2)) AS yLength,
p1, p2
MERGE (p1)-[s:SIMILARITY]-(p2)
SET s.similarity = xyDotProduct / (xLength * yLength)
# 사용자 간 유사도 조회
MATCH (p1:User {user_id:1})-[s:SIMILARITY]-(p2:User {user_id:2})
RETURN s.similarity AS 'Cosine Similarity'
# User1's KNN(5)
MATCH (p1:User {user_id: 1})-[s:SIMILARITY]-(p2:User)
WITH p2, s.similarity AS sim
ORDER BY sim DESC
LIMIT 5
RETURN p2.user_id AS Neighbor, sim AS Similarity
# SIMILARITY 관계 삭제
MATCH ()-[r:SIMILARITY]->()
DELETE r
# User1이 안본영화 추천
MATCH (b:User)-[r:Has_rated]->(m:Movie), (b)-[s:SIMILARITY]-(a:User {user_id:1})
WHERE NOT((a)-[:Has_rated]->(m))
WITH m, s.similarity AS similarity, r.rating AS rating
ORDER BY m.title, similarity DESC
WITH m.title AS movie, COLLECT(rating)[0..3] AS ratings
WITH movie, REDUCE(s = 0, i IN ratings | s + i)*1.0 / LENGTH(ratings) AS reco
ORDER BY reco DESC
RETURN movie AS Movie, reco AS Recommendation