Getting started with Cypher and RedisGraph / Part III
Tags: cypher, redisgraph, graph, redis
Published: 2023-05-29 08:16:00

Welcome to the last part of the "Getting started with Cypher and RedisGraph" series.
In the first post we looked at what a Graph DB is and why should you use one; the second post explained the basics of querying RedisGraph.

Now we will build a very small example to show everything we learned in the previous posts.

First of all, start redis Insight with `docker compose up -d`.

At this point you need python3 installed on your local machine; if you don't, check online the instructions for you OS.
You also need click and redis python extensions; you can install them with:
```sh
pip3 install click redis
```

Now that you have everything, you can clone the demo repository
```sh
git clone https://github.com/gfabrizi/redisgraph-demo.git
```

enter the cloned directory and launch the data import:
```sh
python3 bulk_insert.py Routes -n City.csv -r ROUTE.csv -h 127.0.0.1 -p 6379
```
you should see a message like this:
```sh
b'City'  [####################################]  100%
19 nodes created with label 'b'City''
b'ROUTE'  [####################################]  100%
81 relations created for type 'b'ROUTE''
Construction of graph 'Routes' complete: 19 nodes created, 81 relations created in 0.006071 seconds
```

## What just happened?
we used a script (`bulk_insert.py`) from the awesome repository `redis-dataset`:
https://github.com/redis-developer/redis-datasets
The script is used to import nodes (`-n` parameter) and relationships (`-r` parameter). You can specify more than one nodes and relationships file.
We imported nodes representing cities (`City.csv`) and relations (`ROUTE.csv`); the import script uses the csv filenames to give name to nodes and relations.
So now we have our graph "Routes" filled with 19 cities and 1786 routes.

It's time to open RedisInsight UI in the browser and start querying!

## Visualize data
Go to "Workbench" tab in RedisInsight and launch the generic query to show all nodes and relations:
```cypher
GRAPH.QUERY Routes "MATCH (a) RETURN a"
```
you should see a graph like this (remember to check the "All relationships" flag:

![Showing all nodes and edges](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kp6ya9djotatzyh9d0o9.png)

Not really helpful, right? 😔
Well, informations are there, now you have to query for what you want to know.

> **A little background:**
Years ago a company asked me to resolve a test for an interview as backend developer. I had 3-4 days to solve this: "Imagine you have 20 goods depots, connected through cargo truck routes (each depot is connected with a maximum of 3 nearby depots). Write a script that - given deposit A and deposit B - finds the best route between them".
I struggled for days with this test, trying to learn and implement Dijkstra algorithm... with no success at all! 🥲
Well, if I knew about GraphDBs at that time, I would have passed that test! 🔝

For example, if you want to know the shortest routes between Memphis and Phoenix, you could use this query:
```cypher
GRAPH.QUERY Routes "MATCH (source:City {name: 'Memphis'}), (destination:City {name: 'Phoenix'}) with source, destination MATCH p=allShortestPaths((source)-[:ROUTE*]->(destination)) RETURN nodes(p) as cities"
```
we wrote a first `MATCH` query, followeb by a second one; to use the matching nodes of the first `MATCH` in the second one, we need to specify them with the `with` keyword. This keyword is useful when you have to use one (or more) previously matched node in a following `MATCH`.
`allShortestPaths` is a function of `MATCH` that allows us to find every shortest paths between two nodes; if we choose to see results as text (and not the defaul graph) we could see that Redis found 2 paths:

![allShortestPaths match](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/clthg8up6dx3lyl25cll.png)

if we wanted only one shortest path, we could use the `shortestPath` function:
```cypher
GRAPH.QUERY Routes "MATCH (source:City {name: 'Memphis'}), (destination:City {name: 'Phoenix'}) RETURN shortestPath((source)-[:ROUTE*]->(destination))"
```
if you look at the textual output of the results, you'll notice that this time RedisGraph returned not only the nodes, but also the relationships between nodes.
If you only want the nodes involved in the `shortestPath`, change the query like this:
```cypher
GRAPH.QUERY Routes "MATCH (source:City {name: 'Memphis'}), (destination:City {name: 'Phoenix'}) RETURN nodes(shortestPath((source)-[:ROUTE*]->(destination)))"
```

Cool, right?

## Conclusion
This series showed the basic of RedisGraph and the Cypher language.
There is much more to explore and learn; if we look just at the "path algorithms", the official documentation offers many functions to use:
https://redis.io/docs/stack/graph/path_algorithm/#find-all-paths-from-a-if-the-trip-is-limited-to-10-kilometers

I hope you have followed this series and enjoyed it as much as I did!😊🎉