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
@@ -77,7 +77,7 @@ Now, if you were to write this same information in Cypher, then it would look li
77
77
(:Sally)-[:WORKS_FOR]->(:Neo4j)
78
78
----
79
79
80
-
However, in order to have this information in the graph, first you need to represent it as nodes and relationships.
80
+
With this query, you turn the information into nodes and relationships, which are the core elements of Cypher.
81
81
82
82
=== Nodes
83
83
@@ -93,17 +93,16 @@ The parentheses are a representation of the circles that compose the nodes in th
93
93
94
94
==== Node labels
95
95
96
-
Nodes can be grouped together through a <<label>>.
97
-
They work like tags and allow you to specify certain types of entities to look for or to create.
98
-
Labels also help Cypher distinguish between entities and optimize execution for your queries.
96
+
Nodes can be grouped together through a <<label>>, which is an component that works like tags and allows you to specify certain types of entities in your queries.
97
+
Labels also help Cypher distinguish between nodes and optimize execution.
99
98
100
99
In the example, both `Sally` and `John` are persons, so they get a `Person` label, `Graphs` gets a `Technology` label, and `Neo4j` is a `Company`:
101
100
102
101
.Nodes grouped by labels. Note that `Sally`, `John`, `Graphs`, and `Neo4j` are now xref:cypher/index.adoc#cypher-properties[properties] instead.
In a relational database context, this would be the same as telling SQL which table to look for the particular row.
106
-
The same way you can tell SQL to query a person's information from a `Person` table, you can also tell Cypher to only check the `Person` label for that information.
104
+
In a relational database context, this would be the same as using SQL to refer to a table that should be looked for a particular row.
105
+
The same way you can use SQL to query a person's information from a `Person` table, you can also use Cypher to only check the `Person` label for that information.
107
106
108
107
[CAUTION]
109
108
====
@@ -114,6 +113,7 @@ This can affect performance in very large graphs.
114
113
==== Node variables
115
114
116
115
It is necessary to bind matched data entities to variables in order to reference them in subsequent clauses.
116
+
If part of your query matches nodes that you need to reference in a later part of your query (i.e. in a link:https://neo4j.com/docs/cypher-manual/current/clauses/#reading-sub-clauses[subclause]), you can use *node variables*.
117
117
118
118
Variables can be single letters or words, and should be written in lower-case.
119
119
For example, if you want to bind all nodes labeled `Person` to the variable `p`, you write `(p:Person)`.
@@ -129,20 +129,15 @@ RETURN p
129
129
[#cypher-relationships]
130
130
=== Relationships
131
131
132
-
One of the benefits of graph databases is that you can store information about how elements (nodes) are related to each other in the form of relationships.
132
+
Similarly to relational databases, graphs can also store information about how elements (nodes) are related to each other in the form of relationships.
133
133
134
-
In Cypher, relationships are represented as square brackets and an arrow connecting two nodes (e.g. `(Node1)-[]->(Node2)`).
134
+
In Cypher, relationships are represented as square brackets with an optional arrow to indicate the direction (e.g. `(Node1)-[]->(Node2)`).
135
135
136
136
In the example, the lines containing `:LIKES`, `:IS_FRIENDS_WITH`, and `:WORKS_FOR` represent the relationship between the nodes:
137
137
138
138
.Graph featuring nodes and relationships.
139
139
image::cypherintro-graph1.svg[]
140
140
141
-
[NOTE]
142
-
====
143
-
Remember to always put a colon in front of a relationship type.
144
-
If you happen to forget it, and write a query such as `(:Person)-[LIKES]->(:Technology)`, `[LIKES]` will then represent a xref:cypher/index.adoc#_relationship_variables[relationship *variable*], not a relationship *type*.
145
-
====
146
141
147
142
==== Relationship directions
148
143
@@ -196,6 +191,13 @@ In the previous example, the relationship types are:
196
191
* `[:IS_FRIENDS_WITH]` - communicates that Sally _is friends with_ John.
197
192
* `[:WORKS_FOR]` - communicates that Sally _works for_ Neo4j.
198
193
194
+
[IMPORTANT]
195
+
====
196
+
Remember to always put a colon in front of a relationship type.
197
+
If you write `(Person)-[LIKES]->(Technology)`, `[LIKES]` will represent a relationship *variable*, not a relationship *type*.
198
+
In this case, since no relationship type is declared, Cypher's `RETURN` clause will search for all types of relationships in order to retrieve a result to your query.
199
+
====
200
+
199
201
==== Relationship variables
200
202
201
203
Variables can be used for relationships in the same way as for nodes.
Remember to always put a colon in front of a relationship type.
231
-
If you happen to forget it, and write the query like this:
232
-
233
-
[source,cypher]
234
-
----
235
-
(Person)-[LIKES]->(Technology)
236
-
----
237
-
238
-
`[LIKES]` will represent a relationship *variable*, not a relationship *type*.
239
-
In this case, since no relationship type is declared, Cypher will search for all types of relationships in order to retrieve a result to your query.
240
-
241
232
[#cypher-properties]
242
233
=== Properties
243
234
244
235
Properties can be added both to nodes and relationships and be of a variety of data types.
245
236
For a full list of values and types, see link:{docs-home}/cypher-manual/current/values-and-types/[Cypher manual -> Values and types].
246
237
247
-
Another way to organize the data in the previous example would be to add a *property*, `name`, and `Sally` and `John` as *property values* on `Person`-labeled nodes:
238
+
In the following example, `sally` and `john` are xref:cypher/index.adoc##_node_variables[variables] for `Person` nodes which contain a `name` property with the *property values* "Sally" and "John":
248
239
249
240
.Graph example with node and relationship properties.
250
241
image::cypherintro-properties.svg[]
251
242
243
+
To add this info to the graph, you can use the following query:
And once this data is written to the database, you can retrieve it with this pattern:
289
282
290
283
[source, cypher]
291
-
----
284
+
--
292
285
MATCH (sally:Person {name: "Sally"})-[r:LIKES]->(t:Technology {type: "Graphs"})
293
-
RETURN p,r,t
294
-
----
286
+
RETURN sally,r,t
287
+
--
295
288
296
-
=== Patterns variables
289
+
==== Pattern variables
297
290
298
291
In the same way as nodes and relationships, you can also use variables for patterns.
292
+
Considering the previous example, you can turn contain the whole pattern (`(Sally)-[:LIKES]->(Technology)`) inside a pattern by creating a variable (`p`) for it all:
293
+
294
+
[source,cypher]
295
+
--
296
+
MATCH p = (sally:Person {name: "Sally"})-[r:LIKES]->(t:Technology {type: "Graphs"})
297
+
RETURN p
298
+
--
299
+
299
300
For more information, refer to link:{docs-home}/cypher-manual/current/patterns/reference/[Cypher manual -> Patterns -> Syntax and Semantics].
0 commit comments