nodes

The nodes module provides a comprehensive toolkit for managing multiple graph nodes, enabling linking, updating, type deduction and more.

TraitValue
Module typeutil
ImplementationC++
Graph directiondirected/undirected
Edge weightsweighted/unweighted
Parallelismsequential

Procedures

relationship_types()

Returns a list of distinct relationship types of the given node(s) contained within the given list of types. If the list of types is empty, the procedure returns all distinct relationship types. Relationship types can also be directed:

  • <type - incoming relationship.
  • type> - outgoing relationship.
  • type - either way.

Input:

  • node: int|node|List[int|node] ➡ The input nodes given as nodes themselves or their IDs.
  • types: List[string] (default = []) ➡ A list of relationship types to filter by.

Output:

  • relationship_types: List[Map] ➡ Each list element is a map with two keys: node and types. node representing the given node and types a list of distinct relationship types contained within the given list of types for the corresponding node.

Usage:

The database contains the following graph objects:

CREATE (ivan: Intern {name: 'Ivan'})
CREATE (idora: Intern {name: 'Idora'})
CREATE (matija: Intern {name: 'Matija'})
MERGE (ivan)-[:KNOWS]->(idora)
MERGE (matija)-[:HEARS]->(idora)
MERGE (matija)-[:SEES]->(ivan);

To get the relationship types for multiple nodes, run the following query:

MATCH (n:Intern) 
WITH collect(n) as interns 
CALL nodes.relationship_types(interns, ["<KNOWS", "SEES>", "HEARS"])
YIELD relationship_types 
RETURN relationship_types;

Result:

+---------------------------------------+
|   relationship_types                  |
| [                                     |
|     {                                 |
|       "node": {                       |
|          "labels": [                  |
|             "Intern"                  |
|          ],                           |
|          "properties": {              |
|             "name": "Ivan"            |
|          },                           |
|          "type": "node"               |
|       },                              |
|       "types": []                     |
|     },                                |
|     {                                 |
|        "node": {                      |
|           "labels": [                 |
|              "Intern"                 |
|          ],                           |
|           "properties": {             |
|              "name": "Idora"          |
|           },                          |
|           "type": "node"              |
|        },                             |
|       "types": [                      |
|          "HEARS",                     |
|          "KNOWS"                      |
|       ]                               |
|    },                                 |
|    {                                  |
|        "node": {                      |
|           "labels": [                 |
|             "Intern"                  |
|          ],                           |
|          "properties": {              |
|             "name": "Matija"          |
|          },                           |
|          "type": "node"               |
|        },                             |
|        "types": [                     |
|          "SEES",                      |
|          "HEARS"                      |
|       ]                               |
|    }                                  |
| ]                                     |
+---------------------------------------+

delete()

Deletes the given node(s) from the graph. Equivalent to running a DETACH DELETE query.

Input:

  • nodes: int|node|List[int|node] - Nodes to be deleted given as nodes themselves or their IDs.

Usage:

The database contains the following graph objects:

CREATE (ivan: Intern {name: 'Ivan'})
CREATE (idora: Intern {name: 'Idora'})
CREATE (matija: Intern {name: 'Matija'})
MERGE (ivan)-[:KNOWS]->(idora)
MERGE (matija)-[:HEARS]->(idora)
MERGE (matija)-[:SEES]->(ivan);

The following query will delete all the created nodes and relationships:

MATCH (n:Intern) 
WITH collect(n) as interns 
CALL nodes.delete(interns);

relationships_exist()

Checks if relationships in the input list exist at the given nodes. Results are returned as a map, which contains two smaller maps. The first map represents the node, and the second map represents the relationship status map of the node. Relationships can be directed, and the syntax for direction specification is:

  • <type - incoming relationship.
  • type> - outgoing relationship.
  • type - both incoming and outgoing.

Any other syntax results in an exception.

Input:

  • nodes: List[Any] ➡ A list of input nodes. Elements of the list can be either nodes themselves or their IDs.
  • relationships: List[string] ➡ A list of relationships to be checked.

Output:

  • result: Map ➡ The result map, containing two smaller maps. The first map represents the node, and the second represents the status of relationships checked in the function. Example of the map: {"Node": {"id": 0, "labels":["Dog"], "properties": {},"type": "node"}, "Relationships_exist_status": {"RUNS": false}}.

Usage:

The database contains the following graph objects:

CREATE (d:Dog)-[l:LOVES]->(h:Human)-[t:TAKES_CARE_OF]->(d);

The following query will check if relationships in the input list exist at the given nodes:

MATCH (d:Dog), (h:Human)
CALL nodes.relationships_exist([d,id(h)], ["<LOVES","FOLLOWS"]) YIELD result RETURN result;

Result:

+-----------------------------------------------------------------------------------------------------------------------------------------+
| result                                                                                                                                  |
+-----------------------------------------------------------------------------------------------------------------------------------------+
| {"Node": {"id": 0,"labels": ["Dog"],"properties": {},"type": "node"},"Relationships_exist_status": {"<LOVES": false,"FOLLOWS": false}}  |               
+-----------------------------------------------------------------------------------------------------------------------------------------+
| {"Node": {"id": 1,"labels": ["Human"],"properties": {},"type": "node"},"Relationships_exist_status": {"<LOVES": true,"FOLLOWS": false}} |
+-----------------------------------------------------------------------------------------------------------------------------------------+

link()

Links the provided nodes sequentially with the relationship type provided in the input, essentially creating a linked list of nodes.

Input:

  • nodes: List[Node] ➡ A list of input nodes that need to be linked.
  • type: string ➡ The type of relationship that will link two nodes.

Usage:

The database contains the following graph objects:

Run the following query

MATCH (h:Human), (c:Cat), (m:Mouse), (e:Elephant) 
CALL nodes.link([e, m, c, h, e],"IS_AFRAID_OF");

The database changes to the following state: