【发布时间】:2017-11-21 12:09:57
【问题描述】:
我在 python 中使用 neo4j 库并在其中编写密码查询。
这是我的一段代码:
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 浏览器上运行相同的密码查询时,我得到了这个图表:
在图片中,您可以看到还有 4 个其他关系是我们在 python 中没有得到的,即缺少 2 个linkedWith 和 2 个RelatedTo 关系。
现在,我知道 neo4j 正在匹配请求的关系并仅返回它们,但我的问题是(终于!),有没有办法同时获得这 4 个“其他”关系?
【问题讨论】:
-
您将返回限制为四个(即
(limit=4)) - 不是吗?浏览器将通过左侧导航的Browser Settings中的Connect result nodes属性完成额外的关系。 -
现在我知道为什么浏览器会显示其他连接了。谢谢你。有没有办法从 python 发送这个设置并接收其他关系?
-
不,浏览器基本上有额外的开销。它转身并获取查询结果集中返回的节点之间的所有其他关系。如果您希望您的查询返回您需要询问的内容。您的查询是完全开放式的,只是在数据库中查找所有无序的连接节点对。您将获得相同的节点。