Graph types

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.

Learn how to interact with Neo4j using the Neo4j Java DriverDirect mapping
Java Type Neo4j Cypher Type

null

null

Boolean

Boolean

Long

Integer

Double

Float

String

String

List

List

Map

Map

Graph types

The following code snippet finds all movies with the specified title and returns person, acted_in and movie.

Java
Return Nodes and Relationships
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.

Java
Working with Node Objects
import org.neo4j.driver.types.Node;

var records = result.records();
records.forEach(r -> {
    Node node = r.get("person").asNode();
});

Nodes

Java
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)
});
  1. The elementId() method provides access to the node’s element ID
    eg. 4:97b72e9c-ae4d-427c-96ff-8858ecf16f88:0

  2. The labels() method contains a list of labels attributed to the Node
    eg. ['Person', 'Actor']

  3. The values() method provides access to the node’s properties as an iterable of Value objects.
    eg. {name: 'Tom Hanks', tmdbId: '31'}

  4. A single property can be retrieved using the get() method.

Relationships

Relationships are returned as a Relationship object.

Java
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)
});
  1. elementId() - The element ID of the relationship
    eg. 5:1218f598-63ab-460f-ac59-36d4cadee840:167495

  2. type() - Type of relationship
    eg. ACTED_IN

  3. values() - Returns relationship properties as name-value pairs (eg. {role: 'Woody'})

  4. Access properties using the get() method

  5. startNodeElementId - The element ID of the Node at the start of the relationship

  6. endNodeElementId - The element ID of the Node at the end of the relationship

Paths

A path is a sequence of nodes and relationships and is returned as a Path object.

Java
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)
});
  1. nodes() - An iterable of Node objects in the path

  2. relationships() - An iterable of Relationship objects in the path

  3. start() - The Node object at the start of the path

  4. end() - The Node object at the end of the path

  5. length() - The number of relationships within the 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.