# meta_util

A module that contains procedures describing graphs on a meta-level.

| Trait               | Value      |
| ------------------- | ---------- |
| **Module type**     | util       |
| **Implementation**  | Python     |
| **Parallelism**     | sequential |

## Procedures

> **Info**
>
> You can execute this algorithm on [graph projections, subgraphs or portions of the graph](https://memgraph.com/docs/advanced-algorithms/run-algorithms#run-procedures-on-subgraph).

### `schema()`

Knowing what kind of data, that is, what kind of nodes and relationships, are
stored inside the database and how they're connected can be helpful. Besides
that, each node or relationship can have a set of properties, and while loading
the data in the database, you should be sure that a certain amount of graph
objects has a particular property. That's where the number of graph objects with
a particular property (property count) might come in handy. 

The `schema()` procedure returns a list of distinct relationships connecting
distinct nodes, that is, a graph schema. If `include_properties` is set to
`true`, the graph schema will contain additional information about properties. 

#### Input:

- `subgraph: Graph` (**OPTIONAL**) ➡ A specific subgraph, which is an [object of type Graph](https://memgraph.com/docs/advanced-algorithms/run-algorithms#run-procedures-on-subgraph) returned by the `project()` function, on which the algorithm is run. 
If subgraph is not specified, the algorithm is computed on the entire graph by default.

- `include_properties: bool (default=false)` ➡ If set to `true`, the graph schema will include properties count information. 

#### Output:

- `nodes: List[Map]` ➡ List of distinct node objects with their count. If `include_properties` is set to `true`, the node object contains properties count too.
- `relationships: List[Map]` ➡ List of distinct relationship objects with their count. If `include_properties` is set to `true`, the relationship object contains properties count too.

#### Usage:

Get graph schema without properties count:

```cypher
CALL meta_util.schema() 
YIELD nodes, relationships 
RETURN nodes, relationships;
```

Get graph schema with properties count:

```cypher
CALL meta_util.schema(true) 
YIELD nodes, relationships 
RETURN nodes, relationships;
```

> **Info**
>
> The queries above will return results in the graph view only in Memgraph Lab
> versions >= 2.4.0. For earlier versions of the Memgraph Lab, call `UNWIND` on
> returned object properties nodes and edges.

## Example

### Create a graph
 

Create a graph by running the following Cypher query:

```cypher
CREATE (n:Person {name: "Kate", age: 27})-[:IS_FRIENDS_WITH]->(m:Person:Student {name: "James", age: 30, year: "second"})-[:STUDIES_AT]->(:University {name: "University of Vienna"})
WITH n, m
CREATE (n)-[:LIVES_IN]->(:City {name: "Zagreb"})<-[:LIVES_IN]-(m);
```

### Create a schema
 

Once the graph is created, run the following query to call the `schema` procedure:

```cypher
CALL meta_util.schema() 
YIELD nodes, relationships 
RETURN nodes, relationships;
```

The graph result of the `schema` procedure in Memgraph Lab:

![](https://memgraph.com/docs/pages/advanced-algorithms/available-algorithms/meta_util/meta_result.png)
![](https://memgraph.com/docs/pages/advanced-algorithms/available-algorithms/meta_util/meta_result_count_without_props.png)

Memgraph Lab can also return data results - a list of nodes and a list of relationships:

```json
[
   {
      "id": 0,
      "labels": [
         "Person"
      ],
      "properties": {
         "count": 1
      },
      "type": "node"
   },
   {
      "id": 1,
      "labels": [
         "Person",
         "Student"
      ],
      "properties": {
         "count": 1
      },
      "type": "node"
   },
   {
      "id": 2,
      "labels": [
         "University"
      ],
      "properties": {
         "count": 1
      },
      "type": "node"
   },
   {
      "id": 3,
      "labels": [
         "City"
      ],
      "properties": {
         "count": 1
      },
      "type": "node"
   }
]
```

Here is the obtained list of relationships:

```json
[
   {
      "end": 1,
      "id": 0,
      "label": "IS_FRIENDS_WITH",
      "properties": {
         "count": 1
      },
      "start": 0,
      "type": "relationship"
   },
   {
      "end": 3,
      "id": 1,
      "label": "LIVES_IN",
      "properties": {
         "count": 1
      },
      "start": 0,
      "type": "relationship"
   },
   {
      "end": 2,
      "id": 2,
      "label": "STUDIES_AT",
      "properties": {
         "count": 1
      },
      "start": 1,
      "type": "relationship"
   },
   {
      "end": 3,
      "id": 3,
      "label": "LIVES_IN",
      "properties": {
         "count": 1
      },
      "start": 1,
      "type": "relationship"
   }
]
```

### Create a schema including the properties count
 

Run the following code to call the `schema` procedure including the properties
count:

```cypher
CALL meta_util.schema(true) 
YIELD nodes, relationships 
RETURN nodes, relationships;
```

The graph result of the `schema` procedure seen in Memgraph Lab:

![Graph Result](https://memgraph.com/docs/pages/advanced-algorithms/available-algorithms/meta_util/meta_result.png)
![Graph Result Count](https://memgraph.com/docs/pages/advanced-algorithms/available-algorithms/meta_util/meta_result_count.png)

Memgraph Lab can also return data results - a list of nodes and a list of relationships:

```json
[
   {
      "id": 0,
      "labels": [
         "Person"
      ],
      "properties": {
         "count": 1,
         "properties_count": {
            "age": 1,
            "name": 1
         }
      },
      "type": "node"
   },
   {
      "id": 1,
      "labels": [
         "Person",
         "Student"
      ],
      "properties": {
         "count": 1,
         "properties_count": {
            "age": 1,
            "name": 1,
            "year": 1
         }
      },
      "type": "node"
   },
   {
      "id": 2,
      "labels": [
         "University"
      ],
      "properties": {
         "count": 1,
         "properties_count": {
            "name": 1
         }
      },
      "type": "node"
   },
   {
      "id": 3,
      "labels": [
         "City"
      ],
      "properties": {
         "count": 1,
         "properties_count": {
            "name": 1
         }
      },
      "type": "node"
   }
]
```

Here is the obtained list of relationships:

```json
[
   {
      "end": 1,
      "id": 0,
      "label": "IS_FRIENDS_WITH",
      "properties": {
         "count": 1,
         "properties_count": {}
      },
      "start": 0,
      "type": "relationship"
   },
   {
      "end": 3,
      "id": 1,
      "label": "LIVES_IN",
      "properties": {
         "count": 1,
         "properties_count": {}
      },
      "start": 0,
      "type": "relationship"
   },
   {
      "end": 2,
      "id": 2,
      "label": "STUDIES_AT",
      "properties": {
         "count": 1,
         "properties_count": {}
      },
      "start": 1,
      "type": "relationship"
   },
   {
      "end": 3,
      "id": 3,
      "label": "LIVES_IN",
      "properties": {
         "count": 1,
         "properties_count": {}
      },
      "start": 1,
      "type": "relationship"
   }
]
```
