You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `MATCH` clause allows you to specify the patterns Neo4j will search for in the database.
6
+
The `MATCH` clause enables you to define specific patterns that the database will search for within its graph structure.
7
+
The `MATCH` clause can specify the nodes, relationships, and properties in a pattern, allowing for queries that traverse the graph to retrieve relevant data.
The `MATCH` clause allows you to specify node patterns of varying complexity to retrieve from a graph.
39
41
For more information about finding node patterns, see xref:patterns/fixed-length-patterns#node-patterns[Patterns -> Node patterns].
40
42
41
-
42
43
[[find-all-nodes]]
43
44
=== Find all nodes
44
45
@@ -110,32 +111,52 @@ RETURN n.name AS name, n.title AS title
110
111
2+|Rows: 7
111
112
|===
112
113
114
+
.Node pattern using the `AND` (`&`) and negation (`!`) label expression
115
+
[source, cypher]
116
+
----
117
+
MATCH (n:!Director&!Movie)
118
+
RETURN labels(n) AS label, count(n) AS labelCount
119
+
----
120
+
121
+
[NOTE]
122
+
The above query uses the xref:functions/list.adoc#functions-labels[`labels()`] and xref:functions/aggregating.adoc#functions-count[`count()`] functions.
For more information about how to set parameters, see xref:syntax/parameters.adoc[Syntax -> Parameters].
382
+
316
383
[[find-paths]]
317
384
== Find paths
318
385
319
-
The `MATCH` clause can also be used to bind paths to variables.
386
+
The `MATCH` clause can also be used to bind whole paths to variables.
320
387
321
388
.Find all paths matching a pattern
322
389
[source, cypher]
@@ -356,26 +423,68 @@ RETURN path
356
423
|Rows: 3
357
424
|===
358
425
359
-
For more information about how `MATCH` is used to find patterns (including xref:patterns/variable-length-patterns.adoc#quantified-path-patterns[quantified path patterns], xref:patterns/variable-length-patterns.adoc#quantified-relationships[quantified relationships], and xref:patterns/shortest-paths.adoc[shortest paths]), see the section on xref::patterns/index.adoc[Patterns].
426
+
For more information about how `MATCH` is used to find patterns of varying complexity (including xref:patterns/variable-length-patterns.adoc#quantified-path-patterns[quantified path patterns], xref:patterns/variable-length-patterns.adoc#quantified-relationships[quantified relationships], and the xref:patterns/shortest-paths.adoc[shortest paths] between nodes), see the section on xref::patterns/index.adoc[Patterns].
360
427
361
428
== Multiple MATCH clauses, the WITH clause, and clause composition
362
429
363
-
In Cypher, the output of a clause creates a new state of the graph, and a new table of intermediate results which serves as the input of the next clause.
364
-
The first clause takes as input the state of the graph before the query and an empty table of intermediate results.
365
-
The output of the last clause is the result of the query.
430
+
In Cypher, a query’s behavior is defined by its clauses.
431
+
Each clause takes the current graph state and a table of intermediate results, processes them, and passes the updated graph state and results to the next clause.
432
+
The first clause starts with the graph's initial state and an empty table, while the final clause produces the query's result.
366
433
434
+
.Chaining consecutive `MATCH` clauses
435
+
[source, cypher]
436
+
----
437
+
MATCH (:Person {name: 'Martin Sheen'})-[:ACTED_IN]->(movie:Movie) // <1>
438
+
MATCH (director:Person)-[:DIRECTED]->(movie) // <2>
439
+
RETURN director.name AS director, movie.title AS movieTitle
440
+
----
441
+
<1> The result of the first `MATCH` clause is the variable `movie` which holds all the `Movies` that `Martin Sheen` has `ACTED_IN`.
442
+
<2> The second `MATCH` clause uses the `movie` variable to find any `Person` node with a `DIRECTED` relationship to those `Movie` nodes that `Martin Sheen` has `ACTED_IN`.
367
443
368
-
In Cypher, each clause has as input the state of the graph and a table of intermediate results
369
-
Each clause has as input the state of the graph and a table of intermediate results consisting of the current variables. The output of a clause is a new state of the graph and a new table of intermediate results, serving as input to the next clause. The first clause takes as input the state of the graph before the query and an empty table of intermediate results. The output of the last clause is the result of the query.
MATCH (martin:Person {name: 'Martin Sheen'})-[:ACTED_IN]->(movie:Movie)
372
-
WITH movie // Pass the movie variable to the next part of the query
373
-
MATCH (director:Person)-[:DIRECTED]->(movie)
374
-
RETURN director.name AS Director, movie.title AS MovieTitle
452
+
2+d| Rows: 2
453
+
|===
375
454
455
+
A variable can be implicitly carried over to the following clause by being referenced in another operation.
456
+
A variable can also be explicitly passed to the following clause using the `WITH` clause.
457
+
If a variable is neither implicitly nor explicitly carried over, it will be discarded and unavailable for reference later in the query.
376
458
377
-
== MATCH with parameters
459
+
.Using `WITH` and multiple `MATCH` clauses
460
+
[source, cypher]
461
+
----
462
+
MATCH (actors:Person)-[:ACTED_IN]->(movies:Movie) // <1>
463
+
WITH actors, count(movies) AS movieCount // <2>
464
+
ORDER BY movieCount DESC
465
+
LIMIT 1 // <3>
466
+
MATCH (actors)-[:ACTED_IN]->(movies) // <4>
467
+
RETURN actors.name AS actor, movieCount, collect(movies.title) AS movies
468
+
----
469
+
<1> The `Person` and `Movie` nodes matched in this step are stored in variables.
470
+
which are then passed on to the second row of the query.
471
+
<2> The `movie` variable is here implicitly imported by its occurrence in the `count()` function.
472
+
The `WITH` clause explicitly imports the `actors` variable.
473
+
<3> An xref:clauses/order-by.adoc[`ORDER BY`] clause orders the results by `movieCount` in descending order, ensuring that the `Person` with the highest number of movies appears at the top, and xref:clauses/limit.adoc[`LIMIT] 1` ensures that all other `Person` nodes are discarded.
474
+
<4> The second MATCH clause finds all `Movie` nodes associated with the `Person` nodes currently bound to the `actors` variable.
475
+
476
+
[NOTE]
477
+
The above query uses the xref:functions/aggregating.adoc#functions-collect[`collect()` function].
0 commit comments