我在python中使用neo4j库并在那里编写cypher查询。
这是我的一段代码:
from neo4j.v1 import GraphDatabase, basic_auth
from config import bolt_url,auth_id,auth_pass
driver = GraphDatabase.driver(bolt_url, auth=basic_auth(auth_id, auth_pass))
session = driver.session()
def get_some_data(limit=25) :
query = 'MATCH (n)-[r]-(m) \
RETURN n,r,m LIMIT ' + str(limit)
return session.run(query)
result_some_data = get_some_data(limit=4)
gen = result_some_data.records()
for record in gen :
n = record['n']
m = record['m']
r = record['r']
print()
print(n)
print(m)
print(r)
这就是我得到的:
<Node id=68757 labels={'ContentItem'} properties={'id': '1'}>
<Node id=72389 labels={'Person'} properties={'name': 'Tony'}>
<Relationship id=288942 start=72389 end=68757 type='Inside' properties={}>
<Node id=68757 labels={'ContentItem'} properties={'id': '1'}>
<Node id=72390 labels={'Person'} properties={'name': 'Bruce'}>
<Relationship id=288943 start=72390 end=68757 type='Inside' properties={}>
<Node id=68757 labels={'ContentItem'} properties={'id': '1'}>
<Node id=79758 labels={'Organization'}l properties={'name':'Oscorp'}>
<Relationship id=278985 start=79758 end=68757 type='Inside' properties={}>
<Node id=68758 labels={'ContentItem'} properties={'id': '2'}>
<Node id=79759 labels={'Organization'} properties={'name': 'STAR Labs'}>
<Relationship id=278986 start=79759 end=68758 type='Inside' properties={}>
PS:忽略名称属性,我已经更改了它们。
当我在neo4j浏览器上运行相同的cypher查询时,我得到了这个图:
在图片中,你可以看到我们在python中没有得到4个其他关系,即2个linkedWith
和2个RelatedTo
关系缺失。
现在,我知道neo4j正在匹配所请求的关系并仅返回它们,但是回到我的问题(最后!),是否有办法获得这些“其他”4关系?
答案 0 :(得分:1)
如果要获取特定数据库中节点之间的所有关系,可以使用以下查询。请注意,这仅适用于此版本的数据库。它是使用返回数据中的节点ID构建的。还要注意添加到查询中的方向,即只返回每对节点,重点关注一次关系而不是两次。
WITH [68757,72389,68757,72390,68757,79758,68758,79759] as nodes
MATCH (n)-[r]->(m)
WHERE id(n) in nodes
AND id(m) in modes
RETURN n,r,m LIMIT