如何使用py2n计算与节点的关系(一种类型)

2024-06-25 23:28:14 发布

您现在位置:Python中文网/ 问答频道 /正文

使用py2neo4.x、neo4j3.5.3、python3.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"关系连接到aA neo4j return describing how these relationships are connected to node a(在本例中为7)

我所做的:

x = relMatch.get(20943820943)使用关系的一个id来查看什么是什么。它返回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")

告诉我我肯定读不好这些文件。在

不一定要使用这个类,这可能不是我所认为的,我如何计算有多少["CREATED"]指向{}?在


Tags: theinnodeurlisonlocaltype
2条回答

使用RelationshipMatcher,您只需使用len进行计数。因此,如果我正确地阅读了您的代码,您将需要类似于:

count = len(RelationshipMatcher(graph).match((None, a), "CREATED"))

甚至更简单:

^{pr2}$

(因为graph.matchRelationshipMatcher(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)

相关问题 更多 >