本篇文章主要内容是要将在Neo4j图形界面中操作的Cypher Query Language对应到python代码中去。
了解py2neo的简单操作请参考文章:使用py2neo模块,python操作Neo4j 图数据库
安装py2neo版本4,py2neo使用说明参考:https://py2neo.org/v4/
Neo4j Cypher Query Language请参考文章:《一》、《二》、《三》
主要内容包括:
****注:graph=Graph(host='localhost', author=('你的数据库名','连接数据库的密码') eg:graph=Graph(host='localhost', author=('neo4j','neo4j'))
1.Neo4j CQL - CREATE命令 <=====> graph.create(node)#创建节点
2.Neo4j CQL - MATCH命令 <=====> graph.run(MATCH命令)#查找
3.py2neo --- Node Matching
4.py2neo --- Relationship Matching

1.Neo4j CQL - CREATE命令 <=====> graph.create(node)#创建节点



2.Neo4j CQL - MATCH命令 <=====> graph.nodes.match(...).first()
查找节点





3.py2neo --- Node Matching:
通过标签和属性简单查找:NodeMatcher(graph).match()

通过NodeMatch.where()进行复杂查找:查找名字以“姚”开头的所有节点



4.py2neo --- Relationship Matching:
RelationshipMatcher(graph).match() 查找所有关系

RelationshipMatcher(graph).match(r_type='关系名') 通过r_type确定关系名
语法:match(nodes=None, r_type=None, **properties)

RelationshipMatcher(graph).match().order_by().limit() 匹配、排序、限定返回次数
语法:match.order_by("_.weight", "max(_.a, _.b)")

添加:2018-12-26问题:如何利用头尾节点名和关系名来查找neo4j中是否存在节点所对应的关系???
以下图为例:创建一些BaiduBaike类型的节点,并创建一些关系。
创建语句:CREATE (a:BaiduBaike {name:'上海'})-[r:火车站] ->(b:BaiduBaike {name:'上海站'})

根据以上语句稍微改动一下,创建出如下图节点关系

使用如下代码,即可以通过头实体名 或 尾实体名 查询关系。

网友评论