-
Notifications
You must be signed in to change notification settings - Fork 276
Neo4j::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.
The neo4j-cypher gem makes it possible to generate Cypher query strings from Ruby DSLs. The cypher language is described here: Neo4j Cypher Documentation See the RSpecs for more examples here
Why should I write my queries using the neo4j-cypher DSL instead of using original cypher syntax ? My Answer Better readablity for (ruby) programmers, which means easier to understand and write queries.
For example: Find my friends I got 1994 using the original cypher language:
START me=node(1)
MATCH (me)-[friend_rel:`friends`]->(friends)
WHERE (friend_rel.since = 1994)
RETURN friends
Instead of relying on a strict order of the clauses (START
, MATCH
, WHERE
...)
and having to use variables (me and friends) you can write it like this:
node(1).outgoing(rel(:friends).where{|r| r[:since] == 1994})
This is more or less plain english (for me), navigate from node(1) outgoing relationships friends where friends since property is equal 1994. Remember just like ruby, the last value evaluated is the return value which means it will return your friend.
Another way if you want to write it in many lines and using Ruby variables:
me = node(1)
friend_rel = rel(:friends)
friend = node
match { me > friend_rel > friend }
where { friend_rel[:since] == 1994 }
ret friend
Or using >
operator and flagging what to return with the ret
method.
node(1) > (rel(:friends)[:since] == 1994) > node.ret
The Ruby DSL gives you the control how best to express queries using Ruby syntax.
Notice
- The neo4j-cypher gem can be used both from neo4j.rb (see Neo4j::Core-Cypher) and from the Neo4j rest API, see neography
- Both neography (
Neography.execute_query
) and neo4j-core (Neo4j.query
) includes methods which also executes the query.
- API - The neo4j-cypher and neography cypher adaptor.
- Start - Starting points in the graph, obtained via index lookups or by element IDs.
- Match - The graph pattern to match, bound to the starting points.
- Where - Filtering criteria.
- Return - What to return.
- Mutating - TODO Create,Delete,Set, Foreach
- With - TODO Divides a query into multiple, distinct parts.
- Examples - Some examples
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