15.12. Functions

15.12.1. Predicates
15.12.2. ALL
15.12.3. ANY
15.12.4. NONE
15.12.5. SINGLE
15.12.6. Scalar functions
15.12.7. LENGTH
15.12.8. TYPE
15.12.9. ID
15.12.10. COALESCE
15.12.11. Iterable functions
15.12.12. NODES
15.12.13. RELATIONSHIPS
15.12.14. EXTRACT

Here is a list of the functions in Cypher, seperated into three different sections: Predicates, Scalar functions and Aggregated functions

Graph

cypher-functions-graph.svg

15.12.1. Predicates

Predicates are boolean functions that return true or false for a given set of input. They are most commonly used to filter out subgraphs in the WHERE part of a query.

15.12.2. ALL

Tests whether a predicate holds for all element of this iterable collection.

Syntax: ALL(identifier in iterable WHERE predicate)

Arguments:

  • iterable: An array property, or an iterable symbol, or an iterable function.
  • identifier: This is the identifier that can be used from the predicate.
  • predicate: A predicate that is tested against all items in iterable

Query

START a=node(3), b=node(1)
MATCH p=a-[*1..3]->b
WHERE all(x in nodes(p)
WHERE x.age > 30)
RETURN p

All nodes in the path.

Result

p
1 rows, 2 ms

(3)--[KNOWS,1]-->(5)--[KNOWS,3]-->(1)


15.12.3. ANY

Tests whether a predicate holds for at least one element of this iterable collection.

Syntax: ANY(identifier in iterable WHERE predicate)

Arguments:

  • iterable: An array property, or an iterable symbol, or an iterable function.
  • identifier: This is the identifier that can be used from the predicate.
  • predicate: A predicate that is tested against all items in iterable

Query

START a=node(2)
WHERE any(x in a.array
WHERE x = "one")
RETURN a

All nodes in the path.

Result

a
1 rows, 1 ms

Node[2]{name->"E",age->41,eyes->"blue",array->[Ljava.lang.String;@42101da9}


15.12.4. NONE

Returns true if the predicate holds for no element in the iterable.

Syntax: NONE(identifier in iterable WHERE predicate)

Arguments:

  • iterable: An array property, or an iterable symbol, or an iterable function.
  • identifier: This is the identifier that can be used from the predicate.
  • predicate: A predicate that is tested against all items in iterable

Query

START n=node(3)
MATCH p=n-[*1..3]->b
WHERE NONE(x in nodes(p)
WHERE x.age = 25)
RETURN p

All nodes in the path.

Result

p
2 rows, 2 ms

(3)--[KNOWS,1]-->(5)

(3)--[KNOWS,1]-->(5)--[KNOWS,3]-->(1)


15.12.5. SINGLE

Returns true if the predicate holds for exactly one of the elements in the iterable.

Syntax: SINGLE(identifier in iterable WHERE predicate)

Arguments:

  • iterable: An array property, or an iterable symbol, or an iterable function.
  • identifier: This is the identifier that can be used from the predicate.
  • predicate: A predicate that is tested against all items in iterable

Query

START n=node(3)
MATCH p=n-->b
WHERE SINGLE(var in nodes(p)
WHERE var.eyes = "blue")
RETURN p

All nodes in the path.

Result

p
1 rows, 1 ms

(3)--[KNOWS,0]-->(4)


15.12.6. Scalar functions

Scalar functions return a single value.

15.12.7. LENGTH

To return or filter on the length of a path, use the special property LENGTH

Syntax: LENGTH( iterable )

Arguments:

  • iterable: An iterable, value or function call

Query

START a=node(3)
MATCH p=a-->b-->c
RETURN length(p)

The length of the path p.

Result

LENGTH(p)
3 rows, 3 ms

2

2

2


15.12.8. TYPE

Returns a string representation of the relationship type.

Syntax: TYPE( relationship )

Arguments:

  • relationship: A relationship

Query

START n=node(3)
MATCH (n)-[r]->()
RETURN type(r)

The relationship type of r.

Result

TYPE(r)
2 rows, 1 ms

"KNOWS"

"KNOWS"


15.12.9. ID

Returns the id of the relationship or node

Syntax: ID( property-container )

Arguments:

  • property-container: A node or a relationship

Query

START a=node(3, 4, 5)
RETURN ID(a)

The node id for three nodes.

Result

ID(a)
3 rows, 0 ms

3

4

5


15.12.10. COALESCE

Returns the first non-null value in the list of expressions passed to it.

Syntax: COALESCE( expression [, expression]* )

Arguments:

  • expression: The expression that might return null

Query

START a=node(3)
RETURN coalesce(a.hairColour?, a.eyes?)

Result

COALESCE(a.hairColour,a.eyes)
1 rows, 0 ms

"brown"


15.12.11. Iterable functions

Iterable functions return an iterable of things - nodes in a path, and so on.

15.12.12. NODES

Returns all nodes in a path

Syntax: NODES( path )

Arguments:

  • path: A path

Query

START a=node(3), c=node(2)
MATCH p=a-->b-->c
RETURN NODES(p)

All the nodes in the path p.

Result

NODES(p)
1 rows, 2 ms

List(Node[3], Node[4], Node[2])


15.12.13. RELATIONSHIPS

Returns all relationships in a path

Syntax: RELATIONSHIPS( path )

Arguments:

  • path: A path

Query

START a=node(3), c=node(2)
MATCH p=a-->b-->c
RETURN RELATIONSHIPS(p)

All the nodes in the path p.

Result

RELATIONSHIPS(p)
1 rows, 2 ms

List(Relationship[0], Relationship[4])


15.12.14. EXTRACT

To return a single property, or the value of a function from an iterable of nodes or relationships, you can use EXTRACT. It will go through all enitities in the iterable, and run an expression, and return the results in an iterable with these values. It works like the map method in functional languages such as Lisp and Scala.

Syntax: EXTRACT( identifier in iterable : expression )

Arguments:

  • iterable: An array property, or an iterable identifier, or an iterable function.
  • identifier: The closure will have an identifier introduced in it’s context. Here you decide which identifier to use.
  • expression: This expression will run once per value in the iterable, and produces the result iterable.

Query

START a=node(3), b=node(4), c=node(1)
MATCH p=a-->b-->c
RETURN extract(n in nodes(p) : n.age)

The age property of all nodes in the path.

Result

extract(n in NODES(p) : n.age)
1 rows, 2 ms

List(38, 25, 54)