Skip to content
andreasronge edited this page Oct 1, 2012 · 2 revisions

The API for the neo4j-cypher gem consists of one method: Neo4j::Cypher.query which returns an object of type Neo4j::Cypher::Result

Neo4j::Cypher.query

The Neo4j::Cypher.query method takes a ruby block as argument and returns an Neo4j::Cypher::Result. The Neo4j::Cypher::Result#to_s method returns the query as a string.

Example:

Neo4j::Cypher.query do
  node(3) > :r > :x
end.to_s

This will generate the following string "START v1=node(3) MATCH v2 = (v1)-[:`r`]->(x) RETURN v2"

Neo4j::Cypher::Result

The Neo4j::Cypher::Result has two useful method to_s (as shown above) and return_names. As shown in the example above the DSL can automatically generate new variables. In order to know which values are returned use the return_names method.

Example:

Neo4j::Cypher.query do
    node(3) > :r > :x
end.return_names

It will return the following array: [:v2]

Neography.execute_query

The neo4j-cypher gem includes a small adaptor for the neography gem which makes the following possible:

require 'neography'
require 'neo4j-cypher'
require 'neo4j-cypher/neography' #adds a Neography::Rest#execute_cypher method

@neo = Neography::Rest.new
@neo.execute_cypher(n) { |me| me > ':friends' > node > ':friends' > node(:foaf)[:name].ret }['data']

See this for a complete example. Notice that using JRuby is not required !

Neo4j.query

The neo4j-core gem also wraps the neo4j-cypher API.

The following example will also execute the query using the native neo4j database.

require 'neo4j-core'

q = Neo4j.query{ node(42) }

Notice that the neo4j-core gem must be run on JRuby

See Neo4j::Core-Cypher

Clone this wiki locally