neo4j 基础语句

Posted sheepcore

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了neo4j 基础语句相关的知识,希望对你有一定的参考价值。

前言:

  这篇本章通过 neo4j 官方给出的 movie graph 例子学习 neo4j 基本的创建、查询、修改、删除语句。

一、创建图谱

1. 获取 Example Graph - Movie Graph 实例

技术图片

 2. 导入官网的图谱创建语句

技术图片

 下面展示了电影 "The Matrix" 的人物信息与关系:

 1 // create the profiles of actors
 2 CREATE (TheMatrix:Movie {title:The Matrix, released:1999, tagline:Welcome to the Real World})
 3 CREATE (Keanu:Person {name:Keanu Reeves, born:1964})
 4 CREATE (Carrie:Person {name:Carrie-Anne Moss, born:1967})
 5 CREATE (Laurence:Person {name:Laurence Fishburne, born:1961})
 6 CREATE (Hugo:Person {name:Hugo Weaving, born:1960})
 7 CREATE (LillyW:Person {name:Lilly Wachowski, born:1967})
 8 CREATE (LanaW:Person {name:Lana Wachowski, born:1965})
 9 CREATE (JoelS:Person {name:Joel Silver, born:1952})
10 
11 //create the relationships between the roles and actors
12 CREATE
13 (Keanu)-[:ACTED_IN {roles:[‘Neo‘]}]->(TheMatrix),
14 (Carrie)-[:ACTED_IN {roles:[‘Trinity‘]}]->(TheMatrix),
15 (Laurence)-[:ACTED_IN {roles:[‘Morpheus‘]}]->(TheMatrix),
16 (Hugo)-[:ACTED_IN {roles:[‘Agent Smith‘]}]->(TheMatrix),
17 (LillyW)-[:DIRECTED]->(TheMatrix),
18 (LanaW)-[:DIRECTED]->(TheMatrix),
19 (JoelS)-[:PRODUCED]->(TheMatrix)
20 
21 CREATE (Emil:Person {name:"Emil Eifrem", born:1978})
22 CREATE (Emil)-[:ACTED_IN {roles:["Emil"]}]->(TheMatrix)

 导入成功!

技术图片

 汤姆·汉克斯参演的电影

 二、查询语句

1. 按名字查找某个实体 

eg1: Find the actor named "Tom Hanks"...

 neo4j $ MATCH (tom {name: "Tom Hanks"}) RETURN tom 

技术图片

 eg2: Find the movie with title "Cloud Atlas"...

 neo4j $ MATCH (cloudAtlas {title: "Cloud Atlas"}) RETURN cloudAtlas 

技术图片

2. 使用 limit 关键字限制查询实体个数

eg: Find 10 people...

 neo4j $ MATCH (people:Person) RETURN people.name LIMIT 10 

技术图片

3. 使用 where 条件查询语句

eg: Find movies released in the 1990s...

neo4j $ MATCH (nineties:Movie) 
     WHERE nineties.released >= 1990 AND nineties.released < 2000
     RETURN nineties.title

技术图片

4. 已知一节点和关系查找另一节点信息

eg1: List all Tom Hanks movies...

neo4j $ MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies) 
     RETURN tom,tomHanksMovies

技术图片

 eg2: Who directed "Cloud Atlas"?

 neo4j $ MATCH (directors)-[:DIRECTED]->(cloudAtlas {title: "Cloud Atlas"}) 
      RETURN directors.name

 or

 neo4j $ MATCH (cloudAtlas {title: "Cloud Atlas"})<-[:DIRECTED]-(directors) 
      RETURN directors.name

技术图片

5. 查询某个节点上以同样关系连接的节点信息

eg: Tom Hanks‘ co-actors...

neo4j $ MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors) 
     RETURN coActors.name

技术图片

 6. 查询与某结点有关系的所有结点信息

eg: How people are related to "Cloud Atlas"...

 neo4j $ MATCH (people:Person)-[relatedTo]-(:Movie {title: "Cloud Atlas"}) 
    RETURN people.name, Type(relatedTo), relatedTo

技术图片

7. 查找两个节点的最短路径(不论任何关系)

eg: Bacon path, the shortest path of any relationships to Meg Ryan

 neo4j $ MATCH p=shortestPath( (bacon:Person {name:"Kevin Bacon"})-[*]-(meg:Person {name:"Meg Ryan"}))
     RETURN p

技术图片

 8. 查询两个结点的中介结点信息

eg: Find someone to introduce Tom Hanks to Tom Cruise

neo4j $ MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors),
     (coActors)-[:ACTED_IN]->(m2)<-[:ACTED_IN]-(cruise:Person {name:"Tom Cruise"})
     RETURN tom, m, coActors, m2, cruise

9. 删除所有结点及关系

 neo4j $ MATCH (n) DETACH DELETE n or

 neo4j $ MATCH (n)-[m] DELETE n, m 

 

三、总结

1. 查询一般格式:(keyword  、variable 、  entitle or relationship)

 MATCH (sub:subject)-[pre:predicate]->(obj:object) RETURN sub, pre, obj 

2. 如果是单向关系需要注意箭头方向

 

以上是关于neo4j 基础语句的主要内容,如果未能解决你的问题,请参考以下文章

JSP 基础语法

JSP开发中的基础语法

CYPHER 语句(Neo4j)

使用 neo4j jdbc 驱动程序和 neo4j 4 准备好的语句中的密码查询参数的语法是啥?

学习记录-neo4j语句

动态SQL基础概念复习(Javaweb作业5)