Neography
Welcome to Neography
Neography is a thin Ruby wrapper to the Neo4j Rest API, for more information:
If you want to utilize the full power of Neo4j, you will want to use JRuby and the excellent Neo4j.rb gem at https://github.com/andreasronge/neo4j by Andreas Ronge
Installation
Gemfile
Add neography
to your Gemfile:
gem 'neography'
And run Bundler:
$ bundle
Manually:
Or install neography
manually:
$ gem install 'neography'
And require the gem in your Ruby code:
require 'rubygems'
require 'neography'
Read the wiki for information about dependencies.
Rake tasks are available for downloading, installing and running Neo4j.
Usage
Configuration and initialization
Configure Neography as follows:
Neography.configure do |config|
config.protocol = "http"
config.server = "localhost"
config.port = 7474
config.directory = ""
config.cypher_path = "/cypher"
config.gremlin_path = "/ext/GremlinPlugin/graphdb/execute_script"
config.log_file = "neography.log"
config.log_enabled = false
config.slow_log_threshold = 0
config.max_threads = 20
config.authentication = nil
config.username = nil
config.password = nil
config.parser = MultiJsonParser
config.http_send_timeout = 1200
config.http_receive_timeout = 1200
config.persistent = true
end
Then initialize a Rest
instance:
@neo = Neography::Rest.new
@neo = Neography::Rest.new({:authentication => 'basic', :username => "neo4j", :password => "swordfish"})
@neo = Neography::Rest.new("http://neo4j:swordfish@localhost:7474")
For overriding these default and other initialization methods, see the
configuration and initialization page in the Wiki.
REST API
Neography supports the creation and retrieval of nodes and relationships through the Neo4j REST interface.
It supports indexes, Gremlin scripts, Cypher queries and batch operations.
Some of this functionality is shown here, but all of it is explained in the following Wiki pages:
2.0 Only features:
1.8+ features:
Some example usage:
node1 = @neo.create_node("age" => 31, "name" => "Max")
node2 = @neo.create_node("age" => 33, "name" => "Roel")
@neo.set_node_properties(node1, {"weight" => 200})
@neo.create_relationship("coding_buddies", node1, node2)
@neo.get_node_relationships(node2, "in", "coding_buddies")
@neo.add_node_to_index("people", "name", "max", node1)
@neo.get_node_index("people", "name", "max")
@neo.batch [:create_node, {"name" => "Max"}],
[:create_node, {"name" => "Marc"}]
@neo.execute_query("start n=node(0) return n")
You can also use the cypher gem instead of writing cypher as text.
node(1).outgoing(rel(:friends).where{|r| r[:since] == 1994})
would become:
START me=node(1)
MATCH (me)-[friend_rel:`friends`]->(friends)
WHERE (friend_rel.since = 1994)
RETURN friends
This is just a small sample of the full API, see the Wiki documentation for the full API.
Neography raises REST API errors as Ruby errors, see the wiki page about errors.
(Note: older versions of Neography did not raise any errors!)
Phase 2
Trying to mimic the Neo4j.rb API.
Now we are returning full objects. The properties of the node or relationship can be accessed directly (node.name
).
The Neo4j ID is available by using node.neo_id
.
Some of this functionality is shown here, but all of it is explained in the following Wiki pages:
n1 = Neography::Node.create("age" => 31, "name" => "Max")
n2 = Neography::Node.create("age" => 33, "name" => "Roel")
n1.exist?
n1[:age]
n1.name
n1[:age] = 32
n1.weight = 190
n1.age = nil
new_rel = Neography::Relationship.create(:coding_buddies, n1, n2)
new_rel.del
n1.outgoing(:coding_buddies) << n2
n1.outgoing(:friends)
n1.outgoing(:friends).depth(2).include_start_node
n1.rel?(:outgoing, :friends)
n1.rels(:friends,:work).outgoing
n1.all_paths_to(n2).incoming(:friends).depth(4)
n1.shortest_path_to(n2).incoming(:friends).depth(4).nodes
This is just a small sample of the full API, see the Wiki documentation for the full API.
More
Examples
Some example code.
Testing
Some tips about testing.
Related Neo4j projects
Complement to Neography are the:
An alternative to Neography is Architect4r by Maximilian Schulz
Neography in the Wild
Getting started with Neography
Contributing
Please create a new issue if you run into any bugs.
Contribute patches via pull requests.
Help
If you are just starting out, or need help send me an e-mail at maxdemarzi@gmail.com.
Check you my blog at http://maxdemarzi.com where I have more Neography examples.
Licenses