Skip to content

Instantly share code, notes, and snippets.

@fferegrino
Last active May 25, 2022 07:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save fferegrino/f126fe1da3c073c5bafa9a4303457fd2 to your computer and use it in GitHub Desktop.
Save fferegrino/f126fe1da3c073c5bafa9a4303457fd2 to your computer and use it in GitHub Desktop.
My introduction to neo4j

My introduction to neo4j

Introduction to Neo4j

Let’s create our first basic graph! we’ll create something similar to what you saw in the video (or similar to what you see in this image).

3JiWZht

Of course, if you are not a Barcelona supporter you can change it to be whatever you want.

CREATE
      (m:Player{name:"Lionel Messi"}),
      (b:Team{name:"Barcelona"})
WITH m, b
CREATE (m)-[p:PlaysFor]->(b)
SET p.since=date("2001-02-01")
RETURN m, p, b

A network of friends

But the previous one was a rather simple example, how about we create a network of friends to see what else we can do…​

// Create nodes
CREATE (rob:Person{name:'Roberto'}), (isidro:Person{name:'Isidro'}),
      (tony:Person{name:'Antonio'}), (nora:Person{name:'Nora'}),
      (lily:Person{name:'Lilian'}), (freddy:Person{name:'Alfredo'}),
      (lucas:Person{name:'Lucas'}), (mau:Person{name:'Mauricio'}),
      (alb:Person{name:'Albina'}), (reg:Person{name:'Regina'}),
      (j:Person{name:'Joaquín'}), (julian:Person{name:'Julián'})
// Create relationships
CREATE
  (rob)-[:FriendsWith]->(isidro), (rob)-[:FriendsWith]->(tony), (rob)-[:FriendsWith]->(reg),
  (rob)-[:FriendsWith]->(mau), (rob)-[:FriendsWith]->(julian),
  (tony)-[:FriendsWith]->(reg), (tony)-[:FriendsWith]->(j),
  (alb)-[:FriendsWith]->(reg), (lily)-[:FriendsWith]->(isidro), (lily)-[:FriendsWith]->(j),
  (mau)-[:FriendsWith]->(lucas), (lucas)-[:FriendsWith]->(nora), (freddy)-[:FriendsWith]->(nora);

// Query the relationships
MATCH friendships=()-[:FriendsWith]-()
RETURN friendships

Mhmhmhmhmhmhh 🤔

Let’s find who are friends with Lucas (note that we don’t care about the direction of the relationship, we are not using an arrow in the relationship):

MATCH friends=(a:Person{name:'Lucas'})-[:FriendsWith]-(friend)
RETURN friends

What about the friends of friends of friends of Lucas? imagine doing someting like this in SQL…​

MATCH friends=(a:Person{name:'Lucas'})-[:FriendsWith*3]-(friend)
RETURN friend.name

Or something more advanced, which one is the shortest path between Joaquín and Lucas:

MATCH (lucas:Person{name:'Lucas'}), (joaquin:Person{name:'Joaquín'}),
  p = shortestPath((lucas)-[*]-(joaquin))
RETURN p

Now is your turn, go crazy with the graph above or create your own:

Created by Antonio Feregrino - Twitter | YouTube | LinkedIn

@AVBLANCO
Copy link

In the case with you have the idea of the insert a new node, type person and your respective relationships with another person. how is the correct sentences for make this relationships and insert the person.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment