10.1. Match

10.1.1. Introduction
10.1.2. Basic node finding
10.1.3. Relationship basics
10.1.4. Relationships in depth
10.1.5. Shortest path

10.1.1. Introduction

MATCH is the primary way of getting data from the database into the current set of bindings.

The MATCH clause allows you to specify the patterns Cypher will search for in the database.

Nodes and relationships that are already known at this stage are called bound pattern elements. Cypher will now try to find the unknown parts of the pattern.

If MATCH is the first clause in your query, nothing is bound at this stage. Cypher needs starting points to do it’s pattern matching.

If no bound nodes exist, Cypher can scan all nodes in the database, all nodes with a certain label, or it can use indexes to quickly find the relevant starting points. For more information on indexes, see Section 13.1, “Indexes”. If you want to use index hints to force Cypher to use a specific index, read more in Section 9.7, “Using”.

WHERE defines the MATCH patterns in more detail. The predicates are part of the pattern description, not a filter applied after the matching is done. This means that WHERE should always be put together with the MATCH clause it belongs to.

After finding starting points — either by using scans, indexes or already bound points — the execution engine will use pattern matching to find matching subgraphs. As Cypher is declarative, it can change the order of these operations. Predicates in WHERE clauses can be evaluated before pattern matching, during pattern matching, or after finding matches.

[Tip]Tip

To understand the patterns used in the MATCH clause, read Section 8.5, “Patterns”.

The following graph is used for the examples below:

Figure 10.1. Graph


10.1.2. Basic node finding

Get all nodes

By just specifying a pattern with a single node and no labels, all nodes in the graph will be returned.

Query. 

MATCH (n)
RETURN n

Returns all the nodes in the database.

Result

n
7 rows

Node[0]{name:"Oliver Stone"}

Node[1]{name:"Charlie Sheen"}

Node[2]{name:"Martin Sheen"}

Node[3]{name:"TheAmericanPresident",title:"The American President"}

Node[4]{name:"WallStreet",title:"Wall Street"}

Node[5]{name:"Rob Reiner"}

Node[6]{name:"Michael Douglas"}


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (n) return n

Get all nodes with a label

Getting all nodes with a label on them is done with a single node pattern where the node has a label on it.

Query. 

MATCH (movie:Movie)
RETURN movie

Returns all the movies in the database.

Result

movie
2 rows

Node[3]{name:"TheAmericanPresident",title:"The American President"}

Node[4]{name:"WallStreet",title:"Wall Street"}


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (movie:Movie) return movie

Related nodes

The symbol -- means related to, without regard to type or direction of the relationship.

Query. 

MATCH (director { name:'Oliver Stone' })--(movie)
RETURN movie.title

Returns all the movies directed by Oliver Stone.

Result

movie.title
1 row

"Wall Street"


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (director {name:'Oliver Stone'})--(movie) return movie.title

Match with labels

To constrain your pattern with labels on nodes, you add it to your pattern nodes, using the label syntax.

Query. 

MATCH (charlie:Person { name:'Charlie Sheen' })--(movie:Movie)
RETURN movie

Return any nodes connected with the Person Charlie that are labeled Movie.

Result

movie
1 row

Node[4]{name:"WallStreet",title:"Wall Street"}


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (charlie:Person {name:'Charlie Sheen'})--(movie:Movie) return movie

10.1.3. Relationship basics

Outgoing relationships

When the direction of a relationship is interesting, it is shown by using --> or <--, like this:

Query. 

MATCH (martin { name:'Martin Sheen' })-->(movie)
RETURN movie.title

Returns nodes connected to Martin by outgoing relationships.

Result

movie.title
2 rows

"Wall Street"

"The American President"


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (martin {name:'Martin Sheen'})-->(movie) return movie.title

Directed relationships and identifier

If an identifier is needed, either for filtering on properties of the relationship, or to return the relationship, this is how you introduce the identifier.

Query. 

MATCH (martin { name:'Martin Sheen' })-[r]->(movie)
RETURN r

Returns all outgoing relationships from Martin.

Result

r
2 rows

:ACTED_IN[1]{}

:ACTED_IN[3]{}


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (martin {name:'Martin Sheen'})-[r]->(movie) return r

Match by relationship type

When you know the relationship type you want to match on, you can specify it by using a colon together with the relationship type.

Query. 

MATCH (wallstreet { title:'Wall Street' })<-[:ACTED_IN]-(actor)
RETURN actor

Returns nodes that ACTED_IN Wall Street.

Result

actor
3 rows

Node[1]{name:"Charlie Sheen"}

Node[2]{name:"Martin Sheen"}

Node[6]{name:"Michael Douglas"}


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (wallstreet {title:'Wall Street'})<-[:ACTED_IN]-(actor) return actor

Match by multiple relationship types

To match on one of multiple types, you can specify this by chaining them together with the pipe symbol |.

Query. 

MATCH (wallstreet { title:'Wall Street' })<-[:ACTED_IN|:DIRECTED]-(person)
RETURN person

Returns nodes with a ACTED_IN or DIRECTED relationship to Wall Street.

Result

person
4 rows

Node[0]{name:"Oliver Stone"}

Node[1]{name:"Charlie Sheen"}

Node[2]{name:"Martin Sheen"}

Node[6]{name:"Michael Douglas"}


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (wallstreet {title:'Wall Street'})<-[:ACTED_IN|:DIRECTED]-(person) return person

Match by relationship type and use an identifier

If you both want to introduce an identifier to hold the relationship, and specify the relationship type you want, just add them both, like this.

Query. 

MATCH (wallstreet { title:'Wall Street' })<-[r:ACTED_IN]-(actor)
RETURN r

Returns nodes that ACTED_IN Wall Street.

Result

r
3 rows

:ACTED_IN[0]{}

:ACTED_IN[1]{}

:ACTED_IN[2]{}


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (wallstreet {title:'Wall Street'})<-[r:ACTED_IN]-(actor) return r

10.1.4. Relationships in depth

Relationship types with uncommon characters

Sometime your database will have types with non-letter characters, or with spaces in them. Use ` (backtick) to quote these.

Query. 

MATCH (n { name:'Rob Reiner' })-[r:`TYPE THAT HAS SPACE IN IT`]->()
RETURN r

Returns a relationship of a type with spaces in it.

Result

r
1 row

:TYPE THAT HAS SPACE IN IT[8]{}


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _5-[:`TYPE THAT HAS SPACE IN IT`]->_1 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (n {name:'Rob Reiner'})-[r:`TYPE THAT HAS SPACE IN IT`]->() return r

Multiple relationships

Relationships can be expressed by using multiple statements in the form of ()--(), or they can be strung together, like this:

Query. 

MATCH (charlie { name:'Charlie Sheen' })-[:ACTED_IN]->(movie)<-[:DIRECTED]->(director)
RETURN charlie,movie,director

Returns the three nodes in the path.

Result

charliemoviedirector
1 row

Node[1]{name:"Charlie Sheen"}

Node[4]{name:"WallStreet",title:"Wall Street"}

Node[0]{name:"Oliver Stone"}


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (charlie {name:'Charlie Sheen'})-[:ACTED_IN]->(movie)<-[:DIRECTED]->(director) return charlie,movie,director

Variable length relationships

Nodes that are a variable number of relationship→node hops away can be found using the following syntax: -[:TYPE*minHops..maxHops]->. minHops and maxHops are optional and default to 1 and infinity respectively. When no bounds are given the dots may be omitted.

Query. 

MATCH (martin { name:"Martin Sheen" })-[:ACTED_IN*1..2]-(x)
RETURN x

Returns nodes that are 1 or 2 relationships away from Martin.

Result

x
5 rows

Node[4]{name:"WallStreet",title:"Wall Street"}

Node[1]{name:"Charlie Sheen"}

Node[6]{name:"Michael Douglas"}

Node[3]{name:"TheAmericanPresident",title:"The American President"}

Node[6]{name:"Michael Douglas"}


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (martin {name:"Martin Sheen"})-[:ACTED_IN*1..2]-(x) return x

Relationship identifier in variable length relationships

When the connection between two nodes is of variable length, a relationship identifier becomes an collection of relationships.

Query. 

MATCH (actor { name:'Charlie Sheen' })-[r:ACTED_IN*2]-(co_actor)
RETURN r

The query returns a collection of relationships.

Result

r
2 rows

[:ACTED_IN[0]{},:ACTED_IN[1]{}]

[:ACTED_IN[0]{},:ACTED_IN[2]{}]


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (actor {name:'Charlie Sheen'})-[r:ACTED_IN*2]-(co_actor) return r

Match with properties on a variable length path

A variable length relationship with properties defined on in it means that all relationships in the path must have the property set to the given value. In this query, there are two paths between Charile Sheen and his dad Martin Sheen. One of the includes a “blocked” relationship and the other doesn’t. In this case we first alter the original graph by using the following query to add “blocked” and “unblocked” relationships:

MATCH (charlie:Person { name:'Charlie Sheen' }),(martin:Person { name:'Martin Sheen' })
CREATE (charlie)-[:X { blocked:false }]->(:Unblocked)<-[:X { blocked:false }]-(martin)
CREATE (charlie)-[:X { blocked:true }]->(:Blocked)<-[:X { blocked:false }]-(martin);

This means that we are starting out with the following graph:

Query. 

MATCH p =(charlie:Person)-[* { blocked:false }]-(martin:Person)
WHERE charlie.name = 'Charlie Sheen' AND martin.name = 'Martin Sheen'
RETURN p

Returns the paths between Charlie and Martin Sheen where all relationships have the blocked property set to FALSE.

Result

p
1 row

[Node[1]{name:"Charlie Sheen"},:X[12]{blocked:false},Node[9]{},:X[13]{blocked:false},Node[2]{name:"Martin Sheen"}]


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 MATCH p = (charlie:Person)-[* {blocked:false}]-(martin:Person) WHERE charlie.name = 'Charlie Sheen' AND martin.name = 'Martin Sheen' RETURN p

Zero length paths

Using variable length paths that have the lower bound zero means that two identifiers can point to the same node. If the distance between two nodes is zero, they are by definition the same node. Note that when matching zero length paths the result may contain a match even when matching on a relationship type not in use.

Query. 

MATCH (wallstreet:Movie { title:'Wall Street' })-[*0..1]-(x)
RETURN x

Returns all nodes that are zero or one relationships away from Wall Street.

Result

x
5 rows

Node[4]{name:"WallStreet",title:"Wall Street"}

Node[1]{name:"Charlie Sheen"}

Node[2]{name:"Martin Sheen"}

Node[6]{name:"Michael Douglas"}

Node[0]{name:"Oliver Stone"}


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (wallstreet:Movie {title:'Wall Street'})-[*0..1]-(x) return x

Named path

If you want to return or filter on a path in your pattern graph, you can a introduce a named path.

Query. 

MATCH p =(michael { name:'Michael Douglas' })-->()
RETURN p

Returns the two paths starting from Michael.

Result

p
2 rows

[Node[6]{name:"Michael Douglas"},:ACTED_IN[2]{},Node[4]{name:"WallStreet",title:"Wall Street"}]

[Node[6]{name:"Michael Douglas"},:ACTED_IN[4]{},Node[3]{name:"TheAmericanPresident",title:"The American President"}]


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match p = (michael {name:'Michael Douglas'})-->() return p

Matching on a bound relationship

When your pattern contains a bound relationship, and that relationship pattern doesn’t specify direction, Cypher will try to match the relationship in both directions.

Query. 

MATCH (a)-[r]-(b)
WHERE id(r)= 0
RETURN a,b

This returns the two connected nodes, once as the start node, and once as the end node.

Result

ab
2 rows

Node[1]{name:"Charlie Sheen"}

Node[4]{name:"WallStreet",title:"Wall Street"}

Node[4]{name:"WallStreet",title:"Wall Street"}

Node[1]{name:"Charlie Sheen"}


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (a)-[r]-(b) where id(r) = 0 return a,b

10.1.5. Shortest path

Single shortest path

Finding a single shortest path between two nodes is as easy as using the shortestPath function. It’s done like this:

Query. 

MATCH (martin:Person { name:"Martin Sheen" }),(oliver:Person { name:"Oliver Stone" }),
  p = shortestPath((martin)-[*..15]-(oliver))
RETURN p

This means: find a single shortest path between two nodes, as long as the path is max 15 relationships long. Inside of the parentheses you define a single link of a path — the starting node, the connecting relationship and the end node. Characteristics describing the relationship like relationship type, max hops and direction are all used when finding the shortest path. You can also mark the path as optional.

Result

p
1 row

[Node[2]{name:"Martin Sheen"},:ACTED_IN[1]{},Node[4]{name:"WallStreet",title:"Wall Street"},:DIRECTED[5]{},Node[0]{name:"Oliver Stone"}]


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (martin:Person {name:"Martin Sheen"} ), (oliver:Person {name:"Oliver Stone"}), p = shortestPath( (martin)-[*..15]-(oliver) ) return p

All shortest paths

Finds all the shortest paths between two nodes.

Query. 

MATCH (martin:Person { name:"Martin Sheen" }),(michael:Person { name:"Michael Douglas" }),
  p = allShortestPaths((martin)-[*]-(michael))
RETURN p

Finds the two shortest paths between Martin and Michael.

Result

p
2 rows

[Node[2]{name:"Martin Sheen"},:ACTED_IN[3]{},Node[3]{name:"TheAmericanPresident",title:"The American President"},:ACTED_IN[4]{},Node[6]{name:"Michael Douglas"}]

[Node[2]{name:"Martin Sheen"},:ACTED_IN[1]{},Node[4]{name:"WallStreet",title:"Wall Street"},:ACTED_IN[2]{},Node[6]{name:"Michael Douglas"}]


Try this query live. create (_0:`Person` {`name`:"Oliver Stone"}) create (_1:`Person` {`name`:"Charlie Sheen"}) create (_2:`Person` {`name`:"Martin Sheen"}) create (_3:`Movie` {`name`:"TheAmericanPresident", `title`:"The American President"}) create (_4:`Movie` {`name`:"WallStreet", `title`:"Wall Street"}) create (_5:`Person` {`name`:"Rob Reiner"}) create (_6:`Person` {`name`:"Michael Douglas"}) create _0-[:`DIRECTED`]->_4 create _1-[:`ACTED_IN`]->_4 create _1-[:`FATHER`]->_2 create _2-[:`ACTED_IN`]->_4 create _2-[:`ACTED_IN`]->_3 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_4 create _6-[:`ACTED_IN`]->_3 match (martin:Person {name:"Martin Sheen"} ), (michael:Person {name:"Michael Douglas"}), p = allShortestPaths( (martin)-[*]-(michael) ) return p