Skip to content
Blog

Recursive relationship functions

FunctionDescription
NODESreturns all nodes from a path
RELSreturns all rels from a path
PROPERTIESreturn a given property from nodes or relationships
IS_TRAILcheck if a path contains repeated relationships
IS_ACYCLICcheck if a path contains repeated nodes
LENGTHreturns the number of relationships (path length) in a path
COSTreturns the cost of a weighted path

NODES

Returns all nodes from a path.

Input typeOutput type
RECURSIVE_RELLIST[NODE]
MATCH p = (a:User)-[:Follows*1..2]->(:User)
WHERE a.name = 'Adam'
RETURN nodes(p);

Output:

------------------------------------------------------------------------------------
| NODES(p) |
------------------------------------------------------------------------------------
| [{_ID: 0:0, _LABEL: User, name: Adam, age: 30},{_ID: 0:1, _LABEL: User, name:... |
------------------------------------------------------------------------------------
| [{_ID: 0:0, _LABEL: User, name: Adam, age: 30},{_ID: 0:1, _LABEL: User, name:... |
------------------------------------------------------------------------------------
| [{_ID: 0:0, _LABEL: User, name: Adam, age: 30},{_ID: 0:2, _LABEL: User, name:... |
------------------------------------------------------------------------------------
| [{_ID: 0:0, _LABEL: User, name: Adam, age: 30},{_ID: 0:2, _LABEL: User, name:... |
------------------------------------------------------------------------------------

RELS

Returns all relationships from a path.

Input typeOutput type
RECURSIVE_RELLIST[REL]
MATCH p = (a:User)-[:Follows*1..2]->(:User)
WHERE a.name = 'Adam'
RETURN rels(p);

Output:

------------------------------------------------------------------------------------
| RELS(p) |
------------------------------------------------------------------------------------
| [(0:0)-{_LABEL: Follows, _ID: 2:0, since: 2020}->(0:1)] |
------------------------------------------------------------------------------------
| [(0:0)-{_LABEL: Follows, _ID: 2:0, since: 2020}->(0:1),(0:1)-{_LABEL: Follows... |
------------------------------------------------------------------------------------
| [(0:0)-{_LABEL: Follows, _ID: 2:1, since: 2020}->(0:2)] |
------------------------------------------------------------------------------------
| [(0:0)-{_LABEL: Follows, _ID: 2:1, since: 2020}->(0:2),(0:2)-{_LABEL: Follows... |
------------------------------------------------------------------------------------

PROPERTIES

Return a given property from nodes or relationships.

Input typeOutput type
LIST[NODE/REL], STRINGLIST[ANY]
MATCH p = (a:User)-[:Follows*1..2]->(:User)
WHERE a.name = 'Adam'
RETURN properties(nodes(p), 'name') AS name, properties(rels(p), 'since') AS since;

Output:

--------------------------------------
| name | since |
--------------------------------------
| [Adam,Karissa] | [2020] |
--------------------------------------
| [Adam,Karissa,Zhang] | [2020,2021] |
--------------------------------------
| [Adam,Zhang] | [2020] |
--------------------------------------
| [Adam,Zhang,Noura] | [2020,2022] |
--------------------------------------

IS_TRAIL

Check if a path contains repeated relationships.

Input typeOutput type
RECURSIVE_RELBOOLEAN
MATCH p = (a:User)-[:Follows*2..2]-(b:User)
WHERE a.name='Adam'
RETURN properties(rels(p), '_id'), is_trail(p);

Output

-----------------------------------------
| PROPERTIES(RELS(p),_id) | IS_TRAIL(p) |
-----------------------------------------
| [2:1,2:1] | False |
-----------------------------------------
| [2:0,2:0] | False |
-----------------------------------------
| [2:1,2:2] | True |
-----------------------------------------
| [2:0,2:2] | True |
-----------------------------------------
| [2:1,2:3] | True |
-----------------------------------------

IS_ACYCLIC

Check if a path contains repeated nodes.

Input typeOutput type
RECURSIVE_RELBOOLEAN
MATCH p = (a:User)-[:Follows*2..2]-(b:User)
WHERE a.name='Adam'
RETURN properties(nodes(p), 'name'), is_acyclic(p);

Output:

---------------------------------------------
| PROPERTIES(NODES(p),name) | IS_ACYCLIC(p) |
---------------------------------------------
| [Adam,Zhang,Adam] | False |
---------------------------------------------
| [Adam,Karissa,Adam] | False |
---------------------------------------------
| [Adam,Zhang,Karissa] | True |
---------------------------------------------
| [Adam,Karissa,Zhang] | True |
---------------------------------------------
| [Adam,Zhang,Noura] | True |
---------------------------------------------

LENGTH

Return the number of relationships (path length) in a path.

MATCH p = (a:User)-[f:Follows*1..2]->(b:User)
RETURN LENGTH(p);
----------------
| LENGTH(p) |
----------------
| 1 |
----------------
| 2 |
----------------
| 1 |
----------------
| 2 |
----------------

The LENGTH function when applied to a recursive relationship is equivalent to SIZE(rels(p)).

COST

Return the cost of a weighted path. This function only works for WSHORTEST and ALL WSHORTEST.

Input typeOutput type
RECURSIVE_RELDOUBLE

Example

MATCH (a:User)-[e:Follows]->(b:User) RETURN a.name, e.*, b.name;
┌─────────┬─────────┬───────────┬─────────┐
│ a.name │ e.since │ e.score │ b.name │
│ STRING │ INT64 │ DOUBLE │ STRING │
├─────────┼─────────┼───────────┼─────────┤
│ Adam │ 2020 │ 5.000000 │ Karissa │
│ Adam │ 2020 │ 20.000000 │ Zhang │
│ Karissa │ 2021 │ 6.000000 │ Zhang │
│ Zhang │ 2022 │ 22.000000 │ Noura │
└─────────┴─────────┴───────────┴─────────┘
MATCH p=(a:User)-[e:Follows* WSHORTEST(score)]->(b:User)
WHERE a.name='Adam' RETURN properties(nodes(p), 'name'), cost(e);
┌────────────────────────────┬───────────┐
│ PROPERTIES(NODES(p),name) │ e_cost │
│ STRING[] │ DOUBLE │
├────────────────────────────┼───────────┤
│ [Adam,Karissa,Zhang,Noura] │ 33.000000 │
│ [Adam,Karissa,Zhang] │ 11.000000 │
│ [Adam,Karissa] │ 5.000000 │
└────────────────────────────┴───────────┘