From cdcb485b9c7aff9a95220eda52f12f0e28ce913e Mon Sep 17 00:00:00 2001 From: Reneta Popova Date: Tue, 22 Sep 2026 12:32:16 +0100 Subject: [PATCH 1/2] Update the procedure example to show the equivalent of the integration test --- .../user-defined-procedures-functions.adoc | 57 ++++++++++++------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/modules/ROOT/pages/extending-neo4j/user-defined-procedures-functions.adoc b/modules/ROOT/pages/extending-neo4j/user-defined-procedures-functions.adoc index 1b40796..d00d1d3 100644 --- a/modules/ROOT/pages/extending-neo4j/user-defined-procedures-functions.adoc +++ b/modules/ROOT/pages/extending-neo4j/user-defined-procedures-functions.adoc @@ -315,41 +315,56 @@ The following examples show how to write a user-defined procedure, function, and [.include-with-procedure-example] ====== -The following is an example of a user-defined procedure that returns all nodes in the graph: +The following is an example of a user-defined procedure that returns the relationship types going in and out of a given node: [source, java] ---- package example; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; import java.util.stream.Stream; +import org.neo4j.graphdb.Direction; import org.neo4j.graphdb.Node; -import org.neo4j.graphdb.ResourceIterator; -import org.neo4j.graphdb.Transaction; +import org.neo4j.graphdb.Relationship; +import org.neo4j.logging.Log; import org.neo4j.procedure.Context; -import org.neo4j.procedure.Mode; +import org.neo4j.procedure.Description; +import org.neo4j.procedure.Name; import org.neo4j.procedure.Procedure; /** - * This is an example returning {@link org.neo4j.graphdb.Entity Entities} from stored procedures. - * {@link Node Nodes} and {@link org.neo4j.graphdb.Relationship relationships} are both entities - * and can only be accessed in their transaction. So it is important that you use the injected one - * and not open a new one; otherwise you can access them from the outside. + * This is an example showing how you could expose Neo4j's full text indexes as + * two procedures - one for updating indexes, and one for querying by label and + * the lucene query language. */ -public class EntityResultExample { - - @Context - public Transaction tx; - - public record EntityContainer(Node node) { - } +public class GetRelationshipTypes { + // This gives us a log instance that outputs messages to the + // standard log, normally found under `data/log/console.log` + @Context + public Log log; - @Procedure(name = "example.allnodes", mode = Mode.READ) - public Stream allnodes() { - ResourceIterator nodes = tx.execute("MATCH (n) RETURN n").columnAs("n"); - return nodes.stream().map(EntityContainer::new); - } -} + /** + * This procedure takes a Node and gets the relationships going in and out of it + * + * @param node The node to get the relationships for + * @return A RelationshipTypes instance with the relations (incoming and outgoing) for a given node. + */ + @Procedure(name = "example.getRelationshipTypes") + @Description("Get the different relationships going in and out of a node.") + public Stream getRelationshipTypes(@Name("node") Node node) { + List outgoing = new ArrayList<>(); + node.getRelationships(Direction.OUTGOING).iterator() + .forEachRemaining(rel -> AddDistinct(outgoing, rel)); + + List incoming = new ArrayList<>(); + node.getRelationships(Direction.INCOMING).iterator() + .forEachRemaining(rel -> AddDistinct(incoming, rel)); + + return Stream.of(new RelationshipTypes(incoming, outgoing)); + } ---- ====== From a3ea37efb5c0555dbe58d0d0c6dc5ca3e9ef986c Mon Sep 17 00:00:00 2001 From: Reneta Popova Date: Tue, 22 Sep 2026 12:58:33 +0100 Subject: [PATCH 2/2] remove the comment --- .../extending-neo4j/user-defined-procedures-functions.adoc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/modules/ROOT/pages/extending-neo4j/user-defined-procedures-functions.adoc b/modules/ROOT/pages/extending-neo4j/user-defined-procedures-functions.adoc index d00d1d3..dfe7a1a 100644 --- a/modules/ROOT/pages/extending-neo4j/user-defined-procedures-functions.adoc +++ b/modules/ROOT/pages/extending-neo4j/user-defined-procedures-functions.adoc @@ -335,11 +335,6 @@ import org.neo4j.procedure.Description; import org.neo4j.procedure.Name; import org.neo4j.procedure.Procedure; -/** - * This is an example showing how you could expose Neo4j's full text indexes as - * two procedures - one for updating indexes, and one for querying by label and - * the lucene query language. - */ public class GetRelationshipTypes { // This gives us a log instance that outputs messages to the // standard log, normally found under `data/log/console.log`