Skip to content

Neo4j::Core Nodes Properties Relationships

Brian Underwood edited this page Jun 19, 2016 · 13 revisions

WARNING: THIS PAGE MAY BE OUT OF DATE

See http://neo4jrb.readthedocs.org for the latest docs

The Neo4j node space consists of three basic elements: nodes, relationships that connect nodes, and properties attached to both nodes and relationships. All relationships have a type. For example, if the node space represents a social network, a relationship type could be knows. If a relationship of the type knows connects two nodes, that probably represents two people that know each other. A lot of the semantics, or meaning, of a node space is encoded in the relationship types of the application.

Creating Nodes

Example of creating a Neo4j::Node

require "rubygems"
require 'neo4j'

Neo4j::Transaction.run do
  node = Neo4j::Node.new
end

TIP: The Neo4j::Node.new will actually return a Java Neo4j org.neo4j.graphdb.Node object, which has been modified to feel more like ruby.

Transactions

All Neo4j write operations must be wrapped in a transaction as shown above.
Here is one way of creating transaction:

Neo4j::Transaction.run do
  # neo4j operations goes here
end

See Yard or the Neo4j docs

Properties

Example of setting properties:

Neo4j::Node.new :name=>'foo', :age=>123, :hungry => false, 4 => 3.14
# which is same as the following:
node = Neo4j::Node.new
node[:name] = 'foo'
node[:age]  = 123
node['hungry'] = false
node[4] = 3.14
node[:age] #  => 123

Properties can be any primitive value (Fixnum, Float, TrueClass, FalseClass, String) or an array of those primitive values.

Notice the neo4j and neo4j-wrapper gems support type conversion (e.g. for saving DateTime objects).
Array Gotcha #1 all items in an array must be of the same type.
Array Gotcha #2 you can’t change just one item in an array (they are mutable), you have to create a new array each time you add, remove, or change an item.

Creating Relationships

Example of creating an outgoing Neo4j::Relationship from node1 to node2 of type friends:

start_node = Neo4j::Node.new
end_node = Neo4j::Node.new
Neo4j::Relationship.new(:friends, start_node, end_node)
# which is same as:
start_node.outgoing(:friends) << end_node

Declared relationships can be used here as well:

Neo4j::Relationship.new(User.friends, start_node, end_node)
# which is same as:
start_node.friends << end_node
# User.friends has a relationship type of :'User#friends'

Finding Relationships & Nodes

Example of getting relationships.

node1.rels # => an Enumerable of all incoming and outgoing relationship of any type
node1.rel?  # => true if there are any relationship of any type
node1.rels(:outgoing, :friends) # => all outgoing relationship of type friends
node1.rels(:both, :friends) # => all relationship of type friends, both incoming and outgoing
node1.rel(:both, :best_friend)  # => returns one relationship, nil or throws an exception if there are more the two relationship from this node
node1.rel(:incoming, :best_friend)

Examples of accessing nodes via relationships with other nodes:

node1.node(:outgoing, :best_friend) # the node connected with outgoing relationship of type best friend (or an exception)
node1.nodes(:outgoing, :best_friend) # all nodes connected with outgoing relationship of type best friend
node1._node(:outgoing, :best_friend) # same as node but does not try to wrap the node using Neo4j::NodeMixin or Model
node1._nodes # same as nodes but does not try to wrap nodes

These methods are also available in the Neo4j::Rails::Model and Neo4j::NodeMixin.
See the YARD docs

Notice the first argument is always the direction. It defaults to :both if not specified.
Notice the _rels methods takes the same arguments as rels and can sometimes be faster since it does not wrap nodes and relationships.

Finding Relationships between two nodes

You can find all relationships between two nodes like this:

node_a.rels.to_other(node_b)  # => an enumerable of all relationship between those two nodes
node_a.rels(:outgoing, :friends).to_other(node_b)  # => an enumerable of relationship of only outgoing relationships of type friend

Deleting Relationships between two nodes

You can combine the to_other method with del to delete relationships between two nodes (see above)

node_a.rels.to_other(node_b).del  # => an enumerable of all relationship between those two nodes
node_a.rels(:outgoing, :friends).to_other(node_b).del  # => an enumerable of relationship of only outgoing relationships of type friend

Properties on Relationships

Example of setting properties on relationships

rel = node1.rels(:outgoing, :friends).first  # get the first relationship object 
rel[:since] = 1982
Clone this wiki locally