Skip to content

Graph functions

View as Markdown

Graph functions work on the nodes, edges and paths matched by a MATCH pattern. Path functions take a path variable (pth in MATCH pth = (a)-[...]-(b)); element functions take a node or edge alias.

Function Returns
nodes(path) LIST of the vertices of a path, in order
relationships(path) LIST of the edges of a path, in order
path_size(path) Number of edges (hops) in a path
path_comp(pattern ...) LIST built from every match of an inline pattern
LABEL(element) Name of the vertex or edge table of a node or edge
JSON_NODE_PROPERTIES(node) / JSON_EDGE_PROPERTIES(edge) JSON with the element id and its columns
JSON_INFO(element) JSON with id, label, columns and adjacency list
JSON_ADJACENCY_LIST(node) JSON array of the node's edges
JSON_GRAPH(elements...) JSON {nodes, edges} subgraph built from all matched rows (aggregate)

They are typically combined with the list functions first(), last(), at(), size() and list_comp(), and with property_of(), which reads a column of a node or edge taken from a list:

property_of(element, column_name, vertex_or_edge_table)

The examples use the e-commerce sample database.

nodes(path_variable)

Returns the vertices of a path as a LIST, from the first node to the last. The argument must be a path variable; a node alias, a scalar or NULL raises an error.

Return type: LIST of vertices

Products liked by a customer who also likes Vegie-spread. The path is product → customer → product, so position 0 is the start product, position 1 the customer and the last position the other product:

SELECT property_of(first(nodes(pth)), name, product) AS product,
property_of(at(nodes(pth), 1), company_name, customer) AS liked_by,
property_of(last(nodes(pth)), name, product) AS also_liked
FROM MATCH pth = (p1:product)-[l:likes{2,2}]-(p2:product)
WHERE p1.name = 'Vegie-spread' AND p1 <> p2
ORDER BY 2, 3
LIMIT 5;
product liked_by also_liked
Vegie-spread Alvarado LLC Lono Yoga Short
Vegie-spread Alvarado LLC Neve Studio Dance Jacket
Vegie-spread Alvarado LLC Zing Jump Rope
Vegie-spread Boyd Inc Helena Hooded Fleece
Vegie-spread Brown Ltd Balboa Persistence Tee
relationships(path_variable)

Returns the edges of a path as a LIST, in path order. The argument must be a path variable.

Return type: LIST of edges

When were the two likes that link Vegie-spread to each co-liked product made?

SELECT p2.name AS other_product,
property_of(first(relationships(pth)), since, likes) AS first_like_at,
property_of(last(relationships(pth)), since, likes) AS second_like_at
FROM MATCH pth = (p1:product)-[l:likes{2,2}]-(p2:product)
WHERE p1.name = 'Vegie-spread' AND p1 <> p2
ORDER BY 1
LIMIT 3;
other_product first_like_at second_like_at
Abominable Hoodie 2023-08-21 19:51:12.000 2025-03-29 22:27:02.000
Aeon Capri 2023-06-06 01:11:39.000 2024-08-03 17:54:24.000
Arcadio Gym Short 2022-11-04 14:49:12.000 2023-12-06 02:07:47.000

Use ALL(... IN relationships(pth) WHERE ...) / NONE(...) to test every edge of a path. Products co-liked with Vegie-spread where both likes date from 2022 or later:

SELECT COUNT(DISTINCT p2.id) AS products
FROM MATCH pth = (p1:product)-[l:likes{2,2}]-(p2:product)
WHERE p1.name = 'Vegie-spread' AND p1 <> p2
AND ALL(r IN relationships(pth) WHERE property_of(r, since, likes) >= DATETIME'2022-01-01 00:00:00');
products
38

path_size(path_variable)

Returns the number of edges in the path, the same value as size(relationships(path)). The number of nodes is one more.

Return type: INT

SELECT path_size(pth) AS path_size,
size(nodes(pth)) AS nodes,
size(relationships(pth)) AS edges
FROM MATCH pth = ANY_SHORTEST (c1:customer)-[f:follows{1,6}]-(c2:customer)
WHERE c1.id = 5 AND c2.id = 15;
path_size nodes edges
6 7 6
path_comp(<graph_pattern> [, <graph_pattern> ...] [WHERE <condition>] | <expression> [ORDER BY <ordering>])

Path comprehension: matches the inline pattern, evaluates expression for every match and returns the values as one LIST. No FROM MATCH is needed.

  • graph_pattern: one or more patterns, written as in MATCH. A single node without an edge is not a valid pattern.
  • condition: optional filter on the matches.
  • expression: required; the value collected for each match.
  • ordering: optional order of the list (ORDER BY 1 sorts by the expression).

Return type: LIST

All products liked by the customer Hampton PLC, alphabetically, in one value:

SELECT path_comp((cu:customer)-[l:likes]->(p:product)
WHERE cu.company_name = 'Hampton PLC'
| p.name ORDER BY 1) AS liked_products;
liked_products
{'Compete Track Tote', 'Daria Bikram Pant', 'Diana Tights', 'Hollister Backyard Sweatshirt', 'Nora Practice Tank'}
LABEL(node_or_edge)

Returns the name of the vertex table of a node, or of the edge table of an edge. Useful with edge alternatives such as [r:likes|follows] or with UNION ALL across relationship types.

Return type: STRING

SELECT LABEL(cu) AS node_label, LABEL(r) AS edge_label, ci.name
FROM MATCH (cu:customer)-[r:resides_in]->(ci:city)
WHERE cu.id = 5;
node_label edge_label ci.name
customer resides_in Luleå

These functions return JSON values, convenient for applications and for graph visualisation tools. Every node and edge is identified by its internal object id, written "(volume|page|slot)"; the id is stable for the life of the row but is not the id column.

JSON_NODE_PROPERTIES, JSON_EDGE_PROPERTIES

Section titled “JSON_NODE_PROPERTIES, JSON_EDGE_PROPERTIES”
JSON_NODE_PROPERTIES(node)
JSON_EDGE_PROPERTIES(edge)

Return {"id": ..., "properties": {column: value, ...}} for one node or edge.

SELECT JSON_NODE_PROPERTIES(b) AS brand_props,
JSON_EDGE_PROPERTIES(f) AS follow_props
FROM MATCH (cu:customer)-[f:follows]->(b:brand)
WHERE cu.id = 5;
-- brand_props
{"id":"(0|4545|57)","properties":{"id":57,"name":"Skechers"}}
-- follow_props
{"id":"(0|4993|5)","properties":{"since":"07:02:33.000 PM 06/03/2025"}}

JSON_ADJACENCY_LIST(node)

Returns a JSON array with one entry per edge of the node: edge_id, the id of the node at the other end (dest) and the direction (dir: "0" outgoing, "1" incoming).

SELECT JSON_ADJACENCY_LIST(b) AS neighbourhood
FROM MATCH (b:brand)
WHERE b.name = 'Lululemon';
[{"edge_id":"(0|4801|8)","dest":"(0|4161|8)","dir":"0"},
{"edge_id":"(0|4929|152)","dest":"(0|4481|132)","dir":"0"},
...
{"edge_id":"(0|4993|23)","dest":"(0|4289|15)","dir":"1"},
...]

Lululemon has outgoing located_at and manufactures edges (dir 0) and incoming follows edges from customers (dir 1).

JSON_INFO(node_or_edge)

Returns everything about an element in one document: id, properties, label and adj_list (the adjacency list).

SELECT JSON_INFO(s) AS supplier_info
FROM MATCH (s:supplier)
WHERE s.company_name = 'Exotic Liquids';
{"id":"(0|4609|1)",
"properties":{"id":1,"phone":"(171) 555-2222","address":"49 Gilbert St.",
"contact_title":"Purchasing Manager","contact_name":"Charlotte Cooper",
"company_name":"Exotic Liquids"},
"label":"supplier",
"adj_list":[{"edge_id":"(0|4737|1)","dest":"(0|4161|1)","dir":"0"}, ...]}

JSON_GRAPH(element [, element ...])

An aggregate function: collects the listed nodes and edges of all matched rows into one subgraph, {"nodes": [...], "edges": [...]}, without duplicates. Each node has id, label and properties; each edge has id, startNodeId, endNodeId, label and properties. The format can be fed directly to graph visualisation libraries.

The customer Hampton PLC, the products it likes and the likes edges:

SELECT JSON_GRAPH(cu, l, p) AS subgraph
FROM MATCH (cu:customer)-[l:likes]->(p:product)
WHERE cu.company_name = 'Hampton PLC';
{"nodes":[
{"id":"(0|4289|66)","label":"customer","properties":{"id":65,"company_name":"Hampton PLC", ...}},
{"id":"(0|4486|171)","label":"product","properties":{"id":1379,"name":"Hollister Backyard Sweatshirt","unit_price":52.0, ...}},
...],
"edges":[
{"id":"(0|5633|111)","startNodeId":"(0|4289|66)","endNodeId":"(0|4492|83)","label":"likes",
"properties":{"since":"12:50:57.000 AM 09/28/2023"}},
...]}