Introduction
Let’s take a look at the types of data returned by a Cypher query.
The majority of the types returned by a Cypher query are mapped directly to Java types, but some more complex types need special handling.
-
Graph types - Nodes, Relationships and Paths
-
Spatial types - Points and distances
Types in Neo4j Browser
When graph types are returned by a query, they are visualized in a graph layout.
Java Type | Neo4j Cypher Type |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Graph types
The following code snippet finds all movies with the specified title and returns person
, acted_in
and movie
.
final String cypher = """
MATCH path = (person:Person)-[actedIn:ACTED_IN]->(movie:Movie {title: $title})
RETURN path, person, actedIn, movie
""";
final String title = "Toy Story";
var result = driver.executableQuery(cypher)
.withParameters(Map.of("title", title))
.execute();
Nodes
Nodes are returned as a Node
object.
import org.neo4j.driver.types.Node;
var records = result.records();
records.forEach(r -> {
Node node = r.get("person").asNode();
});
Nodes
records.forEach(r -> {
Node node = r.get("person").asNode();
System.out.println(node.elementId()); // (1)
System.out.println(node.labels()); // (2)
System.out.println(node.values()); // (3)
System.out.println(node.get("name")); // (4)
});
-
The
elementId()
method provides access to the node’s element ID
eg.4:97b72e9c-ae4d-427c-96ff-8858ecf16f88:0
-
The
labels()
method contains a list of labels attributed to the Node
eg.['Person', 'Actor']
-
The
values()
method provides access to the node’s properties as an iterable ofValue
objects.
eg.{name: 'Tom Hanks', tmdbId: '31'}
-
A single property can be retrieved using the
get()
method.
Relationships
Relationships are returned as a Relationship
object.
import org.neo4j.driver.types.Relationship;
records.forEach(r -> {
Relationship actedIn = r.get("actedIn").asRelationship();
System.out.println(actedIn.elementId()); // (1)
System.out.println(actedIn.type()); // (2)
System.out.println(actedIn.values()); // (3)
System.out.println(actedIn.get("role")); // (4)
System.out.println(actedIn.startNodeElementId()); // (5)
System.out.println(actedIn.endNodeElementId()); // (6)
});
-
elementId()
- The element ID of the relationship
eg.5:1218f598-63ab-460f-ac59-36d4cadee840:167495
-
type()
- Type of relationship
eg.ACTED_IN
-
values()
- Returns relationship properties as name-value pairs (eg.{role: 'Woody'}
) -
Access properties using the
get()
method -
startNodeElementId
- The element ID of theNode
at the start of the relationship -
endNodeElementId
- The element ID of theNode
at the end of the relationship
Paths
A path is a sequence of nodes and relationships and is returned as a Path
object.
import org.neo4j.driver.types.Path;
records.forEach(r -> {
Path path = r.get("path").asPath();
System.out.println(path.nodes()); // (1)
System.out.println(path.relationships()); // (2)
System.out.println(path.start()); // (3)
System.out.println(path.end()); // (4)
System.out.println(path.length()); // (5)
});
-
nodes()
- An iterable ofNode
objects in the path -
relationships()
- An iterable ofRelationship
objects in the path -
start()
- TheNode
object at the start of the path -
end()
- TheNode
object at the end of the path -
length()
- The number of relationships within the path
Check your understanding
Summary
In this lesson, you learned about the types of data returned by a Cypher query and how to work with them in your application.
Now it’s time to test yourself on what you’ve learned.