Skip to content
Rafal Wrzochol edited this page Mar 14, 2017 · 6 revisions

Label and Index Support

API

Neo4j::Label

Examples

Create a node with an label person and one property

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
person.drop_index(:name)
# which indexes do we have and on which properties,
  red.indexes.each {|i| puts "Index #{i.label} properties: #{i.properties}"}

  # drop index, we assume it's the first one we want
  red.indexes.first.drop(:name)

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

Constraints

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 #{node.labels.map(&:name).join(', ')}"

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
  Neo4j::Label.find_all_nodes(:red)

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

Index and Constraint Maintenance

Neo4j::Label includes six class methods that assist with maintenance of indexes and constraints. Non-constraint index operations typically include constraints because Neo4j does not differentiate between the two. A constraint is an index but an index is not necessarily a constraint.

# Return all indexes, including constraints
Neo4j::Label.indexes
# Return all constraints
Neo4j::Label.constraints

# Drop all non-constraint indexes
Neo4j::Label.drop_all_indexes
# Drop all constraints
Neo4j::Label.drop_all_constraints

# Boolean indicating presence of an index or constraint
Neo4j::Label.index?('Label', 'property')
# Boolean indicating presence of a constraint
Neo4j::Label.constraint?('Label', 'property')

WARNING: Much of the information in this wiki is out of day. We are in the process of moving things to readthedocs

Api

Contribute

Clone this wiki locally