Skip to content
This repository was archived by the owner on Sep 18, 2023. It is now read-only.

[master < E-nodes] Write docs for nodes #1016

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
223 changes: 223 additions & 0 deletions mage/query-modules/cpp/nodes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
---
id: nodes
title: nodes
sidebar_label: nodes
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import RunOnSubgraph from '../../templates/_run_on_subgraph.mdx';

export const Highlight = ({children, color}) => (
<span
style={{
backgroundColor: color,
borderRadius: '2px',
color: '#fff',
padding: '0.2rem',
}}>
{children}
</span>
);

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

[![docs-source](https://img.shields.io/badge/source-nodes-FB6E00?logo=github&style=for-the-badge)](https://github.com/memgraph/mage/tree/main/cpp/nodes_module)

| Trait | Value |
| ------------------- | ----------------------------------------------------- |
| **Module type** | <Highlight color="#FB6E00">**algorithm**</Highlight> |
| **Implementation** | <Highlight color="#FB6E00">**C++**</Highlight> |
| **Graph direction** | <Highlight color="#FB6E00">**directed**</Highlight>/<Highlight color="#FB6E00">**undirected**</Highlight> |
| **Edge weights** | <Highlight color="#FB6E00">**weighted**</Highlight>/<Highlight color="#FB6E00">**unweighted**</Highlight> |
| **Parallelism** | <Highlight color="#FB6E00">**sequential**</Highlight> |

### Procedures

### `relationship_types(nodes, 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:
- &lt;type - incoming relationship.
- type> - outgoing relationship.
- type - either way.

#### Input:

- `node: int|node|List[int|node]` ➡ input nodes given as nodes themselves or their IDs.
- `types: List[string] (default = [])` ➡ 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:

```cypher
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);
```

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

```plaintext
+---------------------------------------+
| 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(nodes)`

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:

```cypher
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:
```cypher
MATCH (n:Intern) WITH collect(n) as interns CALL nodes.delete(interns);
```

### `relationships_exist(nodes, relationships)`

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 provided below:
- &lt;type - incoming relationship.
- type> - outgoing relationship.
- type - both incoming and outgoing.

Any other syntax results in an exception.

#### Input:

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

#### Output:

- `result: Map` ➡ 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:

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

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

```plaintext
+-----------------------------------------------------------------------------------------------------------------------------------------+
| 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(nodes, type)`

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

#### Input:

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

#### Usage:

<Tabs
groupId="example"
defaultValue="visualization"
values={[
{label: 'Step 1: Input graph without linking', value: 'visualization'},
{label: 'Step 2: Cypher example', value: 'cypher'},
{label: 'Step 3: Resulting graph', value: 'result'},
]
}>
<TabItem value="visualization">

<img src={require('../../data/query-modules/cpp/nodes/graph_before.png').default}/>

</TabItem>

<TabItem value="cypher">

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

</TabItem>

<TabItem value="result">

<img src={require('../../data/query-modules/cpp/nodes/graph_after.png').default}/>

</TabItem>

</Tabs>


1 change: 1 addition & 0 deletions mage/templates/_mage_spells.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
| [llm_util](/mage/query-modules/python/llm-util) | Python | A module that contains procedures describing graphs in a format best suited for large language models (LLMs). |
| [meta_util](/mage/query-modules/python/meta-util) | Python | A module that contains procedures describing graphs on a meta-level. |
| [migrate](/mage/query-modules/python/migrate) | Python | A module that can access data from a MySQL, SQL Server or Oracle database. |
| [nodes](/mage/query-modules/cpp/nodes) | C++ | A module that provides a comprehensive toolkit for managing multiple graph nodes, enabling linking, updating, type deduction and more. |
| [periodic](/mage/query-modules/cpp/periodic) | C++ | A module containing procedures for periodically running difficult and/or memory/time consuming queries. |
| rust_example | Rust | Example of a basic module with input parameters forwarding, made in Rust. |
| [uuid_generator](/mage/query-modules/cpp/uuid-generator) | C++ | A module that generates a new universally unique identifier (UUID). |
Expand Down
1 change: 1 addition & 0 deletions sidebars/sidebarsMAGE.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module.exports = {
"query-modules/python/node2vec",
"query-modules/python/node2vec-online",
"query-modules/cpp/node-similarity",
"query-modules/cpp/nodes",
"query-modules/python/nxalg",
"query-modules/cpp/pagerank",
"query-modules/cpp/pagerank-online",
Expand Down