Getting started with Ruby and Neo4j is very easy.

Follow these steps and you’ll be up and running in no time.

First we install the neography gem:

Using Bundler:

echo "source 'http://rubygems.org' gem 'neography' " > Gemfile bundle install

Without Bundler:

gem install neography

Then we’ll add our tasks to a Rakefile, download Neo4j and start it:

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



What can you do with a graph database?

How about recommend friends on a Social Network?

require 'rubygems' require 'neography' @neo = Neography::Rest.new def create_person(name) @neo.create_node("name" => name) end def make_mutual_friends(node1, node2) @neo.create_relationship("friends", node1, node2) @neo.create_relationship("friends", node2, node1) end def suggestions_for(node) @neo.traverse(node, "nodes", {"order" => "breadth first", "uniqueness" => "node global", "relationships" => {"type"=> "friends", "direction" => "in"}, "return filter" => {"language" => "javascript", "body" => "position.length() == 2;"}, "depth" => 2}).map{|n| n["data"]["name"]}.join(', ') end johnathan = create_person('Johnathan') mark = create_person('Mark') phil = create_person('Phil') mary = create_person('Mary') luke = create_person('Luke') make_mutual_friends(johnathan, mark) make_mutual_friends(mark, mary) make_mutual_friends(mark, phil) make_mutual_friends(phil, mary) make_mutual_friends(phil, luke) puts "Johnathan should become friends with #{suggestions_for(johnathan)}" # RESULT # Johnathan should become friends with Mary, Phil

Let’s go through each step:

We require our gems and get an instance of Neography connecting to the Neo4j server:

require 'rubygems' require 'neography' @neo = Neography::Rest.new

We write a function to make it easy for us to create nodes that have a name property:

def create_person(name) @neo.create_node("name" => name) end

Then we create another function to relate two nodes together.

In Neo4j all relationships have a direction, so if want two people to be mutual friends we create two relationships one incoming and one outgoing.

def make_mutual_friends(node1, node2) @neo.create_relationship("friends", node1, node2) @neo.create_relationship("friends", node2, node1) end

Now comes the fun part. We want to suggest that you become friends with the friends of your friends.



So using a person as a starting point, we want to go out two friends relationships away and return with those friends of friends.

def suggestions_for(node) @neo.traverse(node, "nodes", {"order" => "breadth first", "uniqueness" => "node global", "relationships" => {"type"=> "friends", "direction" => "in"}, "return filter" => {"language" => "javascript", "body" => "position.length() == 2;"}, "depth" => 2}).map{|n| n["data"]["name"]}.join(', ') end

Let’s test this out by creating a small set of friends and link them together:

johnathan = create_person('Johnathan') mark = create_person('Mark') phil = create_person('Phil') mary = create_person('Mary') luke = create_person('Luke') make_mutual_friends(johnathan, mark) make_mutual_friends(mark, mary) make_mutual_friends(mark, phil) make_mutual_friends(phil, mary) make_mutual_friends(phil, luke) puts "Johnathan should become friends with #{suggestions_for(johnathan)}" # RESULT # Johnathan should become friends with Mary, Phil

There you have it. Pretty simple right?

No? If it looks like a bit much, can we make it easier?

Absolutely. Neography lets you work at the Neo4j REST API level, but also provides an additional layer sprinkled with ruby syntactic sugar.

require 'rubygems' require 'neography' def create_person(name) Neography::Node.create("name" => name) end johnathan = create_person('Johnathan') mark = create_person('Mark') phil = create_person('Phil') mary = create_person('Mary') luke = create_person('Luke') johnathan.both(:friends) << mark mark.both(:friends) << mary mark.both(:friends) << phil phil.both(:friends) << mary phil.both(:friends) << luke def suggestions_for(node) node.incoming(:friends). order("breadth first"). uniqueness("node global"). filter("position.length() == 2;"). depth(2). map{|n| n.name }.join(', ') end puts "Johnathan should become friends with #{suggestions_for(johnathan)}" # RESULT # Johnathan should become friends with Mary, Phil

Take a look at the Neography Documentation for more.

In this post we saw how to traverse the graph using the Traversal Framework directly.

In upcoming posts, I’ll show you two more ways to traverse the graph via Gremlin and Cypher as well as many more things you can do with Neo4j.

Find me and many other graph aficionados at the Neo4j Google Group.