NoSQL Zone is brought to you in partnership with:

Max De Marzi, is a seasoned web developer. He started building websites in 1996 and has worked with Ruby on Rails since 2006. The web forced Max to wear many hats and master a wide range of technologies. He can be a system admin, database developer, graphic designer, back-end engineer and data scientist in the course of one afternoon. Max is a graph database enthusiast. He built the Neography Ruby Gem, a rest api wrapper to the Neo4j Graph Database. He is addicted to learning new things, loves a challenge and finding pragmatic solutions. Max is very easy to work with, focuses under pressure and has the patience of a rock. Max is a DZone MVB and is not an employee of DZone and has posted 60 posts at DZone. You can read more from them at their website. View Full User Profile

Neo4j Spatial, Part 1: Building a Recommendation Engine

02.12.2014
| 5014 views |
  • submit to reddit

 

http://www.iconarchive.com/show/gis-gps-map-icons-by-icons-land/Layers-icon.html

One of my new year resolutions is to do a project with Neo4j Spatial, so we’ll kick off my first blog post of the year with a gentle introduction to this awesome plugin. I advise you to watch this very short 15 minute video by Neo4j Spatial creator Craig Taverner. The man is a genius level developer, you’ll gain IQ points just listening, I swear.

The plan is to make a Restaurant Recommendation engine based on things you care about and your current location. Yes, this is baby level stuff, but we’ll start with this and see where else Neo4j Spatial can take us later on.

So the first thing I always do when starting a new project is look for data. Asking Google, the all knowing and all powerful, I ran into… factual

Factual has a massive amount of data accessible by API and specifically some Restaurant Data that I was interested in. Let’s take a look at it shall we:

{
  "accessible_wheelchair": true,
  "address": "5951 N Broadway St",
  "alcohol": true,
  "alcohol_bar": true,
  "alcohol_beer_wine": true,
  "alcohol_byob": true,
  "attire": "casual",
  "category_ids": [
    366
  ],
  "category_labels": [
    [
      "Social",
      "Food and Dining",
      "Restaurants",
      "Sushi"
    ]
  ],
  "country": "us",
  "cuisine": [
    "Cafe",
    "Thai",
    "Sushi",
    "Asian",
    "Japanese"
  ],
  "email": "indieincindiecafe@gmail.com",
  "factual_id": "8f4b9dff-d1e2-47d5-b761-1e63928f79ac",
  "groups_goodfor": true,
  "hours": "{\"monday\":[[\"15:00\",\"22:00\",\"Dinner\"]],\"tuesday\":[[\"17:00\",\"22:00\",\"Dinner\"]],\"wednesday\":[[\"15:00\",\"22:00\",\"Dinner\"],[\"12:00\",\"15:00\"]],\"thursday\":[[\"15:00\",\"22:00\",\"Dinner\"],[\"12:00\",\"15:00\"]],\"friday\":[[\"15:00\",\"22:30\",\"Dinner\"],[\"12:00\",\"15:00\"]],\"saturday\":[[\"15:00\",\"22:30\",\"Dinner\"],[\"12:00\",\"15:00\"]],\"sunday\":[[\"15:00\",\"22:00\",\"Dinner\"],[\"12:00\",\"15:00\"]]}",
  "hours_display": "Mon 3:00 PM-10:00 PM; Tue 5:00 PM-10:00 PM; Wed-Thu 12:00 PM-10:00 PM; Fri-Sat 12:00 PM-10:30 PM; Sun 12:00 PM-10:00 PM",
  "kids_goodfor": true,
  "latitude": 41.990326,
  "locality": "Chicago",
  "longitude": -87.672907,
  "meal_cater": true,
  "meal_deliver": true,
  "meal_dinner": true,
  "meal_lunch": true,
  "meal_takeout": true,
  "name": "Indie Cafe",
  "neighborhood": [
    "Edgewater",
    "Far North Side",
    "northside",
    "North Edgewater"
  ],
  "open_24hrs": false,
  "options_healthy": true,
  "options_vegan": true,
  "options_vegetarian": true,
  "parking": true,
  "parking_street": true,
  "payment_cashonly": false,
  "postcode": "60660",
  "price": 2,
  "rating": 5,
  "region": "IL",
  "reservations": true,
  "seating_outdoor": true,
  "smoking": false,
  "status": "1",
  "tel": "(773) 561-5577",
  "website": "http://www.indiecafe.us"
}

Look at all that beautiful data. We know their hours, whether or not they have healthy, vegan and vegetarian options. If they are a good restaurant for kids or large groups, cuisines, rating, price… as well as latitude and longitude. We also know if they take reservations, but we have no easy way to just make a reservation (other than calling them and talking to a real human being, but who wants to do that). I know how to fix this…

opentable_logo_reg

I’ve used OpenTable before, and after spending some time poking around their site looking for an API I was unable to find one. Not to fear, Dan Sosedoff was kind enough to build an unofficial OpenTable API for us.

What kind of data can we get here:

{
  "id": 68710,
  "name": "Indie Cafe",
  "address": "5951 North Broadway",
  "city": "Chicago",
  "state": "IL",
  "area": "Chicago / Illinois",
  "postal_code": "60660",
  "country": "US",
  "phone": "7735615111",
  "reserve_url": "http://www.opentable.com/single.aspx?rid=68710",
  "mobile_reserve_url": "http://mobile.opentable.com/opentable/?restId=68710"
}

Awesome, there is that magic reservation link I was looking for. Now if only I had menus and menu items…

food_genius

Ah ha! Found some, but they seem to have discontinued their data API in a recent enterprise pivot. No matter, I’ll e-mail them and ask for a sample of their data. With any luck we’ll have more data than we know what to do with.

Now, back to Neo4j Spatial. The easiest thing we can do here, is create a spatial index and add the restaurants to it, then we’ll be able to query it. Now, us Ruby developers are a lazy lot, so instead of messing around with maven and building the plugin from source, I tweeked Neography so that installing Neo4j with the Spatial plugin looks like this:

echo "require 'neography/tasks'" > Rakefile
rake neo4j:install
rake neo4j:get_spatial
rake neo4j:start

Easy right? Now let’s try creating a Restaurants Spatial Index and adding a restaurant to it:

@neo = Neography::Rest.new
@neo.create_spatial_index("restaurants")

The response we get back from the server is:

{ "template" => "http://localhost:7474/db/data/index/node/restaurants/{key}/{value}",
  "provider" => "spatial",
  "geometry_type" => "point",
  "lat" => "lat",
  "lon" => "lon" }

But what actually happened?

Published at DZone with permission of Max De Marzi, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)