[Big Data] Detailed explanation of the use of Neo4j graph database

Table of contents

1. Introduction to graph database

1.1 What is a graph database?

1.2 Why we need a graph database

1.3 Graph database application fields

2. Introduction to graph database Neo4j

2.1 Neo4j features

2.2 Advantages of Neo4j

3. Neo4j data model

3.1 Basics of graph theory

3.2 Property graph model

3.3 Neo4j’s building elements

3.3.1 Node

3.3.2 Properties

3.3.3 Relationship

3.3.4 Tags

4. Neo4j construction process

4.1 Building steps

4.1.1 Download image

4.1.2 Create directory

4.1.3 Start container

4.1.4 Access the neo4j web interface

5. Neo4j syntax usage

5.1 Data preparation

5.2 Use of Neo4j data operation commands

5.2.1 Create nodes

5.2.2 Create node and specify label

5.2.3 Create relationships between nodes

5.3 Query syntax

5.3.1 Query syntax

5.3.2 Basic data query

5.3.3 Relationship depth query

5.3.4 Paging query

5.3.5 Update data

5.3.6 Delete data

5.4 Index operations

5.4.1 Create index

5.4.2 Delete index

6. Write at the end


1. Introduction to graph database

1.1 What is a graph database?

With the development of social networking, e-commerce, finance, retail, Internet of Things and other industries, the relationships in real society have formed a complex and huge network of relationships, and it is difficult for traditional databases to handle relational operations. Even big data technology is facing As the amount of data continues to grow, computing power bottlenecks will also be faced when processing data relationships. Therefore, there is an urgent need for a database that supports the calculation of massive data relationships, and graph databases were born.

1.2 Why we need a graph database

Graph database is a NoSql database based on graph theory. Its data storage structure and data query method are based on graph theory. Graph database is mainly used to store more connection data. The following picture is an official one provided by neo4j About the relationship diagram of the actors in the movie.

In such a relationship map, if you want to check the relationship between Tom Tykwer and Frank Darab, it is estimated to be quite difficult to use a relational database.

There are many similar relationship scenarios, and some nodes even contain many attribute values. In applications like this, data nodes contain a large amount of structured, semi-structured, and even unstructured connection data. In a traditional RDBMS database, it is not simple to represent this kind of unstructured connection data. If we store this kind of connection data in an RDBMS database, retrieval or traversal is very difficult, and the performance is very inefficient. So to represent or store this kind of data that requires more connections, you should choose a graph database to do this.

Using a graph database, it is very easy to store this kind of data that requires more connections. It stores each profile data internally as node data, and forms relationships by connecting with adjacent nodes. This forms a relationship graph, making retrieval and traversal very easy and more efficient.

1.3 Graph database application fields

Graph databases are widely used in many fields:

  • Social field: used to manage social relationships and implement friend recommendations;
  • Retail field: implement product recommendations to provide buyers with a better shopping experience;
  • Logistics field: achieve precise management of logistics outlets and provide efficient and convenient intelligent logistics solutions;
  • Financial field: Risk control management can be carried out based on graph database in banking business;
  • ...

2. Introduction to graph database Neo4j

Neo4j is an open source NoSQL graph database implemented in Java. Written using scala and java. It has been developed since 2003 and the first version was officially released in 2007. Its source code is hosted on GitHtb. As a representative product in graph databases, Neo4j has been applied in many industry projects, such as network management, software analysis, organization and project management, social projects, etc.

Official website: https://neo4j.com

2.1 Neo4j features

Neo4j has the following features

  • Neo4j implements the storage of professional database-level graph data models and provides complete database features, including ACID transaction support, cluster support, backup and failover, etc.;
  • Neo4j provides a declarative query language, Cypher, which is similar to the SQL language in relational databases. It has the characteristics of rich expressiveness, simple use, high query efficiency, and high scalability;

The data format stored by Neo4j is shown in the figure below

The complete feature summary is as follows:

  • Query SQL, Neo4j CQL, is provided. SQL is like a simple query language;
  • It follows the property graph data model;
  • It supports indexing through the use of Apache Lucence;
  • It supports UNIQUE constraints;
  • It contains a UI interface for executing SQL: Neo4j browser;
  • It supports complete ACID rules;
  • It uses native graphics library and local GPE (Graphics Processing Engine);
  • It supports exporting queried data to json and xls formats;
  • It provides a REST API that can be accessed by any programming language;
  • It provides java scripts that can be accessed through any UI MVC (such as node js);
  • It supports two Java APIs, cypher API and native API to access Java applications;

2.2 Advantages of Neo4j

In summary, Neo4j has the following advantages:

  • It easily represents relationships between connected data nodes;
  • Retrieving/traversing/navigating more connected data is simple and performance-efficient;
  • It easily represents semi-structured data;
  • The commands of the Neo4j CQL query language are user-friendly and highly readable, making it easy for beginners to master;
  • The data model is simple yet powerful;
  • It does not require complex joins to retrieve data with related relationships because it can easily retrieve data nodes with adjacent relationships;

3. Neo4j data model

Before starting to learn neo4j, it is necessary to have a comprehensive and in-depth understanding of the neo4j data model, because in most cases, subsequent operations will revolve around various concepts in the neo4j data model.

3.1 Basics of graph theory

A graph is composed of a set of nodes and the relationships connecting these nodes. The graph stores data in nodes and relationships in the form of attributes. Attributes are used to represent key-value pairs of data. In graph theory, a circle with a circle can be used to represent it. Represented by nodes, the relationship between nodes is marked with an arrow. The simplest graph is a single node;

For example, nodes can be used to represent social networks. A simple node does not have any attributes. For example, if we assume that this node represents a person in the social network, we can fill it with some information about this person as node attributes;

 

The following figure can be used to represent the relationship between two nodes. Here, the relationship between the two configuration files is "Follows". From a relationship perspective, Node1 follows Node2;

3.2 Property graph model

Neo4j graph database follows the property graph model to store and manage data. The rules of the property graph model are as follows:

  • Represent data in nodes, relationships, and attributes;
  • Both nodes and relationships contain attributes;
  • Relationship connection node;
  • Properties are key-value pairs;
  • Nodes are represented by a circle, and relationships are represented by directional keys (arrows in the figure);
  • Relationships have direction, one-way or two-way;
  • Each relationship contains a "start node" or "from node", and a "to node" or "end node";

In property graph data model, relationships should be directional. If we try to create a relationship without direction, it will throw an error message. In Neo4j, relationships should also be directional. If we try to create a relationship without direction, N4o4j will throw an error message, "Relationship should be directional".

Neo4j graph database stores all data in nodes and relationships, so we don’t need any additional RDBMS database or NoSql database to store Neo4j database data, it stores data in the form of graph, Neo4j adopts native GPE (Graph Processing Engine), to use its native graph storage format.

The main modules of the graph database data model include:

  • node;
  • relation;
  • Attributes;

The picture below is a simple relationship diagram

In the above figure, circles are used to represent nodes and arrows are used to represent relationships. Relationships are directional, and attributes in nodes are usually represented by key/value pairs.

3.3 Neo4j’s building elements

Neo4j graph database mainly consists of the following elements:

  • node;
  • Attributes;
  • relation;
  • Label;
  • data browser;

As shown in the figure below, if Person represents a node, the figure below represents the relationship between two Persons

3.3.1 Node

Node is the most basic element in the graph database, used to represent an entity record, just like a piece of data in a relational database. In Neo4j, a node can contain multiple attributes and multiple labels.

  • Nodes are the main data elements;
  • Nodes are connected to other nodes through relationships;
  • Nodes can have one or more attributes (i.e. attributes stored as key-value pairs);
  • Nodes have one or more labels that describe their role in the diagram;

3.3.2 Properties

Attributes are key-value pairs used to describe graph nodes and relationships, where key is a string and the value can be represented by using the data types supported in N4o4j

  • Properties are named values ​​where the name (key) is a string;
  • Properties can be indexed and constrained;
  • Composite indexes can be created from multiple attributes;

3.3.3 Relationship

Relationships are also basic elements in graph databases. When nodes already exist in the database, they need to be connected to form a graph. Relationships are used to connect two nodes. Relationships are also called edges in graph theory. Their beginning and end must be Nodes and relationships cannot point to null or originate from null. Relationships, like nodes, can contain multiple attributes, but relationships can only have one type.

  • A relationship connects two nodes;
  • Relationships are directional;
  • Nodes can have multiple relationships or even recursive relationships;
  • Relationships can also have one or more attributes (stored as key-value pairs);

Based on directionality, there are two main types of Neo4j relationships:

  • One-way relationship;
  • two-way relationship;

3.3.4 Tags

Label associates a common name with a set of nodes or relationships. A node or relationship can contain one or more labels. We can create new labels for existing nodes or relationships, or from existing nodes or relationships. Delete tag.

  • Labels are used to group nodes;
  • A node can have multiple labels;
  • Indexing labels can speed up search efficiency in the graph;
  • Native tag indexing is optimized for speed;

4. Neo4j construction process

Neo4j supports deployment and installation on many platforms, such as Windows, Mac, Linux and other systems. Neo4j is based on the Java platform, so before deployment and installation, make sure that the Java virtual machine has been installed. For the convenience of demonstration, we use docker to deploy Neo4j.

4.1 Building steps

Follow the steps below to set up its Neo4j environment.

4.1.1 Download image

docker pull neo4j:4.4.5

4.1.2 Create directory

Create relevant directories, such as data directory, configuration file directory, etc., as a mapping to the container. Create a neo4j directory under usr/local, and then create four directories: data, logs, conf, and import respectively in the neo4j directory .

data - the folder where the data is stored
logs - the running log folder
conf - the database configuration folder
import - in order to import csv in large batches to build the database, only files in .csv format can be imported, and they must be placed in this folder Down)

4.1.3 Start container

Start the neo4j container using the following command

docker run -d --name neo4j_self \
-p 7474:7474 -p 7687:7687 \
-v /usr/local/neo4j/data:/data \
-v /usr/local/neo4j/logs:/logs \
-v /usr/local/neo4j/conf:/var/lib/neo4j/conf \
-v /usr/local/neo4j/import:/var/lib/neo4j/import \
docker.io/neo4j:4.4.5

4.1.4 Access the neo4j web interface

After the container starts normally, enter the local address and port number to open the neo4j browser. If it is a cloud server, you need to open the above two ports in advance, as follows:

By default, the first login account/password is neo4j/neo4j. After successful login, you will be redirected to the following interface to prompt you to change your password;

After the password modification is completed, go to the interface below and start operating using neo4j's visual interface.

5. Neo4j syntax usage

5.1 Data preparation

Following the web browser interface accessed above, click Open guide in the middle to import the officially provided test data;

When you reach the second step, click on the data in the left area, and you will see a statement similar to creating a database on the right side. This is the generated test data;

Click the execute button in the upper right corner. After the import is successful, you can see the generated visualization data.

5.2 Use of Neo4j data operation commands

Cypher is a Neo4j query language, similar to SQL in relational databases. Some keywords are derived from SQL, such as: CREATE, WHERE, RETRUN, etc. Official document: MATCH - Cypher Manual. Let’s practice using the above imported test data as an example. Use common Neo4j operation commands.

5.2.1 Create nodes

Create a node. The node has the name attribute, and n is the variable of the node. After the creation is completed, the node is returned. The syntax is as follows

CREATE (n {name: $value}) RETURN n

For example, if we create a business outlet

CREATE (n {name:'Hangzhou Gongshu Sales Department'})

After the creation is completed, you can use the query command to see this data. Of course, it is currently an isolated data node;

 

You can also switch between different viewing dimensions on the left, for example, it is displayed in json form as follows, including an attribute but no label;

 

5.2.2 Create node and specify label

As mentioned above, nodes usually have label information. You can specify labels for the created nodes through the following statement:

CREATE (n:$Tag {name: $value}) 

 For example, specify the label of this node as middle, and then execute it again

CREATE (n:middle {name:'Hangzhou West Lake Sales Department'})

Looking at its details, you can see that the current data also contains label middle information.

5.2.3 Create relationships between nodes

 grammar

#创建n指向m的关系,并且指定关系类型为:KNOWS,r后紧跟着的就是关系名称
CREATE (n)-[r:KNOWS]->(m)  

Example requirement: Create two bank branches, a first-level bank branch and a second-level bank branch, with the following statements;

CREATE (n:first_level {name:'Hangzhou Bank Headquarters'}) -[r:manager]-> (m:second_level {name:'Hangzhou Bank West Lake Sales Department'}) RETURN n,r,m

Execute the above statement and see the following effect

5.3 Query syntax

Query can be said to be the most frequently used syntax in daily work. In order to practice query-related syntax in an all-round way, first prepare the following simulation test data, which is a set of logistics transfer test data;

CREATE (北京市转运中心:OLT {bid: 8001, name: "北京市转运中心", address: "北京市转运中心", location : point({latitude:39.904179, longitude:116.407387})})
CREATE (上海市转运中心:OLT {bid: 8002, name: "上海市转运中心", address: "上海市转运中心", location : point({latitude:31.230525, longitude:121.473667})})
CREATE (南京市转运中心:OLT {bid: 8003, name: "南京市转运中心", address: "南京市转运中心", location : point({latitude:32.059344, longitude:118.796624})})
CREATE (太原市转运中心:OLT {bid: 8004, name: "太原市转运中心", address: "太原市转运中心", location : point({latitude:37.870451, longitude:112.549656})})
CREATE (郑州市转运中心:OLT {bid: 8005, name: "郑州市转运中心", address: "郑州市转运中心", location : point({latitude:34.745551, longitude:113.624321})})

CREATE
    (北京市转运中心)-[:IN_LINE {cost:10684.9}]->(上海市转运中心),
    (北京市转运中心)<-[:OUT_LINE {cost:10684.9}]-(上海市转运中心),
    (北京市转运中心)-[:IN_LINE {cost:8993.1}]->(南京市转运中心),
    (北京市转运中心)<-[:OUT_LINE {cost:8993.1}]-(南京市转运中心),
    (南京市转运中心)-[:IN_LINE {cost:2699.4}]->(上海市转运中心),
    (南京市转运中心)<-[:OUT_LINE {cost:2699.4}]-(上海市转运中心),
    (太原市转运中心)-[:IN_LINE {cost:3609.7}]->(郑州市转运中心),
    (太原市转运中心)<-[:OUT_LINE {cost:3609.7}]-(郑州市转运中心),
    (郑州市转运中心)-[:IN_LINE {cost:5659.7}]->(南京市转运中心),
    (郑州市转运中心)<-[:OUT_LINE {cost:5659.7}]-(南京市转运中心)

CREATE (昌平区转运中心:TLT {bid: 90001, name: "昌平区转运中心", address: "昌平区转运中心", location : point({latitude:40.220952, longitude:116.231034})})
CREATE (北京市昌平区新龙城:AGENCY {bid: 100260, name: "北京市昌平区新龙城", address: "龙跃苑四区3号楼底商", phone : "010-53049073,010-53576707", location : point({latitude:40.07544443596149, longitude:116.3470535709328})})
CREATE
    (北京市昌平区新龙城)-[:IN_LINE {cost:189.7}]->(昌平区转运中心),
    (北京市昌平区新龙城)<-[:OUT_LINE {cost:189.7}]-(昌平区转运中心)
CREATE (北京市昌平区定泗路:AGENCY {bid: 100280, name: "北京市昌平区定泗路", address: "北七家镇定泗路苍龙街交叉口", phone : "010-86392987", location : point({latitude:40.11765281246394, longitude:116.37212849638287})})
CREATE
    (北京市昌平区定泗路)-[:IN_LINE {cost:166.2}]->(昌平区转运中心),
    (北京市昌平区定泗路)<-[:OUT_LINE {cost:166.2}]-(昌平区转运中心)
CREATE (海淀区转运中心:TLT {bid: 90002, name: "海淀区转运中心", address: "海淀区转运中心", location : point({latitude:39.959893, longitude:116.2977})})
CREATE (北京市海淀区小营:AGENCY {bid: 100347, name: "北京市海淀区小营", address: "北京市昌平区回龙观街道金燕龙大厦停车场", phone : "010-86483817,010-86483817,010-86483817", location : point({latitude:40.06177798692319, longitude:116.32706587559049})})
CREATE
    (北京市海淀区小营)-[:IN_LINE {cost:116.1}]->(海淀区转运中心),
    (北京市海淀区小营)<-[:OUT_LINE {cost:116.1}]-(海淀区转运中心)
CREATE (北京市海淀区万泉河:AGENCY {bid: 100227, name: "北京市海淀区万泉河", address: "北京市海淀区四季青镇杏石口路47号院", phone : "18521852356", location : point({latitude:39.94882822425318, longitude:116.25707017441161})})
CREATE
    (北京市海淀区万泉河)-[:IN_LINE {cost:36.8}]->(海淀区转运中心),
    (北京市海淀区万泉河)<-[:OUT_LINE {cost:36.8}]-(海淀区转运中心)
CREATE
    (昌平区转运中心)-[:IN_LINE {cost:383.3}]->(北京市转运中心),
    (昌平区转运中心)<-[:OUT_LINE {cost:383.3}]-(北京市转运中心),
    (海淀区转运中心)-[:IN_LINE {cost:112.3}]->(北京市转运中心),
    (海淀区转运中心)<-[:OUT_LINE {cost:112.3}]-(北京市转运中心)
CREATE (浦东新区转运中心:TLT {bid: 90003, name: "浦东新区转运中心", address: "浦东新区转运中心", location : point({latitude:31.221461, longitude:121.544346})})
CREATE (上海市浦东新区南汇:AGENCY {bid: 210057, name: "上海市浦东新区南汇", address: "园春路8号", phone : "18821179169", location : point({latitude:31.035240152911637, longitude:121.73459966751048})})
CREATE
    (上海市浦东新区南汇)-[:IN_LINE {cost:275.4}]->(浦东新区转运中心),
    (上海市浦东新区南汇)<-[:OUT_LINE {cost:275.4}]-(浦东新区转运中心)
CREATE (上海市浦东新区周浦:AGENCY {bid: 210127, name: "上海市浦东新区周浦", address: "川周公路3278-8号", phone : "021-68060322", location : point({latitude:31.132409729356993, longitude:121.59815370294322})})
CREATE
    (上海市浦东新区周浦)-[:IN_LINE {cost:111.6}]->(浦东新区转运中心),
    (上海市浦东新区周浦)<-[:OUT_LINE {cost:111.6}]-(浦东新区转运中心)
CREATE (奉贤区转运中心:TLT {bid: 90004, name: "奉贤区转运中心", address: "奉贤区转运中心", location : point({latitude:30.918406, longitude:121.473945})})
CREATE (上海市奉贤区东部:AGENCY {bid: 210017, name: "上海市奉贤区东部", address: "上上海市奉贤区洪庙镇洪兰路351", phone : "021-57171717", location : point({latitude:30.917752751719863, longitude:121.67587819184698})})
CREATE
    (上海市奉贤区东部)-[:IN_LINE {cost:192.9}]->(奉贤区转运中心),
    (上海市奉贤区东部)<-[:OUT_LINE {cost:192.9}]-(奉贤区转运中心)
CREATE (上海市奉贤区青村:AGENCY {bid: 210442, name: "上海市奉贤区青村", address: "姚家村1127号", phone : "021-57566663,021-57566606", location : point({latitude:30.916946897994983, longitude:121.57954144207972})})
CREATE
    (上海市奉贤区青村)-[:IN_LINE {cost:100.9}]->(奉贤区转运中心),
    (上海市奉贤区青村)<-[:OUT_LINE {cost:100.9}]-(奉贤区转运中心)
CREATE
    (浦东新区转运中心)-[:IN_LINE {cost:68.0}]->(上海市转运中心),
    (浦东新区转运中心)<-[:OUT_LINE {cost:68.0}]-(上海市转运中心),
    (奉贤区转运中心)-[:IN_LINE {cost:347.4}]->(上海市转运中心),
    (奉贤区转运中心)<-[:OUT_LINE {cost:347.4}]-(上海市转运中心)
CREATE (玄武区转运中心:TLT {bid: 90004, name: "玄武区转运中心" , location : point({latitude:32.048644, longitude:118.797779})})
CREATE (江苏省南京市玄武区紫金墨香苑:AGENCY {bid: 25073, name: "江苏省南京市玄武区紫金墨香苑", address: "栖霞区燕尧路100号", phone : "025-58765331,025-83241955,025-83241881", location : point({latitude:32.117016089520305, longitude:118.86319310255513})})
CREATE
    (江苏省南京市玄武区紫金墨香苑)-[:IN_LINE {cost:98.0}]->(玄武区转运中心),
    (江苏省南京市玄武区紫金墨香苑)<-[:OUT_LINE {cost:98.0}]-(玄武区转运中心)
CREATE (江苏省南京市玄武区长江路:AGENCY {bid: 25023, name: "江苏省南京市玄武区长江路", address: "观音阁10号", phone : "18521133265,18695799166", location : point({latitude:32.04803554410631, longitude:118.79190455263355})})
CREATE
    (江苏省南京市玄武区长江路)-[:IN_LINE {cost:5.6}]->(玄武区转运中心),
    (江苏省南京市玄武区长江路)<-[:OUT_LINE {cost:5.6}]-(玄武区转运中心)
CREATE
    (玄武区转运中心)-[:IN_LINE {cost:12.0}]->(南京市转运中心),
    (玄武区转运中心)<-[:OUT_LINE {cost:12.0}]-(南京市转运中心)

CREATE (小店区转运中心:TLT {bid: 90005, name: "小店区转运中心" , location : point({latitude:37.736865, longitude:112.565524})})
CREATE (山西省太原市青龙:AGENCY {bid: 351068, name: "山西省太原市青龙", address: "长治路33号经典家园停车场内13号商铺", phone : "0351-2025888", location : point({latitude:37.83589608758359, longitude:112.56059258109424})})
CREATE
    (山西省太原市青龙)-[:IN_LINE {cost:110.3}]->(小店区转运中心),
    (山西省太原市青龙)<-[:OUT_LINE {cost:110.3}]-(小店区转运中心)
CREATE (山西省太原市长风街:AGENCY {bid: 351045, name: "山西省太原市长风街", address: "平阳路104号省农机公司院内", phone : "18636100730", location : point({latitude:37.809964384001226, longitude:112.55299317699505})})
CREATE
    (山西省太原市长风街)-[:IN_LINE {cost:82.1}]->(小店区转运中心),
    (山西省太原市长风街)<-[:OUT_LINE {cost:82.1}]-(小店区转运中心)
CREATE
    (小店区转运中心)-[:IN_LINE {cost:149.4}]->(太原市转运中心),
    (小店区转运中心)<-[:OUT_LINE {cost:149.4}]-(太原市转运中心)

CREATE (中原区转运中心:TLT {bid: 90006, name: "中原区转运中心" , location : point({latitude:34.74828, longitude:113.612966})})
CREATE (河南省郑州市郑上路:AGENCY {bid: 371067, name: "河南省郑州市郑上路", address: "中原西路西四环西北角", phone : "0371-55116757,0371-68014786", location : point({latitude:34.74753024533005, longitude:113.57428550005442})})
CREATE
    (河南省郑州市郑上路)-[:IN_LINE {cost:35.4}]->(中原区转运中心),
    (河南省郑州市郑上路)<-[:OUT_LINE {cost:35.4}]-(中原区转运中心)
CREATE (河南省郑州市颍河路:AGENCY {bid: 371086, name: "河南省郑州市颍河路", address: "航海西路与西三环交叉口向南300米路西中贸商务", phone : "19139415556", location : point({latitude:34.71593280680163, longitude:113.60398506929064})})
CREATE
    (河南省郑州市颍河路)-[:IN_LINE {cost:36.9}]->(中原区转运中心),
    (河南省郑州市颍河路)<-[:OUT_LINE {cost:36.9}]-(中原区转运中心)
CREATE
    (中原区转运中心)-[:IN_LINE {cost:11.5}]->(郑州市转运中心),
    (中原区转运中心)<-[:OUT_LINE {cost:11.5}]-(郑州市转运中心)

After the execution is completed, use the query statement to see the following effect. Different node colors indicate different labels, which are specifically presented in the data;

5.3.1 Query syntax

Cypher query syntax is as follows:

[MATCH WHERE]  //条件查询
[WITH [ORDER BY] [SKIP] [LIMIT]] //查询的结果以管道的形式传递给下面的语句,聚合查询必须使用WITH
RETURN [ORDER BY] [SKIP] [LIMIT] //返回、排序、跳过、返回个数

When you see this, do you think it is similar to the query statement of mysql?

5.3.2 Basic data query

Query all data (use with caution when the amount of data is large)

MATCH (n) RETURN n 

Query all label data. In the above data, the label is logistics outlet data.

MATCH (n:AGENCY) RETURN n //Query all outlets (AGENCY)

Query all nodes related to "Beijing Transfer Center"

MATCH (n:OLT {name: "北京市转运中心"}) -- (m) RETURN n,m

Syntax description:

  • OLT, the label name corresponding to the transfer center;
  • name, node name;
  • -- , indicates a relationship. No arrow pointing indicates that all data related to the query node will be returned;

Query all first-level transshipment centers associated with "Beijing Transshipment Center"

MATCH (n:OLT {name:"北京市转运中心"}) --> (m:OLT) RETURN n,m

Specify the relationship label query, and the specified relationship name in the query relationship is: IN_LINE

MATCH (n:OLT {name:"北京市转运中心"}) -[r:IN_LINE]- (m) RETURN n,r,m

Query assignment and variables

The query result can be assigned to a variable to facilitate subsequent business logic processing;  

MATCH p = (n:OLT {name:"北京市转运中心"}) --> (m:OLT) RETURN p

Query the relationship type through the type() function

Return the relationship between two transfer centers through the following sql

MATCH (n:OLT {name:"北京市转运中心"}) -[r]-> (m:OLT {name:"南京市转运中心"}) RETURN type(r)

5.3.3 Relationship depth query

You can specify the depth of the relationship for query. The syntax format is: -[:TYPE*minHops..maxHops]->. In actual business, we often encounter the depth of mining a certain dimension or indicator of a node. In similar scenarios, we need to use the syntax of relationship depth query.

Query the nodes in the [Beijing Transfer Center] relationship with a depth of 1~2 levels

The following 3 syntaxes are available

#语法1
MATCH (n:OLT {name:"北京市转运中心"}) -[*1..2]->(m) RETURN *
#语法2
MATCH (n:OLT {name:"北京市转运中心"}) -[*..2]->(m) RETURN *
#语法3
MATCH path = (n:OLT {name:"北京市转运中心"}) -[*..2]->(m)
RETURN path

To put it simply, with "Beijing Transfer Center" as the center, the query effect shows that all the relationship data corresponding to the length of one or two points will be returned;

Query node relationship data

relationships() gets the relationships in the results, WITH passes the data backwards

MATCH path = (n:OLT {name:"北京市转运中心"}) -[*..2]->(m)
WITH n,m, relationships(path) AS r
RETURN r

What is returned is a relational result set. Through the with function, the query results obtained in the previous step can be further filtered, filtered or processed, so that only the expected results are returned;

Query all routes between two outlets, the maximum depth is 6

You can query 2 routes, where where specifies specific filtering conditions, just like specifying a specific field value equal to a certain number in mysql;

MATCH path = (n:AGENCY) -[*..6]->(m:AGENCY)
WHERE n.name = "北京市昌平区定泗路" AND m.name = "上海市浦东新区南汇"
RETURN path

Query the shortest path between two outlets, specifying a maximum query depth of 10

Note the function used here: shortestPath

MATCH path = shortestPath((n:AGENCY) -[*..10]->(m:AGENCY))
WHERE n.name = "北京市昌平区定泗路" AND m.name = "上海市浦东新区南汇"
RETURN path

Query the lowest cost route among all routes between two outlets

The maximum depth specified here is 10 (if the cost is the same, the transshipment node is the least)

MATCH path = (n:AGENCY) -[*..10]->(m:AGENCY)
WHERE n.name = "北京市昌平区定泗路" AND m.name = "上海市浦东新区南汇"
UNWIND relationships(path) AS r
WITH sum(r.cost) AS cost, path
RETURN path ORDER BY cost ASC, LENGTH(path) ASC LIMIT 1

Additional instructions:

  • UNWIND is an operation to expand list data;
  • sum() is an aggregate statistical function, similar to: avg(), max(), min(), etc.;
  • relationships() function to obtain detailed information about attributes;

The query results are as follows, similar to the query results above;

5.3.4 Paging query

When the amount of data is particularly large, paging query is a good choice. For example, using the above numbers, the following two scenario queries

#分页查询网点,按照bid正序排序,每页查询2条数据,第一页
MATCH (n:AGENCY) 
RETURN n ORDER BY n.bid ASC SKIP 0 LIMIT 3

#第二页
MATCH (n:AGENCY) 
RETURN n ORDER BY n.bid ASC SKIP 3 LIMIT 3

5.3.5 Update data

To update data, use the SET statement to update labels and attributes. The SET operation is idempotent.

Update/set new properties

MATCH (n:AGENCY {name:"北京市昌平区新龙城"})
SET n.address = "龙跃苑四区3号楼底商101号"
RETURN n

Remove attributes via remove

MATCH (n:AGENCY {name:"北京市昌平区新龙城"}) REMOVE n.address RETURN n

Add an address attribute

MATCH (n:AGENCY) WHERE n.address IS NULL SET n.address = "暂无地址" RETURN n

5.3.6 Delete data

Deleting data is completed through DELETE and DETACH DELETE. Among them, DELETE cannot delete related nodes. To delete a relationship, DETACH DELETE is required.

#删除节点
MATCH (n:AGENCY {name:"航头营业部"}) DELETE n

#有关系的节点是不能直接删除的
MATCH (n:AGENCY {name:"北京市昌平区新龙城"}) DELETE n

#删除节点和关系
MATCH (n:AGENCY {name:"北京市昌平区新龙城"}) DETACH DELETE n

#删除所有节点和关系,慎用!
MATCH (n) DETACH DELETE n

Relevant nodes cannot be deleted directly. As shown below, an error occurs when deleting the node "Xinlongcheng, Changping District, Beijing".

5.4 Index operations

Indexes exist in many data storage engines. Proper use of indexes can improve data retrieval efficiency. Indexes are also supported in Neo4j. Indexing fields can improve query speed.

5.4.1 Create index

The syntax is as follows, where: The OPTIONS clause specifies the index provider and configuration.

CREATE [TEXT] INDEX [index_name] [IF NOT EXISTS]
FOR (n:LabelName)
ON (n.propertyName)
[OPTIONS "{" option: value[, ...] "}"]

As follows, create an index for AGENCY;

CREATE TEXT INDEX agency_index_bid IF NOT EXISTS FOR (n:AGENCY) ON (n.bid)

5.4.2 Delete index

grammar

DROP INDEX index_name

For example, delete the index created above

6. Write at the end

This article uses a lot of space to comprehensively introduce the use of Neo4j, an excellent graph database. I hope it will be useful to students who read it and provide a reference for your future technology selection. Thank you for watching.

Guess you like

Origin blog.csdn.net/congge_study/article/details/132951781