如何使用 py2neo 计算与节点的关系(一种类型)
How to count relationships (of one type) to a node using py2neo
使用py2neo 4.x, neo4j 3.5.3, python 3.7.x
我有:图中的一个节点 a
graph = Graph(
host="alpha.graph.domain.co",
auth=('neo4j', 'theActualPassword')
)
# grab the graph
a = Node("Type", url="https://en.wikipedia.org/wiki/Vivendi")
# create a local node with attributes I should be able to MERGE on
graph.merge(a,"Type","url")
# do said merge
graph.pull(a)
# pull any attributes (in my case Labels) that exist on the node in neo4j...
# ...but not on my local node
# better ways to do this also would be nice in the comments
relMatch = RelationshipMatcher(graph)
我想要的:有多少 "CREATED"
关系连接到 a
(在本例中为 7)
我尝试过的:
x = relMatch.get(20943820943)
使用关系的 ID 之一来查看什么是什么。它 returns None
,docs 表示
If no such Relationship is found, py:const:None is returned instead. Contrast with matcher[1234] which raises a KeyError if no entity is found.
这让我觉得我完全错了。
还有:relMatch.match(a,"CREATED")
这引发了
raise ValueError("Nodes must be supplied as a Sequence or a Set")
告诉我我绝对没有正确阅读文档。
不一定使用这个 class,这可能不是我想的那样,我如何计算有多少 ["CREATED"]
指向 a
?
下面是可行的,并且比以前的实现更容易编写,但我认为这不是正确的方法。
result = graph.run(
"MATCH(a) < -[r:CREATED]-(b) WHERE ID(a)=" + str(a.identity) + " RETURN count(r)"
).evaluate()
print(result)
使用 RelationshipMatcher
,您可以简单地使用 len
进行计数。因此,如果我正确阅读了您的代码,您将需要类似以下内容:
count = len(RelationshipMatcher(graph).match((None, a), "CREATED"))
或者更简单:
count = len(graph.match((None, a), "CREATED"))
(因为 graph.match
是 RelationshipMatcher(graph)
的快捷方式)
(None, a)
元组指定一对有序的节点(仅在一个方向上的关系),其中起始节点是任何节点,结束节点是a
。使用 len
简单地评估匹配和 returns 计数。
使用py2neo 4.x, neo4j 3.5.3, python 3.7.x
我有:图中的一个节点 a
graph = Graph(
host="alpha.graph.domain.co",
auth=('neo4j', 'theActualPassword')
)
# grab the graph
a = Node("Type", url="https://en.wikipedia.org/wiki/Vivendi")
# create a local node with attributes I should be able to MERGE on
graph.merge(a,"Type","url")
# do said merge
graph.pull(a)
# pull any attributes (in my case Labels) that exist on the node in neo4j...
# ...but not on my local node
# better ways to do this also would be nice in the comments
relMatch = RelationshipMatcher(graph)
我想要的:有多少 "CREATED"
关系连接到 a
我尝试过的:
x = relMatch.get(20943820943)
使用关系的 ID 之一来查看什么是什么。它 returns None
,docs 表示
If no such Relationship is found, py:const:None is returned instead. Contrast with matcher[1234] which raises a KeyError if no entity is found.
这让我觉得我完全错了。
还有:relMatch.match(a,"CREATED")
这引发了
raise ValueError("Nodes must be supplied as a Sequence or a Set")
告诉我我绝对没有正确阅读文档。
不一定使用这个 class,这可能不是我想的那样,我如何计算有多少 ["CREATED"]
指向 a
?
下面是可行的,并且比以前的实现更容易编写,但我认为这不是正确的方法。
result = graph.run(
"MATCH(a) < -[r:CREATED]-(b) WHERE ID(a)=" + str(a.identity) + " RETURN count(r)"
).evaluate()
print(result)
使用 RelationshipMatcher
,您可以简单地使用 len
进行计数。因此,如果我正确阅读了您的代码,您将需要类似以下内容:
count = len(RelationshipMatcher(graph).match((None, a), "CREATED"))
或者更简单:
count = len(graph.match((None, a), "CREATED"))
(因为 graph.match
是 RelationshipMatcher(graph)
的快捷方式)
(None, a)
元组指定一对有序的节点(仅在一个方向上的关系),其中起始节点是任何节点,结束节点是a
。使用 len
简单地评估匹配和 returns 计数。