Intro to Redisgraph
Tags: redis, webdev, redisgraph, database
Published: 2020-09-17 14:33:53

Hey guys! This is a series about **RedisGraph**. For the people out there who are new to this term, it is the first queryable Property Graph database to use sparse matrices to represent the adjacency matrix in graphs and linear algebra to query the graph. Don't panic, it is not as difficult as it seems, in a nutshell, it is a graph-based database where we're going to handle data through nodes and relationships. For guys who have worked in Neo4j, this would be a piece of cake.You can visit the official site here  [Redisgraph](https://oss.redislabs.com/redisgraph/)

Let's see the primary features:
1. It is based on the Property Graph Model.
2. Nodes are treated as vertices and edges are treated as relationships, that may have attributes.
3. Nodes can be labeled.
4. Relationships have a relationship type.
5. Graphs are represented as sparse adjacency matrices, Cypher as a query language
6. Cypher queries are translated into linear algebra expressions


#Setting up redisgraph 
You can find the installation and setup process here [Setup Redisgraph](https://oss.redislabs.com/redisgraph/#building)



#Why should I use Redisgraph?
* **Faster** : Redisgraph provides faster access and retrieval of data. 

* **Straightforward** : Since it is based on nodes and relations, it is not a hard nut to crack.
* **Understandable** : Queries are simple commands and clear. 
* **Visual view** : To interact with data visually, Redislabs provides a service [RedisInsight](https://redislabs.com/redis-enterprise/redis-insight/?utm_source=google&utm_medium=paid-search&utm_term=redisinsight&utm_content=458400925344&utm_campaign=brand-intl-israelandindia-in-en&gclid=CjwKCAjwkoz7BRBPEiwAeKw3q36HB6z3H9Bpv09vT0S03Q_Amp4rq5SnCI4FWQtv0Ss8k387jP_42RoC6LcQAvD_BwE), we will dig deeper into that service later in our tutorial. 

#Supported Datatypes
* Integer
* NULL
* String
* Boolean
* Double

#Let's get our hands dirty

We take an example of creating employees of an organization and perform CRUD.

* **CREATE** 
```
    CREATE (e:emp{id:1, name: 'John', age: '24', designation: 'developer'}) RETURN e;

    CREATE (e:emp{id:2, name: 'Jessie', age: '27', designation: 'tester'}) RETURN e;
```
Here, 
    * e      -   alias of the emp, we can use it within this entire command anywhere we needed.
    * emp   -   name of the node.
    * data will be given as key-value pair (object).
    * RETURN - return the data after creation.

* **MATCH**

It matches all docs(documents) when no condition is given explicitly and when given, it retrieves only the docs matching the condition.

  To return the list of all employees
```
 MATCH (e:emp) RETURN e;
```

  To fetch on condition basis
```
MATCH (e:emp{id:1}) RETURN e;
```
__OR__

```
MATCH (e:emp) WHERE e.id = 1 RETURN e;
```
Both ways returns the doc of an employee having id 1. 

* **SET**
 
 To update a single property
```
 MATCH (e:emp{id: 1}) SET e.age=28 RETURN e;
```
 To update more than one property
```
MATCH (e:emp{id: 1}) SET e.age=28,e.designation=’senior developer’ RETURN e;
```

* **DELETE**

 To delete based on condition
```
MATCH (e:emp{id:2}) DELETE e;	
```
Boom! It deletes the employee with id 2.
 To delete all the docs
```
MATCH (e:emp) DELETE e;
```