Search code examples
neo4jcypherrecommendation-engine

Neo4J Cypher recomendation app


My question is not that complicated, but i can't find an answer.

The picture below shows the result i have so far. The blue circle is the node that was my starting point. What i want to find is the nodes in the red circle minus the blue one! (the node(s) that has the most nodes in common with the starting node)

enter image description here

It's an app that recommends pieces of art based on what you like.. Based on colors, type, artist ect.

Here is my code for the result in the picture:

START Art=node(8986)
MATCH P=(Art)-[r:HAS_COLOR]->(Color)<-[:HAS_COLOR]-(Art1)
RETURN P, count(DISTINCT P) as Rel
ORDER BY Rel DESC

Hope it is understandable and that someone can help me.


Solution

  • If I get it right, you want to MATCH all other pieces of art that share colors (at least one) with you starting point. For recommendation you want to sort them by the number of shared colors.

    This query should do what you need:

    // start as you did before
    START art1=node(8986)
    // MATCH art1 to other art pieces via similar_color
    MATCH (art1)-[:HAS_COLOR]->(similar_color:Color)<-[:HAS_COLOR]-(art2) 
    // only if art1 is not the same as art2
    WHERE art1 <> art2
    // return art2 and the number of similar colors
    RETURN DISTINCT art2, count(DISTINCT similar_color) AS num_sc
    // order by simiar colors
    ORDER BY num_sc DESC
    

    A few notes:

    • the id of a node can change (e.g. after deleting a node the id will be reused), you might want to consider replacing your START with a MATCH
    • I would always use labels for matching nodes (i.e. art2:Artpiece)
    • convention for Cypher queries is to use initial caps for Labels and preferrably not for variables (http://nigelsmall.com/zen)