Skip to content

Instantly share code, notes, and snippets.

@daviddyball
Last active December 23, 2021 21:26
Show Gist options
  • Save daviddyball/b3a2599cafb558b04d55 to your computer and use it in GitHub Desktop.
Save daviddyball/b3a2599cafb558b04d55 to your computer and use it in GitHub Desktop.
Song Recommendations using Neo4J and Collaborative Filtering
// Example Query
PROFILE
MATCH (user:User {id: 2})-[:LIKES]->(liked_song)<-[:LIKES]-(similar_user)-[:LIKES]->(recommendation)
WITH similar_user, COUNT(similar_user) as similar_user_rating, user
ORDER BY similar_user_rating
LIMIT 50
MATCH (similar_user)-[:LIKES]->(recommendation)
WHERE user<>similar_user AND
NOT (user)-[:LIKES]->(recommendation) AND
NOT (user)-[:PRODUCT_RECOMMENDED]->(recommendation)
RETURN recommendation.id as id, COUNT(*) AS weight
ORDER BY weight DESC
LIMIT 100
// Data-set for https://console.neo4j.com
create
(u0:User {id: 0}),
(u1:User {id: 1}),
(u2:User {id: 2}),
(u3:User {id: 3}),
(u4:User {id: 4}),
(u5:User {id: 5}),
(u6:User {id: 6}),
(u7:User {id: 7}),
(u8:User {id: 8}),
(u9:User {id: 9}),
(s0:Song {id: 0}),
(s1:Song {id: 1}),
(s2:Song {id: 2}),
(s3:Song {id: 3}),
(s4:Song {id: 4}),
(s5:Song {id: 5}),
(s6:Song {id: 6}),
(s7:Song {id: 7}),
(s8:Song {id: 8}),
(s9:Song {id: 9}),
u0-[:LIKES {created_at: 491488021}]->s8,
u0-[:LIKES {created_at: 1423248167}]->s8,
u0-[:LIKES {created_at: 230426401}]->s5,
u1-[:LIKES {created_at: 1023378895}]->s7,
u1-[:LIKES {created_at: 1188736528}]->s10,
u1-[:LIKES {created_at: 875605199}]->s10,
u2-[:LIKES {created_at: 91869615}]->s0,
u2-[:LIKES {created_at: 1347318318}]->s2,
u2-[:LIKES {created_at: 1028744723}]->s10,
u3-[:LIKES {created_at: 1048237455}]->s5,
u3-[:LIKES {created_at: 641428539}]->s1,
u3-[:LIKES {created_at: 1368841830}]->s8,
u4-[:LIKES {created_at: 655083475}]->s5,
u4-[:LIKES {created_at: 1414977733}]->s8,
u4-[:LIKES {created_at: 62139078}]->s3,
u5-[:LIKES {created_at: 1321727603}]->s0,
u5-[:LIKES {created_at: 385869403}]->s6,
u5-[:LIKES {created_at: 233896178}]->s6,
u6-[:LIKES {created_at: 222408805}]->s2,
u6-[:LIKES {created_at: 41358724}]->s6,
u6-[:LIKES {created_at: 289675253}]->s8,
u7-[:LIKES {created_at: 285045678}]->s0,
u7-[:LIKES {created_at: 1105139110}]->s6,
u7-[:LIKES {created_at: 783790218}]->s6,
u8-[:LIKES {created_at: 1202732915}]->s9,
u8-[:LIKES {created_at: 329004447}]->s10,
u8-[:LIKES {created_at: 687839756}]->s0,
u9-[:LIKES {created_at: 1377922364}]->s10,
u9-[:LIKES {created_at: 395079992}]->s0,
u9-[:LIKES {created_at: 1212609975}]->s6,
u0<-[:SONG_RECOMMENDED]-s9,
u0<-[:SONG_RECOMMENDED]-s4,
u0<-[:SONG_RECOMMENDED]-s9,
u1<-[:SONG_RECOMMENDED]-s3,
u1<-[:SONG_RECOMMENDED]-s5,
u1<-[:SONG_RECOMMENDED]-s1,
u2<-[:SONG_RECOMMENDED]-s2,
u2<-[:SONG_RECOMMENDED]-s1,
u2<-[:SONG_RECOMMENDED]-s1,
u3<-[:SONG_RECOMMENDED]-s1,
u3<-[:SONG_RECOMMENDED]-s1,
u3<-[:SONG_RECOMMENDED]-s6,
u4<-[:SONG_RECOMMENDED]-s8,
u4<-[:SONG_RECOMMENDED]-s3,
u4<-[:SONG_RECOMMENDED]-s8,
u5<-[:SONG_RECOMMENDED]-s7,
u5<-[:SONG_RECOMMENDED]-s10,
u5<-[:SONG_RECOMMENDED]-s0,
u6<-[:SONG_RECOMMENDED]-s7,
u6<-[:SONG_RECOMMENDED]-s4,
u6<-[:SONG_RECOMMENDED]-s2,
u7<-[:SONG_RECOMMENDED]-s1,
u7<-[:SONG_RECOMMENDED]-s3,
u7<-[:SONG_RECOMMENDED]-s8,
u8<-[:SONG_RECOMMENDED]-s7,
u8<-[:SONG_RECOMMENDED]-s9,
u8<-[:SONG_RECOMMENDED]-s7,
u9<-[:SONG_RECOMMENDED]-s6,
u9<-[:SONG_RECOMMENDED]-s2,
u9<-[:SONG_RECOMMENDED]-s3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment