20210706学习使用Neo4j

Jul 6, 2021

学习使用Neo4j

Neo4j 本身提供7474端口的及时UI操作服务。如: http://localhost:7474 登录上即可。

这里以nodejs 链接代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const neo4j = require("neo4j-driver")
let session = null
function connect(conf:{url:string, username:string, password:string}) {
const driver = neo4j.v1.driver(conf.url, neo4j.v1.auth.basic(conf.username, conf.password))
session = driver.session(neo4j.v1.session.WRITE)
}
function executeCypher(data:{cql:string}) {
session.run(data.cql, data).subscribe({
onNext:(record)=> {
console.log("[neo4j.onNext] record:", record)
},
onCompleted: ()=> {
console.log("[neo4j.onCompleted] ...")
},
onError: (err)=> {
console.log("[neo4j.onError] err: ", err)
}
})
}

cql语法

创建

$CREATE (m:Movie {title: "The Matrix", released: 1990})
$CREATE (m:Movie {title: "Apollo 13", released: 2000})

搜索

$match (m:Movie {title: "The Matrix"}) return m # match (m:Movie) where m.title = "The Matrix"

创建边

// m0-r->m1
$match (m0:Movie {title: "The Matrix"}) match (m1:Movie {title: "Apollo 13"}) \
create (m0)-[r:ACTED_IN]->(m1) return m0, r, m1 # 每次只能创建一个方向
// 在创建一个反向m0<-r-m1
$match (m0:Movie {title: "The Matrix"}) match (m1:Movie {title: "Apollo 13"}) \
create (m0)<-[r:ACTED_IN]-(m1) return m0, r, m1

删除边、node

$match (m0:Movie {title: "The Matrix"}) match (m1:Movie {title: "Apollo 13"}) \
optional match(m0)<-[r0:ACTED_IN]-(m1) \
optional match(m0)-[r1:ACTED_IN]->(m1) \
DELETE r0, r1, m0 # 必须先删除所有边才可以删除node

STARTS WITH / ENDS WITH / CONTAINS

$where a.name STARTS WITH 'Ake'
$where a.name ENDS WITH 'ker'
$where a.name CONTAINS 'ke'

EXISTS 属性检查

$match (m:Movie) where EXISTS(m.title)

NOT 取反

$where NOT m.title ends with "ker"

=~’regexp’ 语法继承来自Java正则表达式

$match (m:Movie) where m.title =~'The Matr*'

(?i) 非大小写敏感

$where n.name =~(?i)TOM.*' return n

模式过滤

$match (m0:Movie {title: "The Matrix"}), (m1:Movie) \
where (m0)-[r:ACTED_IN]-(m1) return m0, m1
相当于:
$match (m0:Movie {title: "The Matrix})-[r:ACTED_IN]-(m1:Movie) return m0, m1

关系类型过滤

$match (n)-[r]-() where type(r)=~'DIRE.*' return n

IN运算符

$match (m:Movie) where m.title IN ["The Matrix", "Apollo 13", "the matrix"]

OR/AND/IS NULL/ORDER BY

$match (m:Movie) where m.title = "The Matrix" OR m.title IS NULL return m ORDER BY m.title

DISTINCT 返回结果去掉重名

$match (m:Movie) return DISTINCT m.title

LIMIT / SKIP

FOREACH

$match p=(m0:Movie {title: "The Matrix"})-[r:ACTED_IN]-(m1) FOREACH (n IN nodes(p) | set n.marked = true)

SIZE

$match p=(m0:Movie)-[r:ACTED_IN]-(m1:Movie) where m0.title="The Matrix" return m0, SIZE(nodes(p))

COLLECT 将资源转为array

WITH / UNWIND

$match (m:Movie) WITH COLLECT(m) as c UNWIND c as x return x

MERGE / CREATE 区别在于已存在

$create (m:Movie {Id: 10}) create (m:Movie {Id: 10}) # 此时有两个Id=10 的movie
$MERGE (m:Movie {Id: 20}) MERGE (m:Movie {Id: 20}) # 此时只会有一个Id=20 的movie

UNION 合并

// foreach 设置属性
$match (cc:CreditCard) with collect(cc) as arr foreach(c in arr | set c.type = "cc")
// 合并CreditCard 和DebitCard 数据
$match (cc:CreditCard) return cc.Id as Id, cc.name as name, cc.number as number order by cc.Id \
union \
match (dc:DebitCard) return dc.Id as Id, dc.name as name, dc.number as number order by dc.Id

ON MERGE / CREATE 更新或者新建

$MERGE (p:Person {name: "Peter"}) \
ON MATCH SET peter += { hungry: true, position: "GAO TOU"}
ON CREATE SET peter += {last: "aker"}

参考1
merge参考
union参考
on merge/create参考