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 Python types, but some more complex types need special handling.
-
Graph types - Nodes, Relationships and Paths
-
Temporal types - Dates and times
-
Spatial types - Points and distances
Types in Neo4j Browser
When graph types are returned by a query, they are visualized in a graph layout.
Python Type | Neo4j Cypher Type |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Graph types
The following code snippet finds all movies with the specified title and returns person
, acted_in
and movie
.
records, summary, keys = driver.execute_query("""
MATCH path = (person:Person)-[actedIn:ACTED_IN]->(movie:Movie {title: $title})
RETURN path, person, actedIn, movie
""", title=movie)
Nodes
Nodes are returned as a Node
object.
for record in records:
node = record["movie"]
print(node.id) # (1)
print(node.labels) # (2)
print(node.items()) # (3)
# (4)
print(node["name"])
print(node.get("name", "N/A"))
-
The
element_id
property provides access to the node’s element ID
eg.4:97b72e9c-ae4d-427c-96ff-8858ecf16f88:0
-
The
labels
property is a frozenset containing an array of labels attributed to the Node
eg.['Person', 'Actor']
-
The
items()
method provides access to the node’s properties as an iterable of all name-value pairs.
eg.{name: 'Tom Hanks', tmdbId: '31' }
-
A single property can be retrieved by either using
[]
brackets or using theget()
method. Theget()
method also allows you to define a default property if none exists.
Relationships
Relationships are returned as a Relationship
object.
acted_in = record["actedIn"]
print(acted_in.id) # (1)
print(acted_in.type) # (2)
print(acted_in.items()) # (3)
# 4
print(acted_in["roles"])
print(acted_in.get("roles", "(Unknown)"))
print(acted_in.start_node) # (5)
print(acted_in.end_node) # (6)
-
id
- Internal ID of the relationship (eg.9876
) -
type
- Type of relationship (eg.ACTED_IN
) -
items()
- Returns relationship properties as name-value pairs (eg.{role: 'Woody'}
) -
Access properties using brackets
[]
orget()
method -
start_node
- ID of the starting node -
end_node
- ID of the ending node
Paths
A path is a sequence of nodes and relationships and is returned as a Path
object.
path = record["path"]
print(path.start_node) # (1)
print(path.end_node) # (2)
print(len(path)) # (1)
print(path.relationships) # (1)
-
start_node
- a Neo4jInteger
representing the internal ID for the node at the start of the path -
end_node
- a Neo4jInteger
representing the internal ID for the node at the end of the path -
len(path)
- A count of the number of relationships within the path -
relationships
- An array ofRelationship
objects within the path.
Paths are iterable
Use iter(path)
to iterate over the relationships in a path.
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.