Search code examples
databaseneo4jrecommendation-engine

Show all the books in database except the ones this specific user has read neo4j


I have a simple database in Neo4j with information about users, their friends and the books they read. I need to write a query that shows all the books that were read by a specific user's friends (sort of a recommendation for the current user). Let's say I want to see the books that were read by the friends of user with name "Sophia". How can I do this in neo4j?

my database schema


Solution

  • You should be able to get those books by running the query below:

    MATCH (sophia: User {name: "Sophia"})-[:FRIENDS_WITH]->(friends: User)-[:READ]->(book: Book)
    RETURN book
    

    If you want to only include books that haven't been read by Sophia, the query below should work:

    MATCH (sophia: User {name: "Sophia"})-[:FRIENDS_WITH]->(friends: User)-[:READ]->(book: Book)
    WHERE NOT EXISTS ((sophia)-[:READ]->(book))
    RETURN book