使用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来查看它是什么。它返回None
,docs说的意思
如果没有找到这样的关系,则返回py:const:None。与匹配器[1234]对比,如果没有找到实体则会引发KeyError。
这让我觉得我对此完全错了。
还有:加油的relMatch.match(a,"CREATED")
提高ValueError(“节点必须作为序列或集合提供”)
告诉我,我绝对不会正确阅读文档。
不一定使用这个类,这可能不是我认为的那样,我如何计算["CREATED"]
指向了多少a
?
使用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
只需评估匹配并返回计数。
下面的工作,比以前的实现更容易编写,但我不认为这是正确的方法。
result = graph.run(
"MATCH(a) < -[r:CREATED]-(b) WHERE ID(a)=" + str(a.identity) + " RETURN count(r)"
).evaluate()
print(result)