The MATCH operation finds all occurrences of a graph pattern in your data.
You describe nodes in parentheses and edges in square brackets with arrows that
indicate the direction, so that a pattern resembles a drawing of the graph
shape you are looking for:
MATCH (a:persons) -[k:knows]-> (b:persons)
RETURN { from: a.name, to: b.name }Every node and edge in a pattern names the collection it belongs to (its
label). For each combination of documents that fits the pattern, MATCH
binds the pattern variables, like a, k, and b in the example above,
and you can use these variables in the subsequent operations of the query.
The MATCH operation is an experimental feature. It is disabled by default and
you need to enable it per query. The syntax and behavior may change in future
versions in ways that are not backward-compatible. Don’t use it in production.
Enable the MATCH operation
To use MATCH, set the matchStatement query option to "experimental".
Otherwise, the query fails with error 9 (not implemented).
In arangosh, pass the option in the third argument of db._query():
db._query(`
MATCH (a:persons) -[:knows]-> (b:persons)
RETURN [a._key, b._key]
`, {}, { matchStatement: "experimental" }).toArray();Via the HTTP API, set the option in the options object of the request body
for POST /_api/cursor:
{
"query": "MATCH (a:persons) -[:knows]-> (b:persons) RETURN [a._key, b._key]",
"options": { "matchStatement": "experimental" }
}
The keyword MATCH is reserved regardless of the option. See
Keywords if you use match as a name
in your queries.
Syntax
MATCH pattern [, pattern ... ]
[OPTIONS {}]A MATCH operation consists of one or more comma-separated patterns.
A pattern is a sequence of node patterns connected by edge patterns, optionally
assigned to a path variable:
[pathVar =] node [ edge node ... ]pathVar(optional): a variable name for the entire path that the pattern matches. See Use path variables.OPTIONS {}(optional): reserved for future options. Only an empty object is accepted at this time. Any attribute raises an error.
In the following, all parentheses ( ), square brackets [ ], and curly
braces { } are part of the syntax.
Node patterns
A node pattern is enclosed in parentheses. Inside, it consists of the following parts, in this order:
| Part | Required | Example | Description |
|---|---|---|---|
nodeVar | no | p | A variable name to access the matched node after the MATCH operation. |
:collection | yes | :persons | The document collection that the node needs to be stored in (its label). You can use a collection bind parameter like :@@coll. Write the colon even if you omit the variable name. |
{ attribute: value, … } | no | { name: "Alice" } | Attribute values that the node needs to have. Each pair is an equality condition. |
WHERE condition | no | WHERE p.age > 30 | An arbitrary AQL condition that the node needs to fulfill. |
RETURN projection, … | no | RETURN name, age | The attributes to keep in the variable. See Projections. |
Examples, from minimal to complete:
(:persons)
(p:persons)
(p:persons { name: "Alice" })
(p:persons { name: "Alice" } WHERE p.age > 30 RETURN name, age)Alternatively, a node pattern can only consist of the name of a variable that
is already defined, like (p). See Reuse variables.
Edge patterns
An edge pattern is enclosed in square brackets. Dashes and an arrowhead
around the brackets indicate the direction, with ... standing for the content
of the brackets:
| Direction | Edge pattern | Usage | Matches edges |
|---|---|---|---|
| Outbound | -[ ... ]-> | (a) -[ ... ]-> (b) | from a to b |
| Inbound | <-[ ... ]- | (a) <-[ ... ]- (b) | from b to a |
| Any | -[ ... ]- | (a) -[ ... ]- (b) | in either direction |
Inside the brackets, an edge pattern consists of the following parts, in this order:
| Part | Required | Example | Description |
|---|---|---|---|
edgeVar | no | k | A variable name to access the matched edge after the MATCH operation. |
:edgeCollection | yes | :knows | The edge collection that the edge needs to be stored in (its label). You can list multiple edge collections separated by |, like :knows|, to match edges from any of them. You can use collection bind parameters like :@@coll. Write the colon even if you omit the variable name. |
{ attribute: value, … } | no | { since: 2020 } | Attribute values that the edge needs to have. Each pair is an equality condition. |
WHERE condition | no | WHERE k.weight > 0.5 | An arbitrary AQL condition that the edge needs to fulfill. |
RETURN projection, … | no | RETURN since | The attributes to keep in the variable. See Projections. |
Examples, from minimal to complete:
-[:knows]->
<-[k:knows]-
<-[k:knows|follows]-
-[k:knows|follows { since: 2020 }]-
-[k:knows|follows { since: 2020 } WHERE k.weight > 0.5 RETURN since]-Variable-length edge patterns
A variable-length edge pattern follows multiple edges. It uses the same brackets and arrows as an edge pattern but consists of the following parts:
| Part | Required | Example | Description |
|---|---|---|---|
pathVar | no | p | A variable name to access the path that the edges form after the MATCH operation. |
:edgeCollection | yes | :knows | The edge collection that the edges need to be stored in (its label). You can list multiple edge collections separated by |, like :knows|, to follow edges from any of them. You can use collection bind parameters like :@@coll. Write the colon even if you omit the variable name. |
* min..max | yes | * 1..3 | An asterisk followed by the minimum and maximum number of edges to follow. |
Examples:
-[:knows * 1..3]->
-[p:knows * 2..2]-
<-[p:knows|follows * 1..2]-If min and max are equal, like * 2..2, the pattern follows exactly this
many edges. This is a shorter way to write a chain of edge and node patterns,
but there are differences. See
Match a fixed number of edges.
Unlike in edge patterns, you cannot specify attribute values, WHERE
conditions, or projections. The variable refers to the entire path rather than
an edge, so it would be ambiguous whether such conditions need to be fulfilled
by every edge of the path, by any edge, or by the path itself. The following
variable-length edge patterns are therefore invalid:
-[p:knows * 1..3 { since: 2020 }]-> // invalid
-[p:knows * 1..3 WHERE p.weight > 0.5]-> // invalid
-[p:knows * 1..3 RETURN since]-> // invalid
To set conditions on the edges and nodes of the path, use a FILTER operation
instead. See Filter variable-length paths.
Usage
The examples on this page use the knows_graph
example graph. It consists of a persons node collection with the people Alice,
Bob, Charlie, Dave, and Eve, and a knows edge collection with the following
edges:
- Alice knows Bob
- Bob knows Charlie
- Bob knows Dave
- Eve knows Alice
- Eve knows Bob
Match nodes
The simplest pattern is a single node pattern. It iterates over a collection,
similar to a FOR loop.
Return the names of all persons:
MATCH (p:persons)
RETURN p.name{
"matchStatement": "experimental"
}Show output
[
"Alice",
"Bob",
"Charlie",
"Dave",
"Eve"
]You can restrict the matched documents with attribute values in curly braces
or with a WHERE condition, or with both.
Return the persons whose name is Alice or starts with D:
MATCH (p:persons WHERE p.name == "Alice" OR p.name LIKE "D%")
RETURN p.name{
"matchStatement": "experimental"
}Show output
[
"Alice",
"Dave"
]Attribute values in curly braces are equality conditions on top-level attributes. The following two node patterns are equivalent:
(p:persons { name: "Alice" })
(p:persons WHERE p.name == "Alice")The values can be arbitrary expressions, including bind parameters and references to pattern variables that are declared earlier. See Refer to other pattern variables.
Match edges
To find nodes that are connected by edges, combine node patterns with edge patterns. The arrow indicates the direction of the edge:
(a) -[ ... ]-> (b): an edge fromatob(outbound froma)(a) <-[ ... ]- (b): an edge frombtoa(inbound toa)(a) -[ ... ]- (b): an edge betweenaandbin either direction
An edge pattern with arrowheads on both sides (<-[ ... ]->) is invalid.
Return the people that Eve knows, together with the edges that connect them:
MATCH (a:persons { _key: "eve" }) -[k:knows]-> (b:persons)
RETURN { from: a.name, edge: k._id, to: b.name }{
"matchStatement": "experimental"
}Show output
[
{
"from" : "Eve",
"edge" : "knows/17474",
"to" : "Alice"
},
{
"from" : "Eve",
"edge" : "knows/17476",
"to" : "Bob"
}
]If you follow edges in any direction, then each edge is matched once from each side.
Find everyone that Bob knows or who knows Bob:
MATCH (a:persons { name: "Bob" }) -[:knows]- (b:persons)
RETURN b.name{
"matchStatement": "experimental"
}Show output
[
"Charlie",
"Dave",
"Alice",
"Eve"
]Edge patterns support attribute values in curly braces and WHERE conditions
like node patterns.
Refer to other pattern variables
The attribute values and WHERE conditions of node and edge patterns can refer
to the variables of elements that are declared earlier, either in the same
pattern or in a preceding pattern of the same MATCH operation. For example,
the condition of an edge can compare attributes of the edge with attributes of
the node it starts from.
The edges of the knows_graph store the document key of the node they point
away from in a vertex attribute.
Follow the edges of Bob in any direction but only keep the ones that point
away from Bob, by comparing the vertex attribute of the edges with the key
of the node that is matched first. Compared to the previous example, only the
people that Bob knows remain:
MATCH (a:persons { name: "Bob" }) -[k:knows WHERE k.vertex == a._key]- (b:persons)
RETURN b.name{
"matchStatement": "experimental"
}Show output
[
"Charlie",
"Dave"
]You cannot refer to variables of elements that come later. For example,
(a:persons WHERE a.name == b.name) -[:knows]- (b:persons) is invalid because
b is not declared yet when the condition of a is evaluated. AQL then
interprets b as the name of a collection or View, and the query fails with the
error collection or view not found: b. If a collection with this name exists,
the query fails with a different error at runtime, or, for example with
LENGTH(b), uses the collection and returns unintended results. Move the condition to the
later element instead, like (a:persons) -[:knows]- (b:persons WHERE b.name == a.name).
You can specify multiple edge collections by separating them with |, like
-[e:knows|follows]->. Edges from any of the collections can then match.
Match longer patterns
You can chain node and edge patterns to describe paths with multiple hops.
Find the people who are known by somebody that Eve knows:
MATCH (a:persons { _key: "eve" }) -[:knows]-> (b:persons) -[:knows]-> (c:persons)
RETURN [a.name, b.name, c.name]{
"matchStatement": "experimental"
}Show output
[
[
"Eve",
"Alice",
"Bob"
],
[
"Eve",
"Bob",
"Charlie"
],
[
"Eve",
"Bob",
"Dave"
]
]Match variable-length paths
To follow a variable number of edges, append * min..max to the edge
collections.
Find everyone that Eve can reach with one to three hops:
MATCH (a:persons { _key: "eve" }) -[p:knows * 1..3]-> (b:persons)
RETURN { to: b.name, via: p.vertices[*].name }{
"matchStatement": "experimental"
}Show output
[
{
"to" : "Bob",
"via" : [
"Eve",
"Bob"
]
},
{
"to" : "Dave",
"via" : [
"Eve",
"Bob",
"Dave"
]
},
{
"to" : "Charlie",
"via" : [
"Eve",
"Bob",
"Charlie"
]
},
{
"to" : "Alice",
"via" : [
"Eve",
"Alice"
]
},
{
"to" : "Bob",
"via" : [
"Eve",
"Alice",
"Bob"
]
},
{
"to" : "Dave",
"via" : [
"Eve",
"Alice",
"Bob",
"Dave"
]
},
{
"to" : "Charlie",
"via" : [
"Eve",
"Alice",
"Bob",
"Charlie"
]
}
]The variable of a variable-length edge pattern (p in the example above) holds
the path that the edges form, not a single edge. The path is an object with an
edges and a vertices attribute (as well as a weights attribute), like
the path variable of a graph traversal.
Note the following about variable-length edge patterns:
minandmaxneed to be positive integers, andmincannot be greater thanmax. You can use bind parameters and constant expressions. Aminof0is not supported and raises a parse error (zero-hop variable-length relationships are not supported).- A pattern with
* 1..1is treated like an edge pattern without* min..max. Its variable holds the edge document rather than a path, and an edge that connects a node to itself (a self-loop) is only matched once for the any-direction pattern-[ ]-. - You cannot specify attribute values,
WHEREconditions, or projections for variable-length edge patterns. This is not supported by design: the variable holds a path rather than an edge, so a condition likeWHERE p.weight > 0.5would be ambiguous. It could mean that every edge needs to fulfill it, that any edge needs to fulfill it, or that it applies to the path object. See Filter variable-length paths for how to state such conditions explicitly. - The collection labels of the node patterns only apply to the nodes at both ends. The nodes in between can be stored in any collection that the edges point to.
- A path never contains the same edge twice, but it can contain the same node multiple times.
Match a fixed number of edges
If you set min and max to the same value, a variable-length edge pattern
follows exactly this number of edges. You can use this to avoid repeating the
same edge and node patterns. For example, the following two patterns both match
paths with two knows edges from Bob:
(a:persons { name: "Bob" }) -[:knows]- (b:persons) -[:knows]- (c:persons)
(a:persons { name: "Bob" }) -[p:knows * 2..2]- (c:persons)The patterns are not equivalent, however:
- A chain of edge patterns can match the same edge multiple times. In the example above, the first pattern also matches Bob → Charlie → Bob via the same edge in both directions, whereas the variable-length edge pattern only matches paths without repeated edges, like Bob → Eve → Alice.
- In a chain, every node needs to fulfill the label of its node pattern. In a variable-length edge pattern, only the labels of the nodes at both ends apply.
- In a chain, you can specify a variable, attribute values,
WHEREconditions, and projections for every node and edge. - A chain is executed as joins, whereas a variable-length edge pattern is
executed as a graph traversal. See How
MATCHis executed.
Filter variable-length paths
To set conditions that every edge or every node of a variable-length path needs
to fulfill, add a FILTER operation after the MATCH operation and use the
array comparison operators
on the path variable.
The following examples use the routeplanner
example graph. It has germanCity and frenchCity node collections and
germanHighway, frenchHighway, and internationalHighway edge collections.
The edges have a distance attribute.
Find routes from Berlin to any French city with up to three highway sections but only keep routes where every section is shorter than 1000 kilometers:
MATCH (a:germanCity { _key: "Berlin" })
-[p:germanHighway|frenchHighway|internationalHighway * 1..3]->
(b:frenchCity)
FILTER p.edges[*].distance ALL < 1000
LET distance = SUM(p.edges[*].distance)
SORT distance
RETURN { route: p.vertices[*]._key, distance }{
"matchStatement": "experimental"
}Show output
[
{
"route" : [
"Berlin",
"Hamburg",
"Paris"
],
"distance" : 1300
},
{
"route" : [
"Berlin",
"Cologne",
"Paris"
],
"distance" : 1400
},
{
"route" : [
"Berlin",
"Hamburg",
"Cologne",
"Paris"
],
"distance" : 1450
},
{
"route" : [
"Berlin",
"Cologne",
"Lyon"
],
"distance" : 1550
},
{
"route" : [
"Berlin",
"Hamburg",
"Cologne",
"Lyon"
],
"distance" : 1600
},
{
"route" : [
"Berlin",
"Hamburg",
"Paris",
"Lyon"
],
"distance" : 1850
},
{
"route" : [
"Berlin",
"Cologne",
"Paris",
"Lyon"
],
"distance" : 1950
}
]The labels of the node patterns only apply to the start and end node, so the
nodes in between can be stored in any of the node collections. To restrict
them to certain collections, check the collection names of the nodes in between
with PARSE_COLLECTION().
SLICE(p.vertices, 1, -1) returns the nodes of the path without the first and
the last one.
Find routes from Berlin to Lyon that only pass through German cities:
MATCH (a:germanCity { _key: "Berlin" })
-[p:germanHighway|frenchHighway|internationalHighway * 1..3]->
(b:frenchCity { _key: "Lyon" })
FILTER SLICE(p.vertices, 1, -1)[* RETURN PARSE_COLLECTION(CURRENT)] ALL == "germanCity"
RETURN p.vertices[*]._key{
"matchStatement": "experimental"
}Show output
[
[
"Berlin",
"Lyon"
],
[
"Berlin",
"Hamburg",
"Lyon"
],
[
"Berlin",
"Hamburg",
"Cologne",
"Lyon"
],
[
"Berlin",
"Cologne",
"Lyon"
]
]Routes via Paris, like Berlin → Paris → Lyon, are not part of the result
because Paris is stored in the frenchCity collection.
To restrict the edges to certain collections, list only these collections in the edge pattern instead of filtering the path.
Use path variables
To get access to the entire path that a pattern describes, assign the pattern
to a variable. The path variable holds an object with an edges and a
vertices attribute, with the matched edges and nodes in the order of the path.
Assign a two-hop pattern to the variable path and return the names of the
nodes along each path:
MATCH path = (a:persons { _key: "eve" }) -[:knows]-> (b:persons) -[:knows]-> (c:persons)
RETURN path.vertices[*].name{
"matchStatement": "experimental"
}Show output
[
[
"Eve",
"Alice",
"Bob"
],
[
"Eve",
"Bob",
"Charlie"
],
[
"Eve",
"Bob",
"Dave"
]
]Path variables also work with variable-length edge patterns. The path then includes the nodes and edges of all segments.
You cannot refer to a path variable in a WHERE condition of its own pattern,
like MATCH path = (a:persons WHERE LENGTH(path.edges) > 1) -[:knows]-> (b:persons).
Such queries raise a parse error (MATCH path variable ‘…’ cannot be
referenced in its own pattern WHERE expression). Use a FILTER operation after
the MATCH operation instead.
Reuse variables
A node pattern with a variable name and a label, like (v:persons), declares
the variable v. You cannot declare the same variable twice. To refer to a node
again in the same MATCH operation, use its variable name without a label,
like (v).
For example, the following query finds nodes with an edge that points to the node itself (a self-loop):
MATCH (v:persons) -[e:knows]-> (v)
RETURN eWriting (v:persons) -[e:knows]-> (v:persons) instead raises a parse error
because it attempts to declare v a second time.
You cannot specify attribute values, WHERE conditions, or projections for a
node pattern that refers to an existing variable. The variable already refers
to a specific node, so its conditions belong to where the variable is declared.
Otherwise, the conditions for the same node would be split up across the pattern
and could even contradict each other, and a projection would change what the
variable holds in the middle of the pattern. Such node patterns raise a parse
error (filters or constraints are not allowed on MATCH pattern reference ‘…’):
// invalid: can never match because 'a' is the same node in both places
MATCH (a:persons { name: "Alice" }) -[:knows]-> (b:persons) -[:knows]-> (a { name: "Bob" })
// invalid: the conditions for 'a' are split up
MATCH (a:persons WHERE a.name LIKE "A%") -[:knows]-> (b:persons) -[:knows]-> (a WHERE LENGTH(a.name) == 5)
// valid: all conditions for 'a' are in its declaration
MATCH (a:persons WHERE a.name LIKE "A%" AND LENGTH(a.name) == 5) -[:knows]-> (b:persons) -[:knows]-> (a)You can also refer to variables that you defined before the MATCH operation,
for example, by a FOR loop or a LET operation. The variable needs to
contain a document (or an object with an _id attribute). To set conditions
for such a node, use a FILTER operation before the MATCH operation. This
way, only the nodes that fulfill the conditions are used to match the pattern.
Iterate over all persons but only match the pattern for Alice and Eve:
FOR start IN persons
FILTER start.name IN ["Alice", "Eve"]
MATCH (start) -[:knows]-> (b:persons)
RETURN [start.name, b.name]{
"matchStatement": "experimental"
}Show output
[
[
"Alice",
"Bob"
],
[
"Eve",
"Alice"
],
[
"Eve",
"Bob"
]
]A node pattern without a label needs to refer to a variable that is already
defined, either earlier in the MATCH operation or before it. Otherwise, the
query fails with a parse error (MATCH vertex variable ‘…’ needs a label or
has to be bound earlier).
Edge variables always need to be new. Using an edge variable name that is already defined raises a parse error.
Note that variable names cannot be the same as the collection names that you
use as labels, like (persons:persons). Labels need to be names of collections
and not of variables or Views. Node patterns need document collections as labels
and edge patterns edge collections. Otherwise, the query fails with an error.
Match multiple patterns
You can specify multiple patterns separated by commas. All patterns need to match, and you can connect them by referring to the same variables.
Find pairs of people w and x who both know v:
MATCH (w:persons) -[:knows]-> (v:persons), (x:persons) -[:knows]-> (v)
FILTER w != x
RETURN [w.name, x.name, v.name]{
"matchStatement": "experimental"
}Show output
[
[
"Alice",
"Eve",
"Bob"
],
[
"Eve",
"Alice",
"Bob"
]
]If the patterns don’t share any variables, the result is the combination of every match of the first pattern with every match of the second pattern.
Projections
You can limit the attributes that a pattern variable holds by adding RETURN
followed by a comma-separated list of attributes at the end of a node or edge
pattern. This reduces the size of the variables and of the query result.
Only keep the name attribute of the persons, add an initial attribute to
Alice, and only keep the vertex attribute of the edges:
MATCH (a:persons { _key: "alice" } RETURN name, initial = LEFT(a.name, 1))
-[k:knows RETURN vertex]-> (b:persons RETURN name)
RETURN [a, k, b]{
"matchStatement": "experimental"
}Show output
[
[
{
"_id" : "persons/alice",
"name" : "Alice",
"initial" : "A"
},
{
"_id" : "knows/20323",
"_from" : "persons/alice",
"_to" : "persons/bob",
"vertex" : "alice"
},
{
"_id" : "persons/bob",
"name" : "Bob"
}
]
]A projection list can contain the following items:
- An attribute name, like
name, to keep this top-level attribute. - A nested attribute path, like
profile.name, to keep a sub-attribute. The result contains the parent objects, like{ "profile": { "name": ... } }. If you specify paths that overlap, likeprofileandprofile.name, the shorter path takes precedence. - A quoted attribute name, like
"profile.name", to keep a top-level attribute whose name contains a dot. - An assignment, like
initial = LEFT(a.name, 1), to add an attribute with the result of an expression. The expression can refer to the pattern variables.
Note the following about projections:
- The
_idattribute of nodes as well as the_id,_from, and_toattributes of edges are always kept. Other system attributes like_keyare only kept if you list them. - Attributes that you list but that don’t exist in a document are set to
null. - You cannot assign to system attributes like
_id = ..., and you cannot use the same attribute name more than once. - Attribute values in curly braces and
WHEREconditions are evaluated against the full documents, so they can use attributes that you don’t keep. This also applies to the conditions of other patterns of the sameMATCHoperation. - If you access an attribute that a projection didn’t keep after the
MATCHoperation, the value isnulland the query raises a warning (error code1596, attribute ‘…’ was excluded by MATCH projection on variable ‘…’). - The nodes and edges of a path variable are the projected documents. This includes the nodes at both ends of a variable-length edge pattern. The nodes and edges in between are always full documents because you cannot specify projections for them.
Use bind parameters for collections
You can specify the labels of node and edge patterns with collection bind
parameters (@@name). Value bind parameters (@name) are not supported in
this place.
Find the French cities that are directly connected to Berlin, with the
collections of the routeplanner graph specified via bind parameters:
MATCH (a:@@startColl { _key: @key }) -[:@@edgeColl1|@@edgeColl2]-> (b:@@targetColl)
RETURN b._key{
"@edgeColl1": "germanHighway",
"@edgeColl2": "internationalHighway",
"@startColl": "germanCity",
"@targetColl": "frenchCity",
"key": "Berlin"
}{
"matchStatement": "experimental"
}Show output
[
"Paris",
"Lyon"
]Combine MATCH with other operations
MATCH is a high-level operation like FOR. You can combine it with other
operations like FILTER, SORT, LIMIT, COLLECT, and RETURN, and you can
use it inside of FOR loops and subqueries.
Count how many people know each person and sort by this number:
MATCH (a:persons) -[:knows]-> (b:persons)
COLLECT name = b.name WITH COUNT INTO knownBy
SORT knownBy DESC
RETURN { name, knownBy }{
"matchStatement": "experimental"
}Show output
[
{
"name" : "Bob",
"knownBy" : 2
},
{
"name" : "Dave",
"knownBy" : 1
},
{
"name" : "Alice",
"knownBy" : 1
},
{
"name" : "Charlie",
"knownBy" : 1
}
]MATCH compared to graph traversals
MATCH and graph traversals (FOR ... IN min..max OUTBOUND|INBOUND|ANY ...) can answer many of the same questions.
They differ in what they let you express and in how they are executed.
How MATCH is executed
When planning a query, ArangoDB translates a MATCH operation into regular
AQL operations:
- Node patterns and fixed-length edge patterns become nested
FORloops that join the nodes and edges via their_id,_from, and_toattributes, with filter conditions for the attribute values andWHEREconditions. This is similar to the joins you could write by hand, and the optimizer can use the primary index, the edge index, and other indexes for them. - Variable-length edge patterns become graph traversals with a filter that checks the collection of the end node.
You can see the resulting execution plan by
explaining the query
with the matchStatement option set.
The pattern becomes three nested loops: a primary index lookup for the start node, followed by an edge index lookup and a primary index lookup for each match:
MATCH (a:persons { _key: "eve" }) -[:knows]-> (b:persons)
RETURN b.name{
"matchStatement": "experimental"
}Show output
Query String (74 chars, results cachable: true):
MATCH (a:persons { _key: "eve" }) -[:knows]-> (b:persons)
RETURN b.name
Execution plan:
Id NodeType Par Est. Comment
1 SingletonNode 1 * ROOT
17 IndexNode ✓ 1 - FOR a IN persons /* primary index scan, index only (projections: `_id`) */ LET #13 = a.`_id`
16 IndexNode ✓ 1 - FOR #1 IN knows /* edge index scan, index only (projections: `_to`) */ LET #12 = #1.`_to`
15 IndexNode ✓ 1 - FOR b IN persons /* primary index scan, index scan + document lookup (projections: `name`) */ LET #11 = b.`name`
14 ReturnNode 1 - RETURN #11
Indexes used:
By Name Type Collection Unique Sparse Cache Selectivity Fields Stored values Ranges
17 primary primary persons true false false 100.00 % [ `_key` ] [ ] (a.`_key` == "eve")
16 edge edge knows false false false 100.00 % [ `_from` ] [ ] (#13 == #1.`_from`)
15 primary primary persons true false false 100.00 % [ `_key` ] [ ] (#12 == b.`_id`)
Optimization rules applied:
Id Rule Name Id Rule Name Id Rule Name
1 move-calculations-up 6 move-calculations-up-2 11 reduce-extraction-to-projection
2 move-filters-up 7 move-filters-up-2 12 optimize-projections
3 remove-redundant-calculations 8 use-indexes 13 remove-unnecessary-calculations-4
4 remove-unnecessary-filters 9 remove-filter-covered-by-index 14 async-prefetch
5 remove-unnecessary-calculations 10 remove-unnecessary-calculations-2
109 rule(s) executed, 2 plan(s) created, peak mem [b]: 32768, exec time [s]: 0.00049When to use MATCH
- Multi-hop patterns with conditions on every element: You can name and
filter each node and edge of a pattern individually, which is more verbose
with traversals, where you need to check
p.vertices[n]andp.edges[n]for each position. - Patterns with cycles and multiple branches: A pattern can refer back to a
node, like
(a) -[...]-> (b) -[...]-> (c) -[...]-> (a), and you can combine multiple patterns that share nodes. Traversals only follow paths from a start node and you need additional filters or nested traversals for such shapes. - Patterns without a fixed start node: With fixed-length edge patterns, the query is executed as a join, which can be efficient if there are selective conditions on the nodes and edges, compared to starting a traversal from every node of a collection.
- Readable syntax: A pattern resembles a drawing of the graph shape you
are looking for. If you know Cypher or GQL, note that
MATCHonly covers a subset of their pattern syntax, and the details differ.
When to use graph traversals
- Control over the traversal: Traversals support
PRUNEconditions to stop following a path early, options for the uniqueness of nodes and edges, breadth-first and weighted order, depth-dependent conditions, and more.MATCHuses the defaults of traversals for variable-length edge patterns (depth-first order, unique edges per path, no uniqueness of nodes) and you cannot change them. - Variable-length paths: Variable-length edge patterns check the collection of the end node, which makes them slower than an equivalent traversal without such a check.
- Named graphs and path searches:
MATCHonly supports collections. For named graphs as well as shortest path and k paths searches, use the respective graph queries. - Production use:
MATCHis experimental.
Performance considerations
- Order of the patterns: The joins are executed in the order that the
elements appear in the pattern. Start a pattern with the most selective node
pattern. For example,
(a:persons) -[:knows]-> (b:persons { _key: "bob" })iterates over all persons forabefore it looks for Bob, whereas the equivalent(b:persons { _key: "bob" }) <-[:knows]- (a:persons)starts with a single document lookup for Bob and only follows his inbound edges. - Indexes: Conditions on the nodes and edges can utilize indexes like in
regular
FORloops, for example, a persistent index on an attribute that you filter by in a node pattern.
Limitations
The following restrictions apply to the experimental MATCH operation:
- Every node pattern that declares a variable and every edge pattern needs a
collection label. Patterns like
(),-[]->, and-->are not supported. - Labels need to be collection names or collection bind parameters. Views are not supported. Node labels need to be document collections, and edge labels need to be edge collections.
- A node pattern can only have a single collection label. Unlike for edge
patterns, you cannot list alternatives like
(c:germanCity|frenchCity). - In cluster deployments, a
MATCHoperation with variable-length edge patterns or multiple edge collections can access node collections that it doesn’t know about at query compile time. Declare all node collections that such patterns can reach with aWITHoperation at the start of the query, like for graph traversals. - Named graphs (
GRAPH "name") are not supported.
