nodes
The nodes
module provides a comprehensive toolkit for managing multiple graph nodes, enabling linking, updating, type deduction and more.
Trait | Value |
---|---|
Module type | util |
Implementation | C++ |
Graph direction | directed/undirected |
Edge weights | weighted/unweighted |
Parallelism | sequential |
Procedures
You can execute this algorithm on graph projections, subgraphs or portions of the graph.
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:
-
subgraph: Graph
(OPTIONAL) ➡ A specific subgraph, which is an object of type Graph returned by theproject()
function, on which the algorithm is run. If subgraph is not specified, the algorithm is computed on the entire graph by default. -
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
andtypes
.node
representing the given node andtypes
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:
-
subgraph: Graph
(OPTIONAL) ➡ A specific subgraph, which is an object of type Graph returned by theproject()
function, on which the algorithm is run. If subgraph is not specified, the algorithm is computed on the entire graph by default. -
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:
-
subgraph: Graph
(OPTIONAL) ➡ A specific subgraph, which is an object of type Graph returned by theproject()
function, on which the algorithm is run. If subgraph is not specified, the algorithm is computed on the entire graph by default. -
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:
-
subgraph: Graph
(OPTIONAL) ➡ A specific subgraph, which is an object of type Graph returned by theproject()
function, on which the algorithm is run. If subgraph is not specified, the algorithm is computed on the entire graph by default. -
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: