16.2. Expand operators

Thes operators explore the graph by expanding graph patterns.

Expand All

Given a start node, expand-all will follow relationships coming in or out, depending on the pattern relationship. Can also handle variable length pattern relationships.

Query 

MATCH (p:Person { name: "me" })-[:FRIENDS_WITH]->(fof)
RETURN fof

Query Plan 

+-----------------+----------------+------+---------+--------------------+----------------------------+
| Operator        | Estimated Rows | Rows | DB Hits | Variables          | Other                      |
+-----------------+----------------+------+---------+--------------------+----------------------------+
| +ProduceResults |              0 |    1 |       0 | fof                | fof                        |
| |               +----------------+------+---------+--------------------+----------------------------+
| +Expand(All)    |              0 |    1 |       2 | anon[30], fof -- p | (p)-[:FRIENDS_WITH]->(fof) |
| |               +----------------+------+---------+--------------------+----------------------------+
| +NodeIndexSeek  |              1 |    1 |       2 | p                  | :Person(name)              |
+-----------------+----------------+------+---------+--------------------+----------------------------+

Total database accesses: 4

Expand Into

When both the start and end node have already been found, expand-into is used to find all connecting relationships between the two nodes.

Query 

MATCH (p:Person { name: "me" })-[:FRIENDS_WITH]->(fof)-->(p)
RETURN fof

Query Plan 

+-----------------+----------------+------+---------+------------------------------+----------------------------+
| Operator        | Estimated Rows | Rows | DB Hits | Variables                    | Other                      |
+-----------------+----------------+------+---------+------------------------------+----------------------------+
| +ProduceResults |              0 |    0 |       0 | fof                          | fof                        |
| |               +----------------+------+---------+------------------------------+----------------------------+
| +Filter         |              0 |    0 |       0 | anon[30], anon[53], fof, p   | NOT(anon[30] == anon[53])  |
| |               +----------------+------+---------+------------------------------+----------------------------+
| +Expand(Into)   |              0 |    0 |       0 | anon[30] -- anon[53], fof, p | (p)-[:FRIENDS_WITH]->(fof) |
| |               +----------------+------+---------+------------------------------+----------------------------+
| +Expand(All)    |              0 |    0 |       1 | anon[53], fof -- p           | (p)<--(fof)                |
| |               +----------------+------+---------+------------------------------+----------------------------+
| +NodeIndexSeek  |              1 |    1 |       2 | p                            | :Person(name)              |
+-----------------+----------------+------+---------+------------------------------+----------------------------+

Total database accesses: 3

Optional Expand All

Optional expand traverses relationships from a given node, and makes sure that predicates are evaluated before producing rows.

If no matching relationships are found, a single row with NULL for the relationship and end node variable is produced.

Query 

MATCH (p:Person)
OPTIONAL MATCH (p)-[works_in:WORKS_IN]->(l)
WHERE works_in.duration > 180
RETURN p, l

Query Plan 

+----------------------+----------------+------+---------+------------------+------------------------------+
| Operator             | Estimated Rows | Rows | DB Hits | Variables        | Other                        |
+----------------------+----------------+------+---------+------------------+------------------------------+
| +ProduceResults      |             14 |   15 |       0 | l, p             | p, l                         |
| |                    +----------------+------+---------+------------------+------------------------------+
| +OptionalExpand(All) |             14 |   15 |      44 | l, works_in -- p | (p)-[works_in:WORKS_IN]->(l) |
| |                    +----------------+------+---------+------------------+------------------------------+
| +NodeByLabelScan     |             14 |   14 |      15 | p                | :Person                      |
+----------------------+----------------+------+---------+------------------+------------------------------+

Total database accesses: 59