Skip to content

Instantly share code, notes, and snippets.

@jexp
Last active November 7, 2025 13:16
Show Gist options
  • Save jexp/2014efa6448b307c65e9 to your computer and use it in GitHub Desktop.
Save jexp/2014efa6448b307c65e9 to your computer and use it in GitHub Desktop.

Revisions

  1. jexp revised this gist Jan 6, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions restaurant_recommendation.adoc
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    == Restaurant Recommendations
    = Restaurant Recommendations
    :author: Neo Technology
    :twitter: neo4j
    :tags: Recommendation, Graph Based Search
    @@ -67,7 +67,7 @@ create (sushi:Cuisine {name:"Sushi"}), (nyc:City {name:"New York"}),

    //graph

    === Philips Friends
    == Philips Friends

    [source,cypher]
    ----
    @@ -77,7 +77,7 @@ RETURn person.name

    //table

    === Restaurants in NYC and their cusines
    == Restaurants in NYC and their cusines

    [source,cypher]
    ----
    @@ -89,7 +89,7 @@ RETURN nyc, restaurant, cusine

    //graph_result

    === Graph Search Recommendation
    == Graph Search Recommendation

    We want to answer the following question

  2. jexp created this gist Feb 25, 2015.
    120 changes: 120 additions & 0 deletions restaurant_recommendation.adoc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,120 @@
    == Restaurant Recommendations
    :author: Neo Technology
    :twitter: neo4j
    :tags: Recommendation, Graph Based Search
    :neo4j-version: 2.1

    We want to demonstrate how easy it is to model a domain as a graph and answer questions in almost natural language.

    Graph Based Search and Discovery is prominent a use-case for graph databases like http://neo4j.com[Neo4j].

    Here we use a Domain of restaurants which serve cuisines and are located in a City.

    image::https://dl.dropboxusercontent.com/u/14493611/sushi_restaurants_nyc.svg[]

    The domain diagram was created with the http://www.apcjones.com/arrows/#[Arrows tool]

    ////
    <ul class="graph-diagram-markup" data-internal-scale="0.1" data-external-scale="1">
    <li class="node" data-node-id="0" data-x="-60.0323224067688" data-y="-100.05387306213379">
    <span class="caption">Restaurant</span>
    </li>
    <li class="node" data-node-id="1" data-x="-1610.867395401001" data-y="-1240.6680226325989">
    <span class="caption">City</span>
    </li>
    <li class="node" data-node-id="2" data-x="1300.7003486156464" data-y="-1020.5495309829712">
    <span class="caption">Cusine</span>
    </li>
    <li class="node" data-node-id="3" data-x="-1240.6680583953857" data-y="1130.608777999878">
    <span class="caption">Person</span>
    </li>
    <li class="node" data-node-id="4" data-x="1130.6088542938232" data-y="1130.608777999878">
    <span class="caption">Person</span>
    </li>
    <li class="relationship" data-from="0" data-to="2">
    <span class="type">SERVES</span>
    </li>
    <li class="relationship" data-from="0" data-to="1">
    <span class="type">IS_LOCATED_IN</span>
    </li>
    <li class="relationship" data-from="3" data-to="0">
    <span class="type">LIKES</span>
    </li>
    <li class="relationship" data-from="4" data-to="0">
    <span class="type">LIKES</span>
    </li>
    <li class="relationship" data-from="3" data-to="4">
    <span class="type">IS_FRIEND_OF</span>
    </li>
    </ul>
    ////

    == Setup: Creating Friends, Restaurants in Cities and their Cusines

    //setup
    [source,cypher]
    ----
    CREATE (philip:Person {name:"Philip"})-[:IS_FRIEND_OF]->(emil:Person {name:"Emil"}),
    (philip)-[:IS_FRIEND_OF]->(michael:Person {name:"Michael"}),
    (philip)-[:IS_FRIEND_OF]->(andreas:Person {name:"Andreas"})
    create (sushi:Cuisine {name:"Sushi"}), (nyc:City {name:"New York"}),
    (iSushi:Restaurant {name:"iSushi"})-[:SERVES]->(sushi),(iSushi)-[:LOCATED_IN]->(nyc),
    (michael)-[:LIKES]->(iSushi),
    (andreas)-[:LIKES]->(iSushi),
    (zam:Restaurant {name:"Zushi Zam"})-[:SERVES]->(sushi),(zam)-[:LOCATED_IN]->(nyc),
    (andreas)-[:LIKES]->(zam)
    ----

    //graph

    === Philips Friends

    [source,cypher]
    ----
    MATCH (philip:Person {name:"Philip"})-[:IS_FRIEND_OF]-(person)
    RETURn person.name
    ----

    //table

    === Restaurants in NYC and their cusines

    [source,cypher]
    ----
    MATCH (nyc:City {name:"New York"})<-[:LOCATED_IN]-(restaurant)-[:SERVES]->(cusine)
    RETURN nyc, restaurant, cusine
    ----

    //table

    //graph_result

    === Graph Search Recommendation

    We want to answer the following question

    ""
    Find Sushi Restaurants in New York that my friends like.
    ""

    image::https://dl.dropboxusercontent.com/u/14493611/sushi_restaurants_nyc.png[]

    To satisfy this question, we have to know who's asking: _Philip_ and he's asking for 4 connected facts

    * _People_ that are friends of _Philip_
    * _Restaurants_ located in _New York_
    * _Restaurants_ that server _Sushi_
    * _Restaurants_ that his _Friends_ like

    [source,cypher]
    ----
    MATCH (philip:Person {name:"Philip"}),
    (philip)-[:IS_FRIEND_OF]-(friend),
    (restaurant:Restaurant)-[:LOCATED_IN]->(:City {name:"New York"}),
    (restaurant)-[:SERVES]->(:Cuisine {name:"Sushi"}),
    (friend)-[:LIKES]->(restaurant)
    RETURN restaurant.name, collect(friend.name) as likers, count(*) as occurence
    ORDER BY occurence DESC
    ----

    //table