3.4.11. User-defined functions

User-defined functions are written in Java, deployed into the database and are called in the same way as any other Cypher function.

There are two main types of functions that can be developed and used:

Type Description Usage Developing

Scalar

For each row the function takes parameters and returns a result

Using UDF

Extending Neo4j (UDF)

Aggregating

Consumes many rows and produces an aggregated result

Using aggregating UDF

Extending Neo4j (Aggregating UDF)

3.4.11.1. User-defined scalar functions

For each incoming row the function takes parameters and returns a single result.

This example shows how you invoke a user-defined function called join from Cypher.

Call a user-defined function

This calls the user-defined function org.neo4j.procedure.example.join().

Query. 

MATCH (n:Member)
RETURN org.neo4j.function.example.join(collect(n.name)) AS members

Table 3.383. Result
members

1 row

"John,Paul,George,Ringo"

Try this query live.  UNWIND ["John", "Paul", "George", "Ringo"] as name CREATE (:Member {name: name}) MATCH (n:Member) RETURN org.neo4j.function.example.join(collect(n.name)) AS members

For developing and deploying user-defined functions in Neo4j, see Extending Neo4j → User-defined functions.

3.4.11.2. User-defined aggregation functions

Aggregating functions consume many rows and produces a single aggregated result.

This example shows how you invoke a user-defined aggregation function called longestString from Cypher.

Call a user-defined aggregation function

This calls the user-defined function org.neo4j.function.example.longestString().

Query. 

MATCH (n:Member)
RETURN org.neo4j.function.example.longestString(n.name) AS member

Table 3.384. Result
member

1 row

"George"

Try this query live.  UNWIND ['John', 'Paul', 'George', 'Ringe'] AS beatle CREATE (:Member {name: beatle}) MATCH (n:Member) RETURN org.neo4j.function.example.longestString(n.name) AS member