知识图谱(四):Neo4j查询语法

一、基础语法

1、节点基础操作

  • 节点创建

    Cypher命令: 

节点模式的构成:(Variable:Lable1:Lable2{Key1:Value1,Key2,Value2}),实际上,每个节点都有一个整数ID,在创建新的节点时,Neo4j自动为节点设置ID值,在整个数据库中,节点的ID值是递增的和唯一的。

下面的Cypher查询创建一个节点,标签是Industry,具有两个属性name和born,通过RETURN子句,返回新建的节点:

create (n:Industry { industry_name: '珠海格力集团有限公司', born: 1991}) return n;

   python创建节点:

from py2neo import Graph, NodeMatcher, Node, Relationship, RelationshipMatcher
graph = Graph("http://xx.xx.xx.xx:7474", username="neo4j", password="neo4j")
a = Node('Industry', industry_name='珠海格力集团有限公司',born='1991')
graph.create(a)
  • 节点查询

  模式:

节点具有标签和属性,Cypher为了引用节点,需要给节点命名:

(n) :该模式用于描述节点,节点的变量名是n;匿名节点是();
(n:lable):该模式用于描述节点,节点具有特定的标签lable;也可以指定多个标签;
(n{name:"Vic"}):该模式用于描述节点,节点具有name属性,并且name属性值是“Vic”;也可以指定多个属性;
(n:lablle{name:"Vic"}):该模式用于描述节点,节点具有特定的标签和name属性,并且name属性值是“Vic”;

  Cypher命令:    

 Match (n:Industry {industry_name:"珠海格力集团有限公司"}) return n

   python查询节点:

from py2neo import Graph, NodeMatcher, Node, Relationship, RelationshipMatcher
graph = Graph("http://xx.xx.xx.xx:7474", username="neo4j", password="neo4j")
#匹配节点,标签:Industry,属性name: 珠海格力集团有限公司
matcher_n = NodeMatcher(graph)
matcher_n.match("Industry", industry_name="珠海格力集团有限公司").first()

# 匹配后删除
n_list=matcher_n.match("Industry",industry_name="珠海格力集团有限公司")
for r in n_list:
	graph.delete(r)

2、关系基础操作

在属性图中,节点之间存在关系,关系通过[]表示,节点之间的关系通过箭头()-[]->()表示,例如:

[r]:该模式用于描述关系,关系的变量名是r;匿名关系是[]
[r:type]:该模式用于描述关系,关系类型是type;每一个关系必须有且仅有一个类型;
[r:type{name:"Friend"}]:该模式用于描述关系,关系的类型是type,关系具有属性name,并且name属性值是“Friend”;
  • 边创建

    Cypher操作

create (n:Industry { industry_name: '珠海格力集团有限公司', born: 1991})

create (p:Persons { name: '董明珠@1954-01-01'})

CREATE
  (p)-[:Executivies {position:'1,3,23'}]->(n)

    Python操作

# 创建
a = Node('Industry', name='珠海格力集团有限公司')
b = Node('Persons', name='董明珠@1954-01-01')
r = Relationship(a, 'Executives', b)
print(a, b, r)
graph.create(r)
  •  边查询

   Python操作

from py2neo import Graph, NodeMatcher, Node, Relationship, RelationshipMatcher
graph = Graph("http://xx.xx.xx.xx:7474", username="neo4j", password="neo4j")
# 关系查找,删除
matcher_r = RelationshipMatcher(graph)
r_list=matcher_r.match(r_type='Executives')
for r in r_list:
	graph.delete(r)

  Cypher操作

  语法上:“-”与“-->”的使用,需要注意

节点之间通过关系联系在一下,由于关系具有方向性,因此,-->表示存在有向的关系,--表示存在关联,不指定关系的方向,例如:

(a)-[r]->(b) :该模式用于描述节点a和b之间存在有向的关系r,可指定关系类型
(a)-->(b):该模式用于描述a和b之间存在有向关系,是步长为1的路径,节点a和b之间有关系直接关联;
(a)-->()-->(b):是步长为2的路径,从节点a,经过两个关系和一个节点,到达节点b;

PS: 与 “珠海格力集团有限公司”有关系的公司(无方向性,步长=1)

Match (n:Industry {name:"珠海格力集团有限公司"})--(m:Industry) return n,m

 PS: 与 “珠海格力集团有限公司”有关系的人(无方向性,步长=2),中间是通过“珠海格力电器股份有限公司”关联的

MATCH (n:Industry { name: "珠海格力集团有限公司" })--(otherPerson)--(p:Persons) return n,otherPerson,p

PS:与“珠海格力电器股份有限公司”有关系“Director”的节点(无向,步为1)

MATCH p=(:Industry{name:"珠海格力电器股份有限公司"})-[r:Director]-() RETURN p

二、多层关系查询

从一个节点,通过直接关系,连接到另外一个节点,这个过程叫遍历,经过的节点和关系的组合叫做路径(Path),路径是由节点和关系的有序组合。

(a)-->(b):是步长为1的路径,节点a和b之间有关系直接关联;
(a)-->()-->(b):是步长为2的路径,从节点a,经过两个关系和一个节点,到达节点b;



Cypher语言支持变长路径的模式,变长路径的表示方式是:[*N..M],N和M表示路径长度的最小值和最大值。

(a)-[*2]->(b):表示路径长度为2,起始节点是a,终止节点是b;
(a)-[*3..5]->(b):表示路径长度的最小值是3,最大值是5,起始节点是a,终止节点是b;
(a)-[*..5]->(b):表示路径长度的最大值是5,起始节点是a,终止节点是b;
(a)-[*3..]->(b):表示路径长度的最小值是3,起始节点是a,终止节点是b;
(a)-[*]->(b):表示不限制路径长度,起始节点是a,终止节点是b;

1、直接拼接关系节点查询

MATCH (na:Industry{name:'格力电器股份有限公司'})-[re1]->(nb:Industry)-[re2]->(nc:Industry) return na,re1,nb,re2,nc

2、使用with关键字

MATCH (na:Industry)-->(re1)-->(nb:Industry)
WITH na,otherPerson1,nb where nb.name='格力电器股份有限公司'
MATCH (nb:Industry)-->(re2)-->(nc:Industry)
RETURN na,re1,nb,re2,nc  limit 50

3、使用深度运算符

当实现多深度关系节点查询时,显然使用以上方式比较繁琐。可变数量的关系->节点可以使用-[:TYPE*minHops..maxHops]->。

如果在1到3的关系中存在路径,将返回开始点和结束点。

match data=(na:Industry{name:'格力电器股份有限公司'})-[*1..3]->(nb:Industry) return data

附录:参考文献

Neo4j 第三篇:Cypher查询入门: https://www.cnblogs.com/ljhdo/p/5516793.html

Neo4j之Cypher学习总结:  https://www.jianshu.com/p/2bb98c81d8ee

查找两个节点之间关系的最短路径: https://blog.csdn.net/Appleyk/article/details/80437746

关于neo4j查询多深度关系节点:  https://blog.csdn.net/u013946356/article/details/81739079

猜你喜欢

转载自blog.csdn.net/ai_1046067944/article/details/85342567