A walk through to Neo4j
Neo4j
Concepts
1. What is index-free adjacency?
Index-free adjacency (IFA) is a property of Neo4j’s native graph engine that eliminates the need for complex joins and enables real-time traversals. With IFA, Neo4j stores nodes and relationships as objects that are linked to each other. Conceptually, the graph looks as follows:
Using IFA, the Neo4j query engine starts with the anchor of the query which is the Group node with the id of 3.
IFA is a key trait of a graph database that allows it to find the neighbors of any given node without having to consider the full set of relationships in the graph.
Relationship in Neo4j graph database are stored at
write time
while joins are computed atread time
in RDBMS.
2. Graph Elements
Element | Description |
---|---|
Node | A node will commonly represent an individual record, for example, a thing or a fact. |
Label | Nodes can have one or more labels. Labels provide a way to group nodes and also serve as a starting point for any database queries. |
Relationship | A relationship connects two nodes. Each relationship has exactly one start and end node. A relationship will have a single a type. |
Type | Relationships are identified by their type. |
Property | Both nodes and relationships can contain properties. A property is a key/value pair. |
3. Modeling Rules
- Nodes typically represent things. Examples of entities that could typically be represented as a node are: person, product, event, book or subway station.
- Relationships are typically verbs. We could use a relationship to represent a personal or professional connection (Person knows Person, Person married to Person), to state a fact (Person lives in Location, Person owns Car, Person rated Movie), or even to represent a hierarchy (Parent parent of Child, Software depends on Library).
- Verbs can also be nodes. A verb may be modeled as a node when one or more facts need to be associated with it. For example, you may want to group multiple product purchases through a single
(:Order)
node.
1. What is Graph Database?
Graph Database is a database that model the data in the form of graph. In here, the nodes of the graph represent the entities while relation depict the association of these nodes.
2. Environment Setup
Make a docker-compose.yml
file and write these lines.
|
|
3. Neo4j building blocks
Neo4j Graph Database has the following building blocks:
- Nodes: is a fundamental unit of a Graph. It contains properties with key value pairs.
- Properties: is a key-value pair to describe Graph Nodes and Relationships.
- Relationships: connects two nodes
- Labes: Label associates a common name to a set of nodes or relationships. A node or relationship can contain one or more labels. We can create new labels to existing nodes or relationships. We can remove the existing labels from the existing nodes or relationships.
- Data Browser
4. Neo4j-CQL Introduction
Neo4j CQL
- Is a query language for Neo4j Graph Database.
- Is a declarative pattern-matching language.
- Follows SQL like syntax.
- Syntax is very simple and in human readable format.
Neo4j Clauses
Neo4j Read Clauses
SN | Read Clauses | Usage |
---|---|---|
1 | MATCH | Used to search the data with a specified pattern |
2 | OPTIONAL MATCH | Same as match, the only difference being it can use nulls in case of missing part of patterns |
3 | WHERE | Used to add contents to the CQL queries |
4 | START | Used to find the starting points through the legacy indexes |
5 | LOAD CSV | Used to import data from CSV files |
Neo4j Write Clauses
SN | Write Clauses | Usage |
---|---|---|
1 | CREATE | Used to create Nodes, Relationships, and Properties |
2 | MERGE | Verifies whether specified pattern exists in the graph, If not, it creates the pattern |
3 | SET | Used to update labels on nodes, properties on nodes and relationships |
4 | DELETE | Used to delete nodes and relationships or paths |
5 | REMOVE | Used to remove properties and elements from nodes and relationships |
6 | FOREACH | Used to update the data within a list |
7 | CREATE UNIQUE | Using the clauses CREATE and MATCH, you can get a unique pattern by matching the existing pattern and creating the missing one |
Neo4j General Clauses
SN | General Clauses | Usage |
---|---|---|
1 | RETURN | Used to define what to include in the query result set |
2 | ORDER BY | Used to arrange the output of a query in order. It is used along with the clauses RETURN or WITH. |
3 | LIMIT | Used to limit the rows in the result to a specific value |
4 | SKIP | Used to define from which row to start including the rows in the output |
5 | WITH | Used to chain the query parts together |
6 | UNWIND | Used to expand a list into a sequence of rows |
7 | UNION | Used to combine the result of multiple queries |
8 | CALL | Used to invoke a procedure deployed in the database |
CQL Functions
SN | CQL | Usage |
---|---|---|
1 | String | Used to work with String literals. |
2 | Aggregation | Used to perform some aggregation operations on CQL Query results. |
3 | Relationship | Used to get details of relationships such as startnode, endnode, etc. |
CQL Data Types
SN | CQL | Data Type Usage |
---|---|---|
1 | Boolean | Used to represent Boolean literals: true, false. |
2 | byte | Used to represent 8-bit integers. |
3 | short | Used to represent 16-bit integers. |
4 | int | Used to represent 32-bit integers. |
5 | long | Used to represent 64-bit integers. |
6 | float | Used to represent 32-bit floating-point numbers. |
7 | double | Used to represent 64-bit floating-point numbers. |
8 | char | Used to represent 16-bit characters. |
9 | String | Used to represent Strings. |
CQL Operators
SN | Type | Operators |
---|---|---|
1 | Mathematical | +, -, *, /, %, ^ |
2 | Comparison | +, <>, <, >, <=, >= |
3 | Boolean | AND, OR, XOR, NOT |
4 | String | + |
5 | List | +, IN, [X], [X…..Y] |
6 | Regular Expression | =- |
7 | String | matching STARTS WITH, ENDS WITH, CONSTRAINTS |
Boolean Operators
SN | Boolean | Operators Description |
---|---|---|
1 | AND | Like SQL AND operator. |
2 | OR | Like SQL OR operator. |
3 | NOT | Like SQL NOT operator. |
4 | XOR | Like SQL XOR operator. |
Comparision Operators
SN | Boolean | Operators Description |
---|---|---|
1 | = | “Equal To” operator. |
2 | <> | “Not Equal To” operator. |
3 | < | “Less Than” operator. |
4 | > | “Greater Than” operator. |
5 | <= | “Less Than Or Equal To” operator. |
6 | >= | “Greater Than Or Equal To” operator. |
5. Creating Nodes
Creating a single node
Create node by simply specifying the name of the node that is to be created along with the CREATE
clause.
Syntax
|
|
Verification
|
|
Creating a multiple nodes
Syntax
|
|
Creating a Node with a Label
Syntax
|
|
Example
|
|
Creating a Node with Multiple Labels
Syntax
|
|
Example
|
|
Create Node with Properties
Syntax
|
|
Example
|
|
Returning the Created Node
Syntax
|
|
Example
|
|
6. Creating a Relationship
Creating Relationships
Syntax
|
|
Example
|
|
Creating a Relationship Between the Existing Nodes
Syntax
|
|
Example
|
|
Creating a Relationship with Label and Properties
Syntax
|
|
Example
|
|
Creating a complete path
Syntax
|
|
|
|
7. Merge Command
MERGE command is a combination of CREATE command and MATCH command.
Neo4j CQL MERGE command searches for a given pattern in the graph. If it exists, then it returns the results else it creates a new node/relationship and returns the results.
Syntax
|
|
Before proceeding to the examples, create these nodes,
|
|
Merging a Node with a Label
Syntax
|
|
Example 1
|
|
When you execute this query, Neo4j verifies whether there is any node with the label player. If not, it creates a node named “Jadeja” and returns it.
Example 2
|
|
Merging a Node with Properties
Syntax
|
|
|
|
OnCreate and OnMatch
Whenever, we execute a merge query, a node is either matched or created. Using on create and on match, you can set properties for indicating whether the node is created or matched.
Syntax
|
|
Merge a relationship
|
|
8. Set Clause
Add new properties to an existing Node or Relationship, and also add or update or remove
existing Properties values.
Setting a Property
Syntax
|
|
Before proceeding, seed this data
|
|
Now, set example,
|
|
Remove property
Syntax
|
|
Example
First create a node,
|
|
Now, remove property,
|
|
Setting multiple properties
Syntax
|
|
Example
|
|
Setting a Label on a Node
Syntax
|
|
First, create a node;
|
|
Now, set label to this node;
|
|
Setting Multiple Labels on a Node
Syntax
|
|
First, create a node,
|
|
Now, set multiple label,
|
|
9. Delete Clause
Deleting all Nodes and Relationships
Query
|
|
Deleting a Particular Node
Syntax
|
|
First, create a node.
|
|
Delete a node,
|
|
10. Remove Clause
DELETE
operation is used to delete nodes and associated relationships.REMOVE
operation is used to remove labels and properties.
Removing a Property
Syntax
|
|
First, create a node
|
|
Now, remove a property
|
|
Removing a Label From a Node
Syntax
|
|
Example
|
|
Removing Multiple Labels
Syntax
|
|
First, create a node
|
|
Now, remove a multiple labels;
|
|
11. Foreach Clause
The FOREACH
clause is used to update data within a list whether components of a path, or result of aggregation.
|
|
First, create a path,
|
|
12. Match Clause
Get All Nodes Using Match
Create Node, Relationships;
|
|
Return all nodes,
|
|
Getting All Nodes Under a Specific Label
Syntax
|
|
Example
|
|
Match by Relationship
Syntax
|
|
Example
|
|
13. Optional Match Clause
The OPTIONAL MATCH
clause is used to search for the pattern described in it, while using nulls
for missing parts of the pattern.
OPTIONAL MATCH
is similar to the match clause, the only difference being it returns null as a result of the missing parts of the pattern.
Syntax
|
|
Example
|
|
14. Where Clause
Like SQL, Neo4j CQL has provided WHERE clause in CQL MATCH command to filter the results of a MATCH Query.
Syntax
|
|
First, create nodes;
|
|
Now, Match;
|
|
WHERE Clause with Multiple Conditions
Syntax
|
|
Example
|
|
Using Relationship with Where Clause
|
|
15. Count Function
Assume we have created a graph in the database with the following details.
Count
Syntax
|
|
|
|
Result
Group Count
Example
|
|
Result
16. Return Clause
The RETURN
clause is used return nodes, relationships, and properties.
Returning Nodes
Syntax
|
|
Example Return node,
|
|
Returning Multiple Nodes
Example
|
|
Returning Relationships
Syntax
|
|
Example
|
|
Returning Properties
Syntax
|
|
Example
|
|
Returning All Elements
Example
|
|
Returning a Variable With a Column Alias
Example
|
|
17. Order By Clause
Syntax
|
|
First, create nodes;
|
|
Example
|
|
Ordering Nodes by Multiple Properties
Syntax
|
|
Example
|
|
18. Limit Clause
Syntax
|
|
Seed the data,
|
|
Order By clause;
|
|
Limit with expression
|
|
19. Skip Clause
Seed,
|
|
Example
|
|
Skip Using Expression
|
|
20. With Clause
Syntax
|
|
Example
|
|
21. Unwind Clause
Example
|
|
22. String Functions
SN | Function | Description |
---|---|---|
1 | UPPER | Used to change all letters into upper case letters. |
2 | LOWER | Used to change all letters into lower case letters. |
3 | SUBSTRING | Used to get substring of a given String. |
4 | Replace | Used to replace a substring with a given substring of a String. |
23. Aggregation Functions
SN | Function | Description |
---|---|---|
1 | COUNT | Returns the number of rows returned by MATCH command. |
2 | MAX | Returns the maximum value from a set of rows returned by MATCH command. |
3 | MIN | Returns the minimum value from a set of rows returned by MATCH command. |
4 | SUM | Returns the summation value of all rows returned by MATCH command. |
5 | AVG | Returns the average value of all rows returned by MATCH command. |
24. Indexing
Neo4j SQL supports Indexes on node or relationship properties to improve the performance of the application. We can create indexes on properties for all nodes, which have the same label name.
We can use these indexed columns on MATCH or WHERE or IN operator to improve the execution of CQL command.
Seed data,
|
|
Create a single-property range index for nodes
Syntax
|
|
Indexing,
|
|
Create a single-property range index for relationships
Syntax
|
|
Create a composite range index for nodes
Syntax
|
|
Create a composite range index for relationships
|
|
Create a range index only if it does not already exist
Syntax
|
|
DROP INDEX index_name [IF EXISTS]
Syntax
|
|
Text Index
Syntax
|
|
|
|
Create a node text index
|
|
Create a relationship text index
|
|
Create a text index only if it does not already exist
|
|
Create a text index specifying the index provider
|
|
For more information Indexing in Neo4j.
25. Constraints
Syntax
|
|
|
|
Example
|
|
References
- Tutorial point