In this article, we will learn about neo4j, the basic concepts of graphical database.
I. Concept of graph database
1. Example of a graph
Here we will look at a basic example of a graph (pictured below) to better understand the concept as well as the graph attributes.
Nodes are often used to represent entities. The simplest graph is the chart in which there is only one node. Back to the drawing above we can see
is a basic node.
Labels can be used to model the value domain of nodes together, usually we will group nodes of the same data type or attribute into a collection and then attach labels to them. . For example, all nodes that represent a user object can be labeled with labels : Users . Now you can conveniently work with neo4j through nodes that have these labels attached, such as finding all users whose names match ABC, etc.
Each node can have one or more lables, in the original example, the nodes will have the labels Person and Movie, respectively . We can see that each label now represents a different class of object. But what about in other problems when we want to represent different dimensions of the data? Now you can add labels to the nodes. The figure below illustrates the use of multiple labels for the same node.
Relationships are exactly as the name implies, or the relationship between nodes. In addition, relationships can also structure nodes into different structures, turn graphs into structure types like lists, trees, maps, or maybe compound entities. Composite entities are entities that have many complex links linked together.
Relationships will make the graph more meaningful, closer to the real problem.
Here we can see that there are two relationships (links) in the graph there are two linkages, these two links allow us to better understand the data we have.
5. Relationship types
Only one Relationship type is allowed for each relationship . In the opening example we used ACTED_IN and DIRECTED as two types of entity links. The roles attribute in the ACTED_IN link has an data structure of an array with only one element in it.
By using the ACTED_IN link, with Tom Hanks node we can understand this is the source node and the Forrest Gump node is the target node.
You can easily see that the Tom Hanks node is heading to the Forrest Gump node. Note that Relationships always have direction. A node can also have Relationships to itself. If we want to perform Tom Hanks KNOWNS to the node itself, we can perform the following:
Properties are a name-value pair, used to represent the properties of nodes as well as relationships . In the initial graph example we use the properties name and born for nodes with labels Person, title and released for the Movie node. Also use property roles in : ACTED_IN links.
Properties can store various types of data such as number , string and boolean with corresponding value ranges. To better understand this issue we can learn more about the Cypher manual
7. Traversals and paths
Traversals are a way of how to query graph databases. Graphical traversals mean browsing through all nodes by “following relationships” and following certain rules. In most cases we will just have to browse the subset of the graph without having to browse the entire graph.
In the opening example, if you want to find the movie that Tom Hanks acted , the traversal will start from the Tom Hanks node, follow the ACTED_IN relationship and find this relationship is associated with a node …. and finally Forrest Gump is the result we are looking for. (You can see the dashed line to get a better understanding)
Here we can easily see that the cost to find the result is 1. The shortest path is the length of zero. It is a single node and has no relationship (as shown below).
And this is the path with a length of 1: