Hello everyone!
I am currently trying to make recommendations based on collaborative Filtering. I have books users and authors in my database. The user 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 queries 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 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? (without graph data science playground or AuraDS. Thank you