-
Notifications
You must be signed in to change notification settings - Fork 274
Neo4j::Core Nodes Properties Relationships
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, the meaning, of a node space
is encoded in the relationship types of the application.
Example of creating a Neo4j::Node
require "rubygems"
require 'neo4j'
Neo4j::Transaction.run do
node = Neo4j::Node.new
end
TIP: The Neo4j::Node.new
does actually return a Java Neo4j org.neo4j.graphdb.Node object which has been modified to fell more rubyish.
All Neo4j write operation 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
Neo4j::Rails::Model#transaction is a convenient wrapper for this.
TIP: The Neo4j::Transaction returns a Java org.neo4j.graph.Transaction for more info see Neo4j Transactions
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 of a primitive value (Fixnum
, Float
, TrueClass
, FalseClass
, String) and arrays of those primitive values.
Also, @Date
and DateTime
is also possible to save if you declared it, see Type Conversion below.
WARNING: Array Gotchas 1 all items in an array must be of the same type. Array Gotchas 2 you can not change one item in the array (they are mutable), you have to create a new array each time you add, remove, change one item.
If you declare a type of a property it can be converted into a primitive.
Example
class Person
include Neo4j::NodeMixin
property :born, :type => DateTime
end
The born property will be marshalled into a Java long value automatically since there is (by default) a
registered type conversion for DateTime in the Neo4j::Config
object under the key conversions
.
You can add your own conversion if you want to save a Ruby object with a property.
Example of creating an outgoing Neo4j::Relationship from node1 to node2 of type friends
node1 = Neo4j::Node.new
node2 = Neo4j::Node.new
Neo4j::Relationship.new(:friends, node1, node2)
# which is same as
node1.outgoing(:friends) << node2
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(:friends) # => all relationship of type friends, both incoming and outgoing
node1.rels(:friends).outgoing # => all outgoing relationship of type friends
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)
Note that _rels takes different arguments from rels!
node1.rels(:both, :friends)
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(:friends).outgoing.to_other(node_b) # => an enumerable of relationship of only outgoing relationships of type friend
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(:friends).outgoing.to_other(node_b).del # => an enumerable of relationship of only outgoing relationships of type friend
Example of setting properties on relationships
rel = node1.rels(:friends).outgoing.first # get the first relationship object
rel[:since] = 1982
WARNING: Much of the information in this wiki is out of date. We are in the process of moving things to readthedocs
- Project Introduction
- Neo4j::ActiveNode
- Neo4j::ActiveRel
- Search and Scope
- Validation, Uniqueness, and Case Sensitivity
- Indexing VS Legacy Indexing
- Optimized Methods
- Inheritance
- Core: Nodes & Rels
- Introduction
- Persistence
- Find : Lucene
- Relationships
- Third Party Gems & extensions
- Scaffolding & Generators
- HA Cluster