Skip to content

Neo4j::Cypher Match

Andreas Ronge edited this page Sep 25, 2012 · 16 revisions

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

Match Operators and Methods

The neo4j-cypher DSL uses methods to specify a match pattern by using the <, >, >>, <<, <=> and 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

Related nodes, <=>

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

Unknown Type, Outgoing Node, <<

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).

Unknown Type, Incoming Node, <<

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).

Outgoing or Incoming of a Type, -

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"

Outgoing of a Type, >

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

Incoming of a Type, <

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

Paths

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

#outgoing, #incoming and #both

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

Binding Matching Nodes to Variables

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.

Using Ruby Varaibles

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)

Using Cypher Variables

This example will instead create a cypher node variable x

node(3) >> node(:x)
node(:x)[:name] == 'andreas'
:x 
Clone this wiki locally