Figure database and cypher statement

Time:2022-5-23

Figure database and cypher statement

1. Understanding of graph database

Recently, I accidentally used graph database in my work, and my understanding of the concept of graph database is still vague. At the beginning, the nodes and edges I understood can be understood as classes in Java in disguise. The label is the name of the class. For example, match (n: person) return n limit 10, person is the class, and N is the variable name after class instantiation. However, the label is not completely equivalent to the class. In Java, there can only be one class with the same name, and there can be multiple persons. This explanation is contradictory. After a period of time, my understanding has changed. Person is the value of the tag. The value of the tag can be person or animal. Combined with the query statement, the tag can be understood as “condition”. Match (n: person) return n limit 10 is described in words. Find out all the data whose tag value is person, intercept 10 items, and assign the data to variable n. Variable n can be compared to a Java object, which has attributes, corresponding to the attributes of points and edges.

2. Java connection diagram database

Java connection to the database can refer toTwo ways to connect neo4j with Java – Xingchao – blog Park (cnblogs. Com)

3. Cypher statement basic usage

————————————————
Copyright notice: This is the original article of CSDN blogger “simply follow the wind”, which follows the CC 4.0 by-sa copyright agreement. Please attach the original source link and this notice for reprint.
Original link:https://blog.csdn.net/lt326030434/article/details/83542676

1. Query
Simple query
Query 10 person nodes

match (n:Person) return n limit 10

Query whether there is has in person node and phone node_ Phone relationship

match  (n:Person)-[:HAS_PHONE]->(p:Phone) return n,p

Query 10 relationships: has in person node and phone node_ Phone relationship, and name of person node = ‘name 6’

Match (n: person) - [: has_phone] - > (P: phone) where n.name = "name 6" return n, P limit 20

Multi dimension query
Query the following relationship: person node n and phone node P1 own has_ Phone relationship, and P1 node has a call relationship with another phone node P2

match  (n:Person)-[:HAS_PHONE]->(p1:Phone)-[:CALL]->(p2:Phone)

Relational query
Query the call relationship between any node and any node

match p=()-[:CALL]->()

Regular query
Query all nodes of users, and the name conforms to the regular ‘Jack. *’ Node of

match (n:Users) where n.name=~'Jack.*' return n

Include query
Query all nodes of users, and the node whose name contains’ J ‘

match (n:Users) where n.name contains 'J' return n

2. Create
Simple creation
Create a person love – > dog relationship

create (n:Person)-[:LOVES]->(m:Dog)

Attribute creation
Create the relationship of person fear – > tiger, and the person Name = ‘Li Si‘, fear Level=1,Tiger. Type = “Amur tiger”

Create (n: person {Name: "Li Si"}) - [: fear (level: 1)] - > (M: Tiger {type: "northeast tiger"})

Create a relationship between two entities that have no relationship
Create a relationship K so that the person with name = “Wang Wu” has a know relationship with the person with name: “Zhao Liu”

Match (n: person {Name: "Wang Wu"}), (M: person {Name: "Zhao Liu"})
create (n)-[k:KNOW]->(m) return k

Return if there is one, and create if there is none: Merge
If the created relationship already exists, it will be returned. If the created relationship does not exist, it will be created

Match (n: person {Name: "Wang Wu"}), (M: person {Name: "Zhao Liu"})
merge (n)-[k:KNOW]->(m) return k

3. Delete
Delete relationship
Delete the fear relationship between the person node of name = “Li Si” and the tiger node

Match (n: person {Name: "Li Si"}) - [F: fear] - > (M: Tiger)
delete f

Delete entity
Delete the person node of name = “Li Si”

Match (n: person {Name: "Li Si"})
delete n

Delete duplicate nodes
If there are multiple people nodes with name = “Zhang San”, delete only one

Match (P: people {Name: "Zhang San"}) with P skip 1 delete p

4. Modification
modify attribute
Insert the age tag into the tiger node of type = “northeast tiger”, and the attribute of age is 10

Match (T: Tiger) where T.TYPE = "northeast tiger" set t.age = 10 return t

Modify relationship name
Has_ Change phone to has_ PHONE

match (a)-[h:HAS_Phone]->(b) 
create (a)-[h2:HAS_PHONE]->(b) 
set h2=h 
with h 
delete h

5. Create a unique index
Create a unique index number for the phone node

CREATE CONSTRAINT ON (p:Phone) ASSERT p.number IS UNIQUE

Recommended Today

CSS architecture and SCSS simple introduction

GitHub project address 1、 Importance of Architecture Not paying attention to early programming Late stage bald 2、 Why CSS architecture The expanding CSS code has nothing to do The project is in the maintenance state without CSS architecture for a long time Result of CSS code is extremely confusingCSS is difficult to reuse, expand and […]