-
Notifications
You must be signed in to change notification settings - Fork 276
Neo4j::Core Cypher
“Cypher” is a declarative graph query language that allows for expressive and efficient querying of the graph store without having to write traversals through the graph structure in code. Neo4j.rb provides two ways of using cypher: express queries as Strings or use the Neo4j.rb Cypher DSL.
The cypher language is described here: Neo4j Cypher Docuementation
q = Neo4j._query("START n=node(42) RETURN n")
q.first(:n) #=> the @node
q.columns.first => :n
See Neo4j#query method.
Every query describes a pattern, and in that pattern one can have multiple start points. A start point is a relationship or a node that form the starting points for a pattern match. You can either introduce start points by id, or by index lookups. Note that trying to use an index that doesn’t exist will throw an exception.
Binding a node as a start point is done with the node(*) function.
Neo4j.query{ node(3) }
# same as START n=node(1) RETURN n
The node above will be assigned a variable automatically.
To get the variable name: use the columns
method, example:
result = Neo4j.query{ node(3) }
result.columns # => [:n0]
result.first[:n0] #=> returns the node 3
Notice that the query result is a once forward Enumerable object.
To read the result more then once use #to_a
to create an array of the Enumerable.
See http://docs.neo4j.org/chunked/snapshot/cypher-cookbook-friend-finding.html Let say we want to express the following Cypher Query:
START joe=node(some node id)
MATCH joe-[:knows]->friend-[:knows]->friend_of_friend, joe-[r?:knows]->friend_of_friend
WHERE r IS NULL
RETURN friend_of_friend.name, COUNT(*)
ORDER BY COUNT(*) DESC, friend_of_friend.name
This can be done like this:
Neo4j.query(joe_node) do |joe|
friends_of_friends = node(:friends_of_friends)
joe > ':knows' > node(:friend) > ':knows' > friends_of_friends
r = rel('r?:knows').as(:r)
joe > r > friends_of_friends
r.exist?
ret(friends_of_friends[:name], count).desc(count).asc(friends_of_friends[:name])
end
Or like this using the outgoing
instead of the >
operator
Neo4j.query(joe_node) do |joe|
friends_of_friends = joe.outgoing(:knows).outgoing(:knows)
r = rel('r?:knows').as(:r)
joe > r > friends_of_friends
r.exist?
ret(friends_of_friends[:name], count).desc(count).asc(friends_of_friends[:name])
end
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