Predicate functions
Introduction
Predicates are boolean functions that return true or false for a given set of non-null input.
They are most commonly used to filter out paths in the WHERE part of a query.
Example graph
The following graph is used for the examples below:
To recreate it, run the following query against an empty Neo4j database:
CREATE
(keanu:Person {name:'Keanu Reeves', age:58, nationality:'Canadian'}),
(carrie:Person {name:'Carrie Anne Moss', age:55, nationality:'American'}),
(liam:Person {name:'Liam Neeson', age:70, nationality:'Northern Irish'}),
(guy:Person {name:'Guy Pearce', age:55, nationality:'Australian'}),
(kathryn:Person {name:'Kathryn Bigelow', age:71, nationality:'American'}),
(jessica:Person {name:'Jessica Chastain', age:45, address:''}),
(theMatrix:Movie {title:'The Matrix'}),
(keanu)-[:KNOWS]->(carrie),
(keanu)-[:KNOWS]->(liam),
(keanu)-[:KNOWS]->(kathryn),
(kathryn)-[:KNOWS]->(jessica),
(carrie)-[:KNOWS]->(guy),
(liam)-[:KNOWS]->(guy),
(keanu)-[:ACTED_IN]->(theMatrix),
(carrie)-[:ACTED_IN]->(theMatrix)
all()
Syntax |
|
||
Description |
Returns true if the predicate holds for all elements in the given |
||
Arguments |
Name |
Type |
Description |
|
|
A variable that can be used within the |
|
|
|
A predicate must hold for all elements in this list for the function to return |
|
|
|
A predicate that is tested against all items in the given list. |
|
Returns |
|
||
|
|
|
MATCH p = (a)-[*]->(b)
WHERE
a.name = 'Keanu Reeves'
AND b.name = 'Guy Pearce'
AND all(x IN nodes(p) WHERE x.age < 60)
RETURN p
All nodes in the returned paths will have a property age with a value lower than 60:
| p |
|---|
|
Rows: 1 |
all() on an empty LISTWITH [] as emptyList
RETURN all(i in emptyList WHERE true) as allTrue, all(i in emptyList WHERE false) as allFalse
| allTrue | allFalse |
|---|---|
|
|
Rows: 1 |
|
any()
Syntax |
|
||
Description |
Returns true if the predicate holds for at least one element in the given |
||
Arguments |
Name |
Type |
Description |
|
|
A variable that can be used within the |
|
|
|
A predicate must hold for all elements in this list for the function to return |
|
|
|
A predicate that is tested against all items in the given list. |
|
Returns |
|
||
|
|
|
MATCH (p:Person)
WHERE any(nationality IN p.nationality WHERE nationality = 'American')
RETURN p
The query returns the Person nodes with the nationality property value American:
| p |
|---|
|
|
Rows: 2 |
any() on an empty LISTWITH [] as emptyList
RETURN any(i IN emptyList WHERE true) as anyTrue, any(i IN emptyList WHERE false) as anyFalse
| anyTrue | anyFalse |
|---|---|
|
|
Rows: 1 |
|
exists()
Syntax |
|
||
Description |
Returns true if a match for the pattern exists in the graph. |
||
Arguments |
Name |
Type |
Description |
|
|
A pattern to verify the existence of. |
|
Returns |
|
||
|
|
To check if a property is not |
MATCH (p:Person)
RETURN
p.name AS name,
exists((p)-[:ACTED_IN]->()) AS has_acted_in_rel
This query returns the name property of every Person node, along with a boolean (true or false) indicating if those nodes have an ACTED_IN relationship in the graph.
| name | has_acted_in_rel |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Rows: 6 |
|
|
For information about the |
isEmpty()
Syntax |
|
||
Description |
Checks whether a |
||
Arguments |
Name |
Type |
Description |
|
|
A value to be checked for emptiness. |
|
Returns |
|
||
MATCH (p:Person)
WHERE NOT isEmpty(p.nationality)
RETURN p.name, p.nationality
This query returns every Person node in the graph with a set nationality property value (i.e., all Person nodes except for Jessica Chastain):
| p.name | p.nationality |
|---|---|
|
|
|
|
|
|
|
|
|
|
Rows: 5 |
|
MATCH (n)
WHERE isEmpty(properties(n))
RETURN n
Because the example graph contains no empty nodes, nothing is returned:
(no changes, no records)
MATCH (p:Person)
WHERE isEmpty(p.address)
RETURN p.name AS name
The name property of each node that has an empty STRING address property is returned:
| name |
|---|
|
Rows: 1 |
|
The function |
none()
Syntax |
|
||
Description |
Returns true if the predicate holds for no element in the given |
||
Arguments |
Name |
Type |
Description |
|
|
A variable that can be used within the |
|
|
|
A predicate must hold for all elements in this list for the function to return |
|
|
|
A predicate that is tested against all items in the given list. |
|
Returns |
|
||
|
|
|
MATCH p = (n)-[*]->(b)
WHERE
n.name = 'Keanu Reeves'
AND none(x IN nodes(p) WHERE x.age > 60)
RETURN p
No node in the returned path has an age property with a greater value than 60:
| p |
|---|
|
|
Rows: 2 |
none() on an empty LISTWITH [] as emptyList
RETURN none(i IN emptyList WHERE true) as noneTrue, none(i IN emptyList WHERE false) as noneFalse
| noneTrue | noneFalse |
|---|---|
|
|
Rows: 1 |
|
single()
Syntax |
|
||
Description |
Returns true if the predicate holds for exactly one of the elements in the given |
||
Arguments |
Name |
Type |
Description |
|
|
A variable that can be used within the |
|
|
|
A predicate must hold for all elements in this list for the function to return |
|
|
|
A predicate that is tested against all items in the given list. |
|
Returns |
|
||
|
|
|
MATCH p = (n)-->(b)
WHERE
n.name = 'Keanu Reeves'
AND single(x IN nodes(p) WHERE x.nationality = 'Northern Irish')
RETURN p
In every returned path there is exactly one node which has the nationality property value Northern Irish:
| p |
|---|
|
Rows: 1 |
single() on an empty LISTWITH [] as emptyList
RETURN single(i IN emptyList WHERE true) as singleTrue, single(i IN emptyList WHERE false) as singleFalse
| singleTrue | singleFalse |
|---|---|
|
|
Rows: 1 |
|