- Notifications
You must be signed in to change notification settings - Fork134
A thin Ruby wrapper to the Neo4j Rest API
License
maxdemarzi/neography
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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 athttps://github.com/andreasronge/neo4j by Andreas Ronge
Addneography
to your Gemfile:
gem'neography'
And run Bundler:
$ bundle
Or installneography
manually:
$ gem install'neography'
And require the gem in your Ruby code:
require'rubygems'require'neography'
Read the wiki for information aboutdependencies.
Rake tasks are available for downloading, installing and running Neo4j.
Configure Neography as follows:
# these are the default values:Neography.configuredo |config|config.protocol="http"config.server="localhost"config.port=7474config.directory=""# prefix this path with '/'config.cypher_path="/cypher"config.gremlin_path="/ext/GremlinPlugin/graphdb/execute_script"config.log_file="neography.log"config.log_enabled=falseconfig.slow_log_threshold=0# time in ms for query loggingconfig.max_threads=20config.authentication=nil# 'basic' or 'digest'config.username=nilconfig.password=nilconfig.parser=MultiJsonParserconfig.http_send_timeout=1200config.http_receive_timeout=1200config.persistent=trueend
Then initialize aRest
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 theconfiguration and initialization page in the Wiki.
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:
- Schema indexes - Create, get and delete schema indexes.
- Node labels - Create, get and delete node labels.
- Transactions - Begin, add to, commit, rollback transactions.
1.8+ features:
- Nodes - Create, get and delete nodes.
- Node properties - Set, get and remove node properties.
- Node relationships - Create and get relationships between nodes.
- Relationship - Get and delete relationships.
- Relationship properties - Create, get and delete relationship properties.
- Relationship types - List relationship types.
- Node indexes - List and create node indexes. Add, remove, get and search nodes in indexes.
- Relationship indexes - List and create relationships indexes. Add, remove, get and search relationships in indexes.
- Auto indexes - Get, set and remove auto indexes.
- Scripts and queries - Run Gremlin scripts and Cypher queries.
- Paths and traversal - Paths between nodes and path traversal.
- Batch - Execute multiple calls at once.
- Errors - Errors raised if REST API calls fail.
Some example usage:
# Node creation:node1=@neo.create_node("age"=>31,"name"=>"Max")node2=@neo.create_node("age"=>33,"name"=>"Roel")# Node properties:@neo.set_node_properties(node1,{"weight"=>200})# Relationships between nodes:@neo.create_relationship("coding_buddies",node1,node2)# Get node relationships:@neo.get_node_relationships(node2,"in","coding_buddies")# Use indexes:@neo.add_node_to_index("people","name","max",node1)@neo.get_node_index("people","name","max")# Batches:@neo.batch[:create_node,{"name"=>"Max"}],[:create_node,{"name"=>"Marc"}]# Cypher queries:@neo.execute_query("start n=node(0) return n")
You can also use thecypher 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 theWiki documentation for the full API.
Neography raises REST API errors as Ruby errors, see the wiki page abouterrors.(Note: older versions of Neography did not raise any errors!)
Trying to mimic theNeo4j.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 usingnode.neo_id
.
Some of this functionality is shown here, but all of it is explained in the following Wiki pages:
- Nodes - Create, load and delete nodes.
- Node properties - Add, get and remove node properties.
- Node relationships - Create and retrieve node relationships.
- Node paths - Gets paths between nodes.
# create two nodes:n1=Neography::Node.create("age"=>31,"name"=>"Max")n2=Neography::Node.create("age"=>33,"name"=>"Roel")n1.exist?# => true# get and change some properties:n1[:age]# => 31n1.name# => "Max"n1[:age]=32# change propertyn1.weight=190# new propertyn1.age=nil# remove property# add a relationship between nodes:new_rel=Neography::Relationship.create(:coding_buddies,n1,n2)# remove a relationship:new_rel.del# add a relationship on nodes:n1.outgoing(:coding_buddies) <<n2# more advanced relationship traversal:n1.outgoing(:friends)# Get nodes related by outgoing friends relationshipn1.outgoing(:friends).depth(2).include_start_node# Get n1 and nodes related by friends and friends of friendsn1.rel?(:outgoing,:friends)# Has outgoing friends relationshipn1.rels(:friends,:work).outgoing# Get outgoing friends and work relationshipsn1.all_paths_to(n2).incoming(:friends).depth(4)# Gets all paths of a specified typen1.shortest_path_to(n2).incoming(:friends).depth(4).nodes# Gets just nodes in path
This is just a small sample of the full API, see theWiki documentation for the full API.
Someexample code.
Sometips about testing.
Complement to Neography are the:
- Neo4j Active Record Adapter by Nikhil Lanjewar
- Neology by Carlo Alberto Degli Atti
- Neoid by Elad Ossadon
An alternative to Neography isArchitect4r by Maximilian Schulz
- Vouched
- Neovigator fork it athttps://github.com/maxdemarzi/neovigator
- Neoflix fork it athttps://github.com/maxdemarzi/neoflix
Please create anew issue if you run into any bugs.
Contribute patches viapull requests.
If you are just starting out, or need help send me an e-mail atmaxdemarzi@gmail.com.
Check you my blog athttp://maxdemarzi.com where I have more Neography examples.
- Neography - MIT, see the LICENSE filehttp://github.com/maxdemarzi/neography/tree/master/LICENSE.
- Lucene - Apache, seehttp://lucene.apache.org/java/docs/features.html
- Neo4j - Dual free software/commercial license, seehttp://neo4j.org
About
A thin Ruby wrapper to the Neo4j Rest API