I am currently trying to make recommendations based on collaborative Filtering. I have Book, User, and Author nodes in my database. The users are connected to the books with RATED and ADDED TO LIST. The books and authors with WRITTEN_BY.
// k-nearest neighbor
MATCH (u1:User {user_id: 11927})-[r:RATED]->(b:Book)
WITH u1,r.rating AS rating_u1
MATCH (u1)-[r1:RATED]->(b:Book)<-[r2:RATED]-(u2)
WITH u1, u2, COLLECT({r1: r1, r2: r2}) AS ratings WHERE size(ratings) > 5
MATCH (u2)-[r:RATED]->(b:Book)
WITH u1, u2, r.rating AS rating_u2, ratings
UNWIND ratings AS r
WITH sqrt(sum((r.r1.rating - r.r2.rating)^2)) AS euclidean_distance,
u1, u2 WHERE euclidean_distance <> 0
WITH u1, u2, 1 / (1 + euclidean_distance) AS similarity
ORDER BY similarity DESC LIMIT 10
MATCH (u2)-[r:RATED]->(b:Book)
WHERE NOT EXISTS((u1)-[:RATED]->(b))
RETURN b.title, SUM(similarity * r.rating) AS score
ORDER BY score DESC LIMIT 25
With this Cypher query I want to compare each pair of users who have rated at least 5 common books. Then, I want to compare the 10 most similar users to the user with user_id 11927. But there must be something wrong with my code; it is not doing what it should. Have you an idea how I could solve this? (I want to do it without graph data science, playground, or AuraDS).