Neo4j-core v3.0 Code Climate Build Status

A simple Ruby wrapper around the Neo4j graph database that works with the server and embedded Neo4j API. This gem can be used both from JRuby and normal MRI. You may get better performance using it from JRuby and the embedded Neo4j, but it will probably be easier to develop (e.g. faster to run tests) on MRI and neo4j server. This gem is designed to work well together with the neo4j active model compliant gem (see the 3.0 branch).

For the stable v2.0 version, see the v2.0 branch Do not use this gem in production.


You can use this gem in two different ways:

Embedded or Server Neo4j ?

I suggest you start using the Neo4j server instead of Neo4j embedded because it is easier to use for development. If you later get performance problem (e.g. too many HTTP requests hitting the Neo4j Server) you can try the embedded neo4j with almost no changes in your code base. The embedded neo4j via JRuby also gives you direct access to the Neo4j Java API (e.g. the Neo4j Traversal API) which can be used to do more powerful and efficient traversals.

Usage from Neo4j Server

You need to install the Neo4j server. This can be done by using a Rake task.

Install the gem: gem install neo4j-core --pre

Create a Rakefile with the following content:

require 'neo4j/tasks/neo4j_server'

Install and start neo4j by typing: rake neo4j:install[community-2.1.1] rake neo4j:start

Usage from Neo4j Embedded

The Gemfile contains references to Neo4j Java libraries. Nothing is needed to be installed. The embedded database is only accessible from JRuby (unlike the Neo4j Server). No need to start the server since it is embedded.

Neo4j-core API, v3.0

API Documentation

See also Neo4j Docs

Database Session

There are currently two available types of session, one for connecting to a neo4j server and one for connecting to the embedded Neo4j database (which requires JRuby).

Using the Neo4j Server: :server_db Open a IRB/Pry session:

  require 'neo4j-core'
  # Using Neo4j Server Cypher Database
  session =

Session Configuration

Example, Basic Authentication:, 'http://my.server', basic_auth: { username: 'username', password: 'password'})

The last option hash is passed on to HTTParty. See here for more available options:

Embedded Session

Using the Neo4j Embedded Database, :embedded_db

  # Using Neo4j Embedded Database
  session =, '/folder/db', auto_commit: true)

To stop the database (only supported via the embedded database) use session.shutdown session.running? #=> false session.close # make the session not current/default


When a session has been created it will be stored in the Neo4j::Session object. Example, get the default session

session = Neo4j::Session.current

Multiple Sessions

The default session is used by all operation unless specified as the last argument. For example create a node with a different session:

my_session = Neo4j::Session.create_session(:server_db, "http://localhost:7474")
Neo4j::Node.create(name: 'kalle', my_session)

When using the Neo4j Server: :server_db, multiple sessions are supported. They can be created using the open_named method. This method takes two extra parameters, the second parameter is the session name, and the third parameter is whether the new session should over-ride the default session (becoming the session returned by calling Neo4j::Session.current). Valid options are true (always become current), false (never become current) and nil (become current if no existing current session).

Neo4j::Session.open_named(:server_db, :test, true, "https://localhost:7474")
session = Neo4j::Session.named :test # Returns the session named :test.
session = Neo4j::Session.current # Returns the session named :test, because the 'default' flag was set to true.

Label and Index Support

Create a node with an label person and one property ruby Neo4j::Node.create({name: 'kalle'}, :person)

Add index on a label

  person = Label.create(:person)
  person.create_index(:name) # compound keys will be supported in Neo4j 2.1

  # drop index
  # which indexes do we have and on which properties,
  red.indexes.each {|i| puts "Index #{i.label} properties: #{}"}

  # drop index, we assume it's the first one we want

  # which indices exist ?
  # (compound keys will be supported in Neo4j 2.1 (?))
  red.indexes # => {:property_keys => [[:age]]}


Only unique constraint and single property is supported (yet).

label = Neo4j::Label.create(:person)
label.create_constraint(:name, type: :unique)
label.drop_constraint(:name, type: :unique)

Creating Nodes

  # notice, label argument can be both Label objects or string/symbols.
  node = Node.create({name: 'andreas'}, red, :green)
  puts "Created node #{node[:name]} with labels #{', ')}"

Notice, nodes will be indexed based on which labels they have.

Setting properties

  node = Node.create({name: 'andreas'}, red, :green)
  node[:name] = 'changed name' # changes immediately one property
  node[:name] # => 'changed name'
  node.props # => {name: 'changed name'}
  node.props={ foo: 42}  # replace all properties
  node.update_props( bar: 42) # keeps old properties (unlike #props=) update with given hash

Notice properties are never stored in ruby objects, instead they are always fetched from the database.

Finding Nodes by Label

Each node and relationship has a id, neo_id

  node = Neo4j::Node.create
  # load the node again from the database
  node2 = Neo4j::Node.load(node.neo_id)

Finding nodes by label:

  # Find nodes using an index, returns an Enumerable
  Neo4j::Label.find_nodes(:red, :name, "andreas")

  # Find all nodes for this label, returns an Enumerable

  # which labels does a node have ?
  node.labels # [:red]

Cypher Queries

Full documentation, see Neo4j::Session#query or RSpecs.

Examples using queries as strings:

# same as Neo4j::Session.current.query
Neo4j::Session.query.create(n: Label: {mydata: 'Hello'}).exec

# With cypher parameters
Neo4j::Session.query.start(n: "node({a_parameter})").params(a_parameter: 0).pluck("ID(n)").first

Example of chained queries:

query = Neo4j::Session.query.match(n: :person) # Returns a Query object

query.return(:n) # Also returns a Query object

query.return(:n).to_a # Returns an array of result rows as Structs (i.e. [<struct n=Node>, etc...])

query.pluck(:n) # Returns an array of nodes

query.return(n: [:name, :age]) # => [<struct name='Brian', age=33>, etc...]

query.where(name: /kalle.*/)

query.order(n: {name: :desc, age: :asc}).skip(5).limit(4) # sorting and skip and limit the result

query.match('n-[:friends]->o').where(o: {age: 42}, n: {age: 1})

query.match('n-[f:friends]->o').pluck(:f) # [<Relationship>, etc..]


By default each Neo4j operation is wrapped in an transaction. If you want to execute several operation in one operation you can use the Neo4j::Transaction class, example: do
  n = Neo4j::Node.create(name: 'kalle')
  n[:age] = 42

Rollback occurs if an exception is thrown, or the failure method is called on the transaction.

E.g. do |tx|
  n = Neo4j::Node.create(name: 'kalle')
  tx.failure # all operations inside this tx will be rollbacked
  n[:age] = 42

This feature is experimental, since it has not been tested enough.


How to create a relationship between node n1 and node n2 with one property

n1 = Neo4j::Node.create
n2 = Neo4j::Node.create
rel = n1.create_rel(:knows, n2, since: 1994)

# Alternative
Neo4j::Relationship.create(:knows, n1, n2, since: 1994)

Setting properties on relationships works like setting properties on nodes.

Finding relationships

# any type any direction any label

# Outgoing of one type:
n1.rels(dir: :outgoing, type: :know).to_a

# same but expects only one relationship
n1.rel(dir: :outgoing, type: :best_friend)

# several types
n1.rels(types: [:knows, :friend])

# label
n1.rels(label: :rich)

# matching several labels
n1.rels(labels: [:rich, :poor])

# outgoing between two nodes
n1.rels(dir: :outgoing, between: n2)

Returns nodes instead of relationships

# same parameters as rels method
n1.nodes(dir: outgoing)
n1.node(dir: outgoing)

Delete relationship

rel = n1.rel(:outgoing, :know) # expects only one relationship


All method prefixed with _ gives direct access to the java layer/rest layer. Notice, the database starts with auto commit by default.

No state is cached in the neo4j-core (e.g. neo4j properties).

The public Neo4j::Node classes is abstract and provides a common API/docs for both the embedded and neo4j server.

The Neo4j::Embedded and Neo4j::Server modules contains drivers for classes like the Neo4j::Node. This is implemented something like this:

  class Neo4j::Node
    # YARD docs
    def [](key)
      # abstract method - impl using either HTTP or Java API

    def self.create(props, session=Neo4j::Session.current)

Both implementation use the same E2E specs.


The testing will be using much more mocking.

The public API

See also the cypher DSL gem, Neo4j Wiki

Version 3.0

The neo4j-core version 3.0 uses the java Jar and/or the Neo4j Server version 2.0.0-M6+ . This mean that it should work on Ruby implementation and not just JRuby !

It uses the new label feature in order to do mappings between Neo4j::Node (java objects) and your own ruby classes.

The code base for the 3.0 should be smaller and simpler to maintain because there is less work to be done in the Ruby layer but also by removing features that are too complex or not that useful.

The neo4j-wrapper source code is included in this git repo until the refactoring has stabilized. The old source code for neo4j-core is also included (lib.old). The old source code might later on be copied into the 3.0 source code (the lib folder).

The neo4j-core gem will work for both the embedded Neo4j API and the server api. That means that neo4j.rb will work on any Ruby implementation and not just JRuby. This is under investigation ! It's possible that some features for the Neo4j.rb 2.0 will not be available in the 3.0 version since it has to work with both the Neo4j server and Neo4j embedded APIs.

Since neo4j-core provides one unified API to both the server end embedded neo4j database the neo4j-wrapper and neo4j gems will also work with server and embedded neo4j databases.

New features:

Removed features:


Future (when Neo4j 2.1 is released) * Support for fulltext search * Compound keys in index


Notice there are different license for the neo4j-community, neo4j-advanced and neo4j-enterprise jar gems. Only the neo4j-community gem is by default required.