4.10. Functions in Cypher

4.10.1. Predicates
4.10.2. ALL
4.10.3. ANY
4.10.4. NONE
4.10.5. SINGLE
4.10.6. Scalar functions
4.10.7. LENGTH
4.10.8. TYPE
4.10.9. ID
4.10.10. Iterable functions
4.10.11. NODES
4.10.12. RELATIONSHIPS

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

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

4.10.2. ALL

Tests the predicate closure to see if all items in the iterable match.

Syntax: ALL(iterable, [symbol ⇒] predicate-closure) Arguments: iterable: An array property, or an iterable symbol, or an iterable function. symbol: The closure will have a symbol introduced in it’s context. Here you decide which symbol to use. If you leave the symbol out, the default symbol _ (underscore) will be used. predicate-closure: A predicate that is tested against all items in iterable

Graph

Query

start a=(3), b=(1) match p=a-[^1..3]->b where all(nodes(p), x => x.age > 30) return p

All nodes in the path.

Result

 +-------------------------------------------------------------------+
 | p                                                                 |
 +-------------------------------------------------------------------+
 | Path(Node[3], Relationship[1], Node[5], Relationship[3], Node[1]) |
 +-------------------------------------------------------------------+
 1 rows, 3 ms

4.10.3. ANY

Tests the predicate closure to see if at least one item in the iterable match.

Syntax: ANY(iterable, [symbol ⇒] predicate-closure) Arguments: iterable: An array property, or an iterable symbol, or an iterable function. symbol: The closure will have a symbol introduced in it’s context. Here you decide which symbol to use. If you leave the symbol out, the default symbol _ (underscore) will be used. predicate-closure: A predicate that is tested against all items in iterable

Graph

Query

start a=(3) match p=a-[^1..3]->b where any(nodes(p), x => x.eyes = "blue") return p

All nodes in the path.

Result

 +-------------------------------------------------------------------+
 | p                                                                 |
 +-------------------------------------------------------------------+
 | Path(Node[3], Relationship[0], Node[4])                           |
 | Path(Node[3], Relationship[0], Node[4], Relationship[2], Node[1]) |
 | Path(Node[3], Relationship[0], Node[4], Relationship[4], Node[2]) |
 +-------------------------------------------------------------------+
 3 rows, 5 ms

4.10.4. NONE

Tests the predicate closure to see if no items in the iterable match. If even one matches, the function returns false.

Syntax: NONE(iterable, [symbol ⇒] predicate-closure) Arguments: iterable: An array property, or an iterable symbol, or an iterable function. symbol: The closure will have a symbol introduced in it’s context. Here you decide which symbol to use. If you leave the symbol out, the default symbol _ (underscore) will be used. predicate-closure: A predicate that is tested against all items in iterable

Graph

Query

start n=(3) match p=n-[^1..3]->b where NONE(nodes(p), x => x.age = 25) return p

All nodes in the path.

Result

 +-------------------------------------------------------------------+
 | p                                                                 |
 +-------------------------------------------------------------------+
 | Path(Node[3], Relationship[1], Node[5])                           |
 | Path(Node[3], Relationship[1], Node[5], Relationship[3], Node[1]) |
 +-------------------------------------------------------------------+
 2 rows, 5 ms

4.10.5. SINGLE

Returns true if the closure predicate matches exactly one of the items in the iterable.

Syntax: SINGLE(iterable, [symbol ⇒] predicate-closure) Arguments: iterable: An array property, or an iterable symbol, or an iterable function. symbol: The closure will have a symbol introduced in it’s context. Here you decide which symbol to use. If you leave the symbol out, the default symbol _ (underscore) will be used. predicate-closure: A predicate that is tested against all items in iterable

Graph

Query

start n=(3) match p=n-->b where SINGLE(nodes(p), _.eyes = "blue") return p

All nodes in the path.

Result

 +-----------------------------------------+
 | p                                       |
 +-----------------------------------------+
 | Path(Node[3], Relationship[0], Node[4]) |
 +-----------------------------------------+
 1 rows, 2 ms

4.10.6. Scalar functions

Scalar functions return a single value.

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

Graph

Query

start a=(3) match p=a-->b-->c return length(p)

The length of the path p.

Result

 +-----------+
 | LENGTH(p) |
 +-----------+
 | 5         |
 | 5         |
 | 5         |
 +-----------+
 3 rows, 4 ms

4.10.8. TYPE

Returns a string representation of the relationship type.

Syntax: TYPE( relationship ) Arguments: relationship: A relationship

Graph

Query

start n=(3) match (n)-[r]->() return type(r)

The relationship type of r.

Result

 +---------+
 | TYPE(r) |
 +---------+
 | KNOWS   |
 | KNOWS   |
 +---------+
 2 rows, 1 ms

4.10.9. ID

Returns the id of the relationship or node

Syntax: ID( property-container ) Arguments: property-container: A node or a relationship

Graph

Query

start a=(3, 4, 5) return ID(a)

The node id for three nodes.

Result

 +-------+
 | ID(a) |
 +-------+
 | 3     |
 | 4     |
 | 5     |
 +-------+
 3 rows, 0 ms

4.10.10. Iterable functions

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

4.10.11. NODES

Returns all nodes in a path

Syntax: NODES( path ) Arguments: path: A path

Graph

Query

start a=(3), c=(2) match p=a-->b-->c return NODES(p)

All the nodes in the path p.

Result

 +---------------------------------+
 | NODES(p)                        |
 +---------------------------------+
 | List(Node[3], Node[4], Node[2]) |
 +---------------------------------+
 1 rows, 2 ms

4.10.12. RELATIONSHIPS

Returns all relationships in a path

Syntax: RELATIONSHIPS( path ) Arguments: path: A path

Graph

Query

start a=(3), c=(2) match p=a-->b-->c return RELATIONSHIPS(p)

All the nodes in the path p.

Result

 +----------------------------------------+
 | RELATIONSHIPS(p)                       |
 +----------------------------------------+
 | List(Relationship[0], Relationship[4]) |
 +----------------------------------------+
 1 rows, 3 ms