首页 > 知识图谱 > 正文

3.Cypher

标签:neo4j, kg

1.Pattern

Neo4j中的图由节点(Node)和关系(Relation)组成,节点和关系也可以具有属性。节点代表的是实体,比如概念、事件、地点和事物。关系将节点连接起来。

节点和关系是图的基本单元,但是真正让图表示复杂知识的是连接起来的节点和关系构成的patterns。单一的节点和关系仅仅只能携带少量的信息但是pattern却可以携带或表示复杂的信息。

Cypher是Neo4j的查询语言就是基于pattern。通常pattern用来匹配期望找到的图结构,一旦找到或者创建,就可以进行进一步处理。

Cypher中简单的pattern比如:a Person LIVES_IN a City or a City is PART_OF a Country

或者更为复杂一点的:(:Person) -[:LIVES_IN]-> (:City) -[:PART_OF]-> (:Country)

Cypher的长处在于擅长识别各种给定的模式(pattern)。就跟人脑一样,比如人脑就很擅长识别图片中的人、物体等,因为人、物体都具备一定的模式。

Cypher跟SQL语言类似,有比如MATCH,WHERE,DELETE之类的关键字。

2.Node语法

Node使用一对括号表示:

()
((matrix))
(:Movie)
(matrix:Movie)
(matrix:Movie {title: "The Matrix"})
(matrix:Movie {title: "The Matrix", released: 1997})

Movie是Node的标签,表明节点的类型。不同的标签可以组成不同的模式,比如Movie和Actor就可以组成一种模式。Neo4j的节点使用标签索引的,每一个索引由标签和属性组成。 节点的属性由大括号包含一系列key/value。比如 {title: "The Matrix", released: 1997}

Relation语法

Cypher中--表示无方向性的关系,<--,-->表示指向性的关系,[....]表示对关系的说明,可以是变量、属性等。

-->
-[role]->
-[:ACTED_IN]->
-[role:ACTED_IN]->
-[role:ACTED_IN {roles: ["Neo"]}]->

ACTED_IN是关系的标签,指明关系的类型。role是关系变量,可以在别处使用。roles是属性值,和节点的属性一样。

Pattern语法

(keanu:Person:Actor {name: "Keanu Reeves"} )-[role:ACTED_IN {roles: ["Neo"] } ]->(matrix:Movie {title: "The Matrix"} )

Pattern指明在图中符合给定pattern的节点以及连接的关系。 Pattern还可以被声明为一个变量:

acted_in = (:Person)-[:ACTED_IN]->(:Movie)

Cypher示例

打开命令行执行:

sudo bin/neo4j console

打开浏览器打开:

http://localhost:7474/browser/

添加:

CREATE (:Movie { title:"The Matrix",released:1997 })

执行完毕可以看到:



如果创建之后需要返回值:

CREATE (p:Person { name:"Keanu Reeves", born:1964 })
RETURN p

执行完毕可以看到:

更为复杂的一个例子:

CREATE (a:Person { name:"Tom Hanks",
  born:1956 })-[r:ACTED_IN { roles: ["Forrest"]}]->(m:Movie { title:"Forrest Gump",released:1994 })
CREATE (d:Person { name:"Robert Zemeckis", born:1951 })-[:DIRECTED]->(m)
RETURN a,d,r,m



查找

比方说查找所有的Movie:

MATCH (m:Movie)
RETURN m



也可以找某个具体的人:

MATCH (p:Person { name:"Keanu Reeves" })
RETURN p



还可以查找某个关系:

MATCH (p:Person { name:"Tom Hanks" })-[r:ACTED_IN]->(m:Movie)
RETURN m, r



创建新的pattern

MATCH (p:Person { name:"Tom Hanks" })
CREATE (m:Movie { title:"Cloud Atlas",released:2012 })
CREATE (p)-[r:ACTED_IN { roles: ['Zachry']}]->(m)
RETURN p,r,m



MERGE

Merge表示为节点或者关系添加新的pattern或属性。

添加新的属性:

MERGE (m:Movie { title:"Cloud Atlas" })
ON CREATE SET m.released = 2012
RETURN m



添加新的关系:

MATCH (m:Movie { title:"Cloud Atlas" })
MATCH (p:Person { name:"Tom Hanks" })
MERGE (p)-[r:ACTED_IN]->(m)
ON CREATE SET r.roles =['Zachry']
RETURN p,r,m



添加新的节点和关系:

CREATE (y:Year { year:2014 })
MERGE (y)<-[:IN_YEAR]-(m10:Month { month:10 })
MERGE (y)<-[:IN_YEAR]-(m11:Month { month:11 })
RETURN y,m10,m11



条件选择

#1 WHERE
MATCH (m:Movie)
WHERE m.title = "The Matrix"
RETURN m

#2 WHERE
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name =~ "K.+" OR m.released > 2000 OR "Neo" IN r.roles
RETURN p,r,m

#3 NOT
MATCH (p:Person)-[:ACTED_IN]->(m)
WHERE NOT (p)-[:DIRECTED]->()
RETURN p,m

#4 ALIAS
MATCH (p:Person)
RETURN p, p.name AS name, toUpper(p.name), coalesce(p.nickname,"n/a") AS nickname, { name: p.name,
  label:head(labels(p))} AS person
  
#5 COUNT
MATCH (:Person)
RETURN count(*) AS people
OR
RETURN  count(DISTINCT role)

MATCH (actor:Person)-[:ACTED_IN]->(movie:Movie)<-[:DIRECTED]-(director:Person)
RETURN actor,director,count(*) AS collaborations

#6 ORDER
MATCH (a:Person)-[:ACTED_IN]->(m:Movie)
RETURN a,count(*) AS appearances
ORDER BY appearances DESC LIMIT 10;

#7 COLLECT
MATCH (m:Movie)<-[:ACTED_IN]-(a:Person)
RETURN m.title AS movie, collect(a.name) AS cast, count(*) AS actors

#8 UNION
MATCH (actor:Person)-[r:ACTED_IN]->(movie:Movie)
RETURN actor.name AS name, type(r) AS acted_in, movie.title AS title
UNION
MATCH (director:Person)-[r:DIRECTED]->(movie:Movie)
RETURN director.name AS name, type(r) AS acted_in, movie.title AS title

#9 WITH
MATCH (person:Person)-[:ACTED_IN]->(m:Movie)
WITH person, count(*) AS appearances, collect(m.title) AS movies
WHERE appearances > 1
RETURN person.name, appearances, movies

查询的结果可能是数字、字符串、或者列表、数组。

约束和索引

# UNIQUE
CREATE CONSTRAINT ON (movie:Movie) ASSERT movie.title IS UNIQUE

# INDEX
CREATE INDEX ON :Actor(name)
CREATE (actor:Actor { name:"Tom Hanks" }),(movie:Movie { title:'Sleepless IN Seattle' }),
  (actor)-[:ACTED_IN]->(movie);
  
  

原创文章,转载请注明出处!
本文链接:http://youran.tech/posts/neo4j-3-cypher.html
上篇: 2.Neo4j相关概念
下篇: 1.Introduction

留言: