jdk安装、配置
从官网下载https://neo4j.com/download-center/
解压、设置环境变量
NEO4J_HOME 本机地址
path %NEO4J_HOME%\bin
命令
启动 neo4j.bat console
如果出现错误"powershell命令提示符出现“不是内部或外部命令,也不是可运行的程序或批处理文件",
解决方法:在环境变量path后添加powershell的路径——C:\Program Files\TortoiseSVN\bin
web访问
http://localhost:7474/browser/
初始用户: neo4j 初始密码: neo4j
也可命令行访问 (暂时忽略)
// 格式
create (node_name:lable_name
{
key: value
});
// node_name 类型于关系型数据库的表的别名(建立节点之后,这个东西就不知道什么用了)
// label_name 类似于表明一样,表明一类实体
// 例子
create (dog:DOG
{
name:“dog_name”,
age:20
});
// 查询节点的某个属性
match(node_name:node_label)
where node_name.key1=value1
return node.key3 as key3
// 查询整个节点
match(node_name:node_label)
where node_name.key1=value1 and/or node_name.key2>value2
return node_name
// 例如
match (dog:Dog)
where dog.name = ‘dog_name’
return dog
relationship
// 给现有节点添加关系
match (a:A),(b,B)
where a.key1=value1 and b.key2=value2 or …
create (a)-[r:R{key3:value3,key4:value4,…}]->(b)
// 新建节点的同时创建关系,甚至可以在后面追加return
create (a:A{…})-[r:R{…}]->(b:B{…})
return r
// 查询关系
match (a:A)-[r:R]->(b:B)
where a.key1=value1 or r.key2=value2 and b.key3=value3
return r
delete 删除节点或关系,在删除节点前,必须先删除其相关关系
// 删除节点
match (a:A)
where a.key1=value1
delete a
// 删除节点的属性
match (a:A)
where a.key1=value1
delete a.key1
match (a:A)
delete a
// 删除所有A\B之间的R关系
match (a:A)-[r:R]->(b:B)
delete r
// 同时删除关系和节点
match (a:A)-[r:R]->(b:B) where a.key1=value1 delete a,b,r
数据初始化创建语句:
CREATE (philip:Person {name:“Philip”})-[:IS_FRIEND_OF]->(emil:Person {name:“Emil”}), (philip)-[:IS_FRIEND_OF]->(michael:Person {name:“Michael”}), (philip)-[:IS_FRIEND_OF]->(andreas:Person {name:“Andreas”}) CREATE (sushi:Cuisine {name:“Sushi”}), (nyc:City {name:“New York”}), (iSushi:Restaurant {name:“iSushi”})-[:SERVES]->(sushi),(iSushi)-[:LOCATED_IN]->(nyc), (michael)-[:LIKES]->(iSushi), (andreas)-[:LIKES]->(iSushi), (zam:Restaurant {name:“Zushi Zam”})-[:SERVES]->(sushi),(zam)-[:LOCATED_IN]->(nyc), (andreas)-[:LIKES]->(zam)
问题:
(1)New York都有哪些餐厅?
(2)Philip朋友的朋友是谁?
(3)给Philip推荐他朋友爱吃的位于纽约的餐厅,并按照喜欢人数进行排序?
问题答案参考:https://neo4j.com/graphgist/restaurant-recommendations
CREATE (philip:Person {name:“Philip”})-[:IS_FRIEND_OF]->(emil:Person {name:“Emil”}), (philip)-[:IS_FRIEND_OF]->(michael:Person {name:“Michael”}), (philip)-[:IS_FRIEND_OF]->(andreas:Person {name:“Andreas”}) CREATE (sushi:Cuisine {name:“Sushi”}), (nyc:City {name:“New York”}), (iSushi:Restaurant {name:“iSushi”})-[:SERVES]->(sushi),(iSushi)-[:LOCATED_IN]->(nyc), (michael)-[:LIKES]->(iSushi), (andreas)-[:LIKES]->(iSushi), (zam:Restaurant {name:“Zushi Zam”})-[:SERVES]->(sushi),(zam)-[:LOCATED_IN]->(nyc), (andreas)-[:LIKES]->(zam)
上面一大推拆分成下面的一步步
label: Person
label: Cuisine
5. 创建sushi
create (sushi:Cuisine{name:“Sushi”});
label: City
6. 创建nyc
create (nyc:City {name:“New York”});
label: Restaurant
7. 创建iSushi
create (iSushi:Restaurant{name:“iSushi”})
8. 创建zam
create (zam:Restaurant {name:“Zushi Zam”})
创建关系 // 若关系没有标签、where等限制会给所有的节点加上关系
match(a:Person),(b:Person)
where a.name=“Philip” and b.name=“Michael”
create (a)-[:IS_FRIEND_OF]->(b)
与 emil建立关系 同上
与 Michael建立关系 同上
与 andreas建立关系 同上
– 简略记法,实际这么写可不成
create (iSushi)-[:SERVES]->(sushi)
create (zam)-[:SERVES]->(sushi)
– 之后通过id(n)方法选择元素
iSushi 160 nyc 140 michael 240 andreas 260 zam 180
create (iSushi)-[:LOCATED_IN]->(nyc)
create (zam)-[:LOCATED_IN]->(nyc)
create (michael)-[:LIKES]->(iSushi)
create (andreas)-[:LIKES]->(iSushi)
create (andreas)-[:LIKES]->(zam)
match (philip:Person),(emil:Person)
where philip.name=“Philip” and emil.name=“Emil” – 若无选择项是否能创建关系,能,但是philip与emil此时代表标签。会有很多关系建立
create (philip)-[:IS_FRIEND_OF]->(emil)
(1)match (a:Restaurant)-[:LOCATED_IN]->(b:City) where b.name=“New York” return a
(2)match (a:Person)-[:IS_FRIEND_OF]->(b:Person) where a.name=“Philip” return b
(3)match (a:Restaurant)-[:LOCATED_IN]->(b:City),(c:Person)-[:IS_FRIEND_OF]->(d:Person),(d:Person)-[:LIKES]->(a:Restaurant) where b.name=“New York” and c.name=“Philip” return a
参考答案更整洁
通过id()方法 id(n)=10