Category Archives: Neography

Cypher with Neography

Cypher is the query language of Neo4j, and as promised I’ll show you how you can use it to implement friend recommendations as well as degrees of separation.

We can send any cypher query to Neo4j via the REST API and neography using the execute_query command. Let’s implement suggestions_for so it sends a cypher query to the server:

def suggestions_for(node)
  node_id = node["self"].split('/').last.to_i
  @neo.execute_query("START me = node({node_id})
                      MATCH (me)-[:friends]->(friend)-[:friends]->(foaf)
                      RETURN foaf.name", {:node_id => node_id})["data"]
end

puts "Johnathan should become friends with #{suggestions_for(johnathan).join(', ')}"

# RESULT
# Johnathan should become friends with Mary, Phil

Continue reading

Tagged , , ,

Gremlin with Neography

Gremlin is a domain specific language for traversing property graphs. Neo4j is one of the databases that can speak the gremlin language, and as promised I’ll show you how you can use it to implement friend recommendations as well as degrees of separation.

We can send any gremlin script to Neo4j via the REST API and neography using the execute_script command. Let’s implement suggestions_for so it sends a gremlin script to the server:

def suggestions_for(node)
  node_id = node["self"].split('/').last.to_i
  @neo.execute_script("g.v(node_id).
                         in('friends').
                         in('friends').
                         dedup.
                         filter{it != g.v(node_id)}.
                         name", {:node_id => node_id})
end

puts "Johnathan should become friends with #{suggestions_for(johnathan).join(', ')}"

# RESULT
# Johnathan should become friends with Mary, Phil

Continue reading

Tagged , , , ,

How you’re connected to Kevin Bacon

Previously I showed you how to get Neo4j up and running with Ruby and how to find recommended friends on a social network. What about finding out how you are connected to someone outside of your friends of friends network? Do you remember the concept of six degrees of separation? No, how about six degrees of Kevin Bacon?

A credit card commercial explains how this works:

The actor, Kevin Bacon wants to write a check to buy a book, but the clerk asks for his ID, which he does not have. He leaves and returns with a group of people, then says to the clerk, “Okay, I was in a movie with an extra, Eunice, whose hairdresser, Wayne, attended Sunday school with Father O’Neill, who plays racquetball with Dr. Sanjay, who recently removed the appendix of Kim, who dumped you sophomore year. So you see, we’re practically brothers.”

Continue reading

Tagged , , ,

Getting started with Ruby and Neo4j

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

Continue reading

Tagged , ,