11.3. The Neo4j Graph Database

11.3.1. Relationships
11.3.2. Properties

This chapter will introduce more details on the data model and behavior of Neo4j.

11.3.1. Relationships

Relationships between nodes are a key part of a graph database. They allow for finding related data.

A relationship connects two nodes, and is guaranteed to have valid start and end nodes.

As relationships are always directed, they can be viewed as outgoing or incoming relative to a node, which is useful when traversing the graph:

Relationships are equally well traversed in either direction. This means that there is no need to add duplicate relationships in the opposite direction (with regard to traversal or performance).

While relationships always have a direction, you can ignore the direction where it is not useful in your application.

Note that a node can have relationships to itself as well:

To further enhance graph traversal all relationships have a relationship type. The following example shows a simple social network with two relationship types.

Table 11.1. Using relationship direction and type

What How

get who a person follows

outgoing follows relationships, depth one

get the followers of a person

incoming follows relationships, depth one

get who a person blocks

outgoing blocks relationships, depth one

get who a person is blocked by

incoming blocks relationships, depth one


This example is a simple model of a file system, which includes symbolic links:

Depending on what you are looking for, you will use the direction and type of relationships during traversal.

What How

get the full path of a file

incoming file relationships

get all paths for a file

incoming file and symbolic link relationships

get all files in a directory

outgoing file and symbolic link relationships, stop at depth one

get all files in a directory, excluding symbolic links

outgoing file relationships, stop at depth one

get all files in a directory, recursively

outgoing file and symbolic link relationships

11.3.2. Properties

Properties are key-value pairs where the key is a string. Property values can be either a primitive or an array of one primitive type. For example String, int and int[] values are valid for properties.

[Note]Note

null is not a valid property value. Nulls can instead be modeled by the absence of a key.

Table 11.2. Property value types

Type Description Value range

boolean

true/false

byte

8-bit integer

-128 to 127, inclusive

short

16-bit integer

-32768 to 32767, inclusive

int

32-bit integer

-2147483648 to 2147483647, inclusive

long

64-bit integer

-9223372036854775808 to 9223372036854775807, inclusive

float

32-bit IEEE 754 floating-point number

double

64-bit IEEE 754 floating-point number

char

16-bit unsigned integers representing Unicode characters

u0000 to uffff (0 to 65535)

String

sequence of Unicode characters


For further details on float/double values, see Java Language Specification.