Skip to content

πŸ“ˆ Learn how to build a graph database with Neo4j

Notifications You must be signed in to change notification settings

dwyl/learn-neo4j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Learn Neo4j [WORK IN PROGRESS!]

Build scalable databases that leverage relationships between data

neo logo

Why?

Neo4j harnesses the power of connections between data. It provides an efficient and intuitive way of working with data by mimicking how we naturally think about it.

  • Performance - extremely scalable, performance remains the same as your data grows
  • Flexibility - the database can be added to without endangering current functionality

Popular Use Cases

  • Fraud detection
  • Real-time recommendation engines
  • Master data management
  • Identity and access management
  • Related search

What?

"A highly scalable native graph database that leverages data relationships as first-class entities"

A graph database can store any kind of data using a few simple concepts:

  1. Nodes - graph data records
  2. Relationships - connect nodes (always have a direction and type)
  3. Properties - named data values

Neo4j can be queried for nodes (objects in the graph) and relationships (connections between objects_) with Cypher (Neo4j's query language).

nodes and edges
Both nodes and relationships can have name-value properties attached to them that can be used to give a more declarative description of what they are.

Background Links

Who?

Neo4j is perfect for those who are particularly concerned with not only the storage and retrieval of, but the connections and relationships between data.

Getting Started

Follow these steps to get up and running with your first Neo4j database:

  1. Download Neo4j and follow the installation instructions for your OS on the page that it takes you to after you click download (choose the community version if it's for personal use) download neo4j installation

  2. After you've followed the installation instructions, you can then start Neo4j which will then give you a local url that you can paste in your browser local url

  3. Enter the default login details - username = neo4j password=neo4j you'll then be prompted to update your password change password

  4. You should then see that you've successfully connected to Neo4j connect neo4j

  5. There are also a couple of great tutorial resources/slideshows that you can look at if you click on the dashboard on the left or the 'Start learning' button in the middle of the page. They will give you a bit of background information on Neo4j and Cypher dashboard

Cypher - Query language for Neo4j

Cypher is a very declarative query language meaning that you describe what you want to find, not how you want to find it. Let's look at a few examples of how we might implement it:

CREATE or DELETE - create or delete nodes and relationships
SET or REMOVE - set values to properties or labels on nodes
MERGE - Match existing or create new nodes and patterns. This is especially useful together with uniqueness constraints.

These are just a few but there are many many more that you can check out here

CREATE - create a node:

$ CREATE (d:Person { name: "David", from: "Canada" })

Now let's break the pieces down:

CREATE - claus to create data
() - parentheses to indicate a node
d:Person - a variable 'd' and label 'Person' {} - brackets to add properties to a node

Essentially we've just created a node d with a label of Person with the properties name: "David" and from: "Canada". If you run this command you'll have successfully created your first graph!

MATCH - find a node:

$ MATCH (d:Person) WHERE person.name = "David" RETURN d;

MATCH - clause to specify a pattern of nodes and relationships
(d:Person) a single node pattern with label 'Person' which will assign matches to the variable 'd'
WHERE clause to constrain the results
d.name = "David" - compares name property to the value "David"
RETURN - clause used to request particular results

CREATE clauses can create multiple nodes and relationships at once:

$ MATCH (d:Person) WHERE d.name = "David"    
CREATE (j:Person { name: "John", from: "Sweden", learn: "surfing" }),  
(i:Person { name: "Ian", from: "England", title: "author" }),  
(r:Person { name: "Rick", from: "Belgium", pet: "Orval" }),  
(a:Person { name: "Allison", from: "California", hobby: "surfing" }),  
(d)-[:KNOWS {since: 2001}]->(j),(d)-[:KNOWS {rating: 5}]->(i),  
(j)-[:KNOWS]->(i),(j)-[:KNOWS]->(r),  
(i)-[:KNOWS]->(j),(i)-[:KNOWS]->(a),  
(r)-[:KNOWS]->(a)

Here we're creating 4 more nodes (people) and then creating 6 relationships between them. Note that each relationship has a direction notated by ->. Just like with nodes, we can add properties to the relationships using curly braces:

For example if we wanted to say that David has known John since 2001 we could represent that as shown below (note that this doesn't mean that John knows David):
(d)-[:KNOWS {since: 2001}]->(j)

Now that we have some more data with some relationships, we can begin to query it in interesting ways!

Let's take this query as an example:

MATCH (d:Person)-[:KNOWS]-(friends)  
WHERE d.name = "David" RETURN d, friends

MATCH - clause to describe the pattern from known Nodes to found Nodes
(d) - starts the pattern with a Person (qualified by WHERE)
-[:KNOWS]- matches "KNOWS" relationships (in either direction)
(friends) - will be bound to David's friends

david friends

Recommendation - Using Patterns

With the graph we previously created above we can start to make recommendations based on the data. For example, John is learning to surf and so he might want to find a new friend that already does. We can query our graph as follows:

MATCH (j:Person)-[:KNOWS]-()-[:KNOWS]-(surfer)  
WHERE j.name = "John" AND surfer.hobby = "surfing"  
RETURN DISTINCT surfer  

Here we're saying, find me someone who knows one of John's friends who has surfing as a hobby.
() - empty parentheses means ignore those nodes
DISTINCT - returns a unique path because more than one will match the pattern
surfer - will contain Allison, a friend of a friend who surfs

If we wanted to take a closer look into what our query was doing, we can prepend it with either EXPLAIN or PROFILE for example:

PROFILE MATCH (js:Person)-[:KNOWS]-()-[:KNOWS]-(surfer)  
WHERE js.name = "Johan" AND surfer.hobby = "surfing"  
RETURN DISTINCT surfer

Which will show the following in the console: profile query

About

πŸ“ˆ Learn how to build a graph database with Neo4j

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published