【问题标题】:python neo4j not getting other relationshipspython neo4j没有得到其他关系
【发布时间】: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 发送这个设置并接收其他关系?
  • 不,浏览器基本上有额外的开销。它转身并获取查询结果集中返回的节点之间的所有其他关系。如果您希望您的查询返回您需要询问的内容。您的查询是完全开放式的,只是在数据库中查找所有无序的连接节点对。您将获得相同的节点。

标签: python neo4j cypher


【解决方案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

【讨论】:

  • 在我这个版本的数据库中,有上万个节点,我只是限制搜索到4个关系
  • 是的...我看到了:)
  • 所以我正在从 python 运行 2 个密码查询,第一个与问题相同,以获取节点,第二个从您的答案中获取,其中数组是从第一个查询的结果中获取的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
相关资源
最近更新 更多