10.1. Match

Introduction

The MATCH clause allows you to specify the patterns Cypher will search for in the database, and it is the primary way of getting data into the current set of bindings. It is worth reading up more on the specification of the patterns themselves in Section 8.8, “Patterns”.

It is often coupled to a WHERE clause which adds restrictions, or predicates, to the MATCH patterns, making them more specific. 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.

MATCH can occur at the beginning of the query or later, possibly after a WITH. If it is the first, nothing will have been bound yet, and Cypher will design a search to find the results matching the clause and any associated predicates specified in any WHERE clause. This could involve a scan of the database, a search for nodes of a certain label, or a search of an index to find starting points for the pattern matching. Nodes and relationships found by this search are available as bound pattern elements, and can be used for pattern matching of sub-graphs, as well as used in any further MATCH clauses, where Cypher will use the known elements, and from there find further unknown elements.

Cypher is declarative, and so usually the query itself does not specify the algorithm to use to perform the search. Cypher will automatically work out the best approach to finding start nodes and matching patterns. Predicates in WHERE clauses can be evaluated before pattern matching, during pattern matching, or after finding matches. However, there are cases where you can influence the decisions taken by the query compiler. Read more about indexes in Section 13.1, “Indexes”, and more about the specifying index hints to force Cypher to use a specific index in Section 9.8, “Using”.

[Tip]Tip

To understand more about the patterns used in the MATCH clause, read Section 8.8, “Patterns”.

The following graph is used for the examples below:

Figure 10.1. Graph

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 match (movie:Movie) return movie

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 match (charlie:Person {name:'Charlie Sheen'})--(movie:Movie) return movie

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

"The American President"

"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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 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[3]{}

:ACTED_IN[1]{}

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 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[6]{name:"Michael Douglas"}

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

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 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[6]{name:"Michael Douglas"}

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

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

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 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[2]{}

:ACTED_IN[1]{}

:ACTED_IN[0]{}

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 match (wallstreet {title:'Wall Street'})<-[r:ACTED_IN]-(actor) return r

Relationships in depth

[Note]Note

Inside a single pattern, relationships will only be matched once. You can read more about this in Section 7.4, “Uniqueness”.

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _5-[:`TYPE THAT HAS SPACE IN IT`]->_1 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 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[3]{name:"TheAmericanPresident",title:"The American President"}

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

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

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

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 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[2]{}]

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

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 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[8]{blocked:false},Node[7]{},:X[9]{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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 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[6]{name:"Michael Douglas"}

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

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

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 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[4]{},Node[3]{name:"TheAmericanPresident",title:"The American President"}]

[Node[6]{name:"Michael Douglas"},:ACTED_IN[2]{},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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 match (a)-[r]-(b) where id(r) = 0 return a,b

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 match (martin:Person {name:"Martin Sheen"} ), (michael:Person {name:"Michael Douglas"}), p = allShortestPaths( (martin)-[*]-(michael) ) return p

Get node or relationship by id

Node by id

Search for nodes by id can be done with the id function in a predicate.

[Note]Note

Neo4j reuses its internal ids when nodes and relationships are deleted. This means that applications using, and relying on internal Neo4j ids, are brittle or at risk of making mistakes. Rather use application generated ids.

Query. 

MATCH n
WHERE id(n)= 1
RETURN n

The corresponding node is returned.

Result

n
1 row

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 match n where id(n) = 1 return n

Relationship by id

Search for nodes by id can be done with the id function in a predicate.

This is not recommended practice. See the section called “Node by id” for more information on the use of Neo4j ids.

Query. 

MATCH ()-[r]->()
WHERE id(r)= 0
RETURN r

The relationship with id 0 is returned.

Result

r
1 row

:ACTED_IN[0]{}

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 match ()-[r]->() where id(r) = 0 return r

Multiple nodes by id

Multiple nodes are selected by specifying them in an IN clause.

Query. 

MATCH n
WHERE id(n) IN [1, 2, 0]
RETURN n

This returns the nodes listed in the IN expression.

Result

n
3 rows

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

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

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`]->_3 create _2-[:`ACTED_IN`]->_4 create _5-[:`DIRECTED`]->_3 create _6-[:`ACTED_IN`]->_3 create _6-[:`ACTED_IN`]->_4 match n where id(n) in [1, 2, 0] return n