-
Notifications
You must be signed in to change notification settings - Fork 276
Neo4j::Cypher Match
Pattern matching is one of the pillars of Cypher. The pattern is used to describe the shape of the data that we are looking for. Cypher will then try to find patterns in the graph — these are called matching sub graphs. See the Neo4j Documenation on Match
The neo4j-cypher DSL uses the folloing matching methods/operators: <
, >
, >>
, <<
, <=>
and both
, outgoing
and incoming
methods.
The <<
, <=>
and >>
is used when you don't care what type of relationship the has.
The other operators and method allows you to specify which relationship(s) to navigate.
Before reading this page please read about how to specify relationship patterns, nodes and variables here: Neo4j::Cypher-Start See Neo4j::Cypher-Return how to specify which value or values should be returned from a match.
The method <=>
will generate a pattern match without regard to type or direction.
Example:
node(3) <=> :x
Generates the following strings: START v1=node(3) MATCH v2 = (v1)--(x) RETURN v2
You can specify the direction of a relationship to match. Example: match all outgoing relationship of any type
node(3, 4) >> :x
See below for the outgoing
method, which works in a similar way (it returns the node instead of the path).
This will generate: START v1=node(3,4) MATCH v2 = (v1)-->(x) RETURN v2
Example: match all incoming relationship of any type and return those incoming nodes:
node(3, 4) << node.ret
This will generate: START v1=node(3,4) MATCH (v1)<--(v2) RETURN v2
See below for the incoming
method, which works in a similar way (it returns the node instead of the path).
If you don't want to specify the direction of the relationship but only the type of relationship
you can use the -
operator:
node(3) - ':knows|friends' - :foo
:foo
# Same as "START n0=node(3) MATCH (n0)-[:knows|friends]-(foo) RETURN foo"
Example, find all incoming relationship of type friends
to a node and return the end node.
node(3) > :friends > node(:x).ret
Will generate: START v1=node(3) MATCH (v1)-[:
friends]->(x) RETURN x
Example, find all incoming relationship of type friends
to a node and return the path.
node(3) < rel(':friends') < node
Will generate: START v1=node(3) MATCH v3 = (v1)<-[:friends]-(v2) RETURN v3
A path specified with for example <<
has a number of useful methods.
For example, we can return the length of the path like this:
Neo4j.query { p = node(3) >> :b; p.length }
Available methods for paths, see MatchRelRight
The outgoing
, incoming
and both
method works a bit similar to the <<
, >>
, <=>
operators.
Instead of returning the path it will return the last node.
Example:
node(3).both # same as node(3).both(rel)
This will generate the following string START v1=node(3) MATCH (v1)-[?]-(v2) RETURN v2
It is often useful to assign a variable to a path (as seen above), relationships or nodes. By doing that you can use the variable as a condition in the WHERE clause or in the RETURN clause.
You can do that in two ways.
Example: return all outgoing nodes with property name equal 'andreas'
x = node
node(3) >> x
x[:name] == 'andreas'
x
In the example above a new cypher variable name will automatically be created and used as a normal Ruby variable (x
)
This example will instead create a cypher node variable x
node(3) >> node(:x)
node(:x)[:name] == 'andreas'
:x
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