Search code examples
memgraphdb

Transforming SQL query for product recommendation to Cypher query


I have a recommendation system that uses SQL query similar to this one:

select B.* from user User1 
join rating Rating1 on User1.user_id = Rating1.id and Rating1.value = 5 
join product A on A.id = Rating1.product_id 
join rating Rating2 on Rating2.product_id = A.id and Rating2.value = 5 
join user User2 on User2.id = Rating2.user_id and User2.id <> User1.id 
join rating RatingB on RatingB.user_id = User2.id and RatingB.value =5 
join product B on B.id = RatingB.product_id 
WHERE User1.id = 1;

This system recommends a product to a user if they didn’t buy that product yet. The recommendation is based on the rating of the product that they gave the best rating and it looks which other users also rated that product with the same rating.

How would this query look like in Cypher?


Solution

  • The mentioned SQL query would look something like this if it was written using Cypher:

    MATCH (pA:PRODUCT)<-[r1:Rated {"rating":5}]-(n1:USER)-[r2:Rated {"rating":5}]->(pB:PRODUCT)
    MATCH (n2:USER {id:1})-[r3:Rated {"rating":5}]->(pb)
    WHERE n1.id != n2.id
    RETURN pB;