-
Notifications
You must be signed in to change notification settings - Fork 64
Dynamic labels/types #1098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic labels/types #1098
Changes from 19 commits
edec296
47a4162
84d1ae6
6990444
fa24662
5131a8d
cfc3da4
e3bd684
aef4d38
2a3579a
270ff81
b614ba2
007d488
ed4ff85
e9debb3
d7a126a
6912c0d
051e14e
06fac3b
a2211f0
39581df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,11 +17,11 @@ To recreate the graph, run the following query against an empty Neo4j database: | |
|
|
||
| [source, cypher, role=test-setup] | ||
| ---- | ||
| CREATE (charlie:Person {name: 'Charlie Sheen'}), | ||
| (martin:Person {name: 'Martin Sheen'}), | ||
| (michael:Person {name: 'Michael Douglas'}), | ||
| (oliver:Person {name: 'Oliver Stone'}), | ||
| (rob:Person {name: 'Rob Reiner'}), | ||
| CREATE (charlie:Person:Actor {name: 'Charlie Sheen'}), | ||
| (martin:Person:Actor {name: 'Martin Sheen'}), | ||
| (michael:Person:Actor {name: 'Michael Douglas'}), | ||
| (oliver:Person:Director {name: 'Oliver Stone'}), | ||
| (rob:Person:Director {name: 'Rob Reiner'}), | ||
| (wallStreet:Movie {title: 'Wall Street'}), | ||
| (charlie)-[:ACTED_IN {role: 'Bud Fox'}]->(wallStreet), | ||
| (martin)-[:ACTED_IN {role: 'Carl Fox'}]->(wallStreet), | ||
|
|
@@ -124,8 +124,10 @@ The above query uses the xref:functions/list.adoc#functions-labels[`labels()`] a | |
| [role="queryresult",options="header,footer",cols="2*<m"] | ||
| |=== | ||
| | label | labelCount | ||
| | ["Person"] | 5 | ||
| 2+d| Rows: 1 | ||
| | ["Person", "Actor"] | 3 | ||
| | ["Person", "Director"] | 2 | ||
|
|
||
| 2+d| Rows: 2 | ||
| |=== | ||
|
|
||
| For a list of all label expressions supported by Cypher, see xref:patterns/reference.adoc#label-expressions[Patterns -> Label expressions]. | ||
|
|
@@ -490,3 +492,124 @@ The above query uses the xref:functions/aggregating.adoc#functions-collect[`coll | |
|
|
||
| For more information about how Cypher queries work, see xref:clauses/clause-composition.adoc[]. | ||
|
|
||
| [role=label--new-5.26] | ||
| [[dynamic-match]] | ||
| == MATCH nodes and relationships using dynamic node labels and relationship types | ||
|
||
|
|
||
| Node labels and relationship types can be referenced dynamically in expressions, parameters, and variables when matching nodes and relationships. | ||
| This allows for more flexible queries and mitigates the risk of Cypher injection. | ||
| (For more information about Cypher injection, see link:https://neo4j.com/developer/kb/protecting-against-cypher-injection/[Neo4j Knowledge Base -> Protecting against Cypher injection]). | ||
|
|
||
| .Syntax for matching node labels dynamically | ||
| [source, syntax] | ||
| ---- | ||
| MATCH (n:$(<expr>)) | ||
| MATCH (n:$any(<expr>)) | ||
| MATCH (n:$all(<expr>)) | ||
| ---- | ||
|
|
||
| [NOTE] | ||
| `MATCH (n:$all(<expr>))` is functionally equivalent to `MATCH (n:$(<expr>))`. | ||
|
|
||
| .Syntax for matching relationship types dynamically | ||
| [source, syntax] | ||
| ---- | ||
| MATCH ()-[r:$(<expr>))]->() | ||
| MATCH ()-[r:$any(<expr>)]->() | ||
| MATCH ()-[r:$all(<expr>))]->() | ||
| ---- | ||
|
|
||
| The expression must evaluate to a `STRING NOT NULL | LIST<STRING NOT NULL> NOT NULL` value. | ||
| If you use a `LIST<STRING>` with more than one item in a relationship pattern with dynamic relationship types, no results will be returned. | ||
| This is because a relationship can only have exactly one type. | ||
|
|
||
| .Match labels dynamically | ||
| [source, cypher] | ||
| ---- | ||
| WITH ["Person", "Director"] AS labels | ||
| MATCH (directors:$(labels)) | ||
| RETURN directors | ||
| ---- | ||
|
|
||
| .Result | ||
| [role="queryresult",options="header,footer",cols="1*<m"] | ||
| |=== | ||
| | directors | ||
|
|
||
| | (:Person:Director {name: "Oliver Stone"}) | ||
| | (:Person:Director {name: "Rob Reiner"}) | ||
|
|
||
| 1+d|Rows: 2 | ||
| |=== | ||
|
|
||
| .Match nodes dynamically using the `any()` function | ||
| [source, cypher] | ||
| ---- | ||
| MATCH (n:$any(["Movie", "Actor"])) | ||
| RETURN n AS nodes | ||
| ---- | ||
|
|
||
| [NOTE] | ||
| The xref:functions/predicate.adoc#functions-any[`any()`] function matches nodes that have any of the specified labels. | ||
|
||
|
|
||
| .Result | ||
| [role="queryresult",options="header,footer",cols="1*<m"] | ||
| |=== | ||
| | nodes | ||
|
|
||
| | (:Person:Actor {name: "Charlie Sheen"}) | ||
| | (:Person:Actor {name: "Martin Sheen"}) | ||
| | (:Person:Actor {name: "Michael Douglas"}) | ||
| | (:Movie {title: "Wall Street"}) | ||
| | (:Movie {title: "The American President"}) | ||
|
|
||
| 1+d|Rows: 5 | ||
| |=== | ||
|
|
||
|
|
||
| .Parameter | ||
| [source, parameters] | ||
| ---- | ||
| { | ||
| "label": "Movie" | ||
| } | ||
| ---- | ||
|
|
||
| .Match nodes dynamically using a parameter | ||
| [source, cypher] | ||
| ---- | ||
| MATCH (movie:$($label)) | ||
| RETURN movie.title AS movieTitle | ||
| ---- | ||
|
|
||
| .Result | ||
| [role="queryresult",options="header,footer",cols="1*<m"] | ||
| |=== | ||
| | movieTitle | ||
|
|
||
| | "Wall Street" | ||
| | "The American President" | ||
|
|
||
| 1+d|Rows: 2 | ||
| |=== | ||
|
|
||
|
|
||
| .Match relationships dynamically using a variable | ||
| [source, cypher] | ||
| ---- | ||
| CALL db.relationshipTypes() | ||
| YIELD relationshipType | ||
| MATCH ()-[r:$(relationshipType)]->() | ||
| RETURN relationshipType, count(r) AS relationshipCount | ||
| ---- | ||
|
|
||
| .Result | ||
| [role="queryresult",options="header,footer",cols="2*<m"] | ||
| |=== | ||
| | relationshipType | relationshipCount | ||
|
|
||
| | "ACTED_IN" | 5 | ||
| | "DIRECTED" | 2 | ||
|
|
||
| 2+d|Rows: 2 | ||
| |=== | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a really long title, is it possible to make it shorter?
CREATE using dynamic node labels and relationship typesperhaps?