Neo4j Python Driver:将节点作为参数传递给数据库。

huangapple go评论61阅读模式
英文:

Neo4j Python Driver: Pass nodes as parameter to DB

问题

我想使用 Python 驱动程序为 Neo4j 创建两个节点之间的新边缘。

def cypher_orchestration(tx, start_identifier: str, end_identifier:str, edge_type: str):
    start_node = node_match(tx, start_identifier).single()
    end_node = node_match(tx, end_identifier).single()
    edge = create_edge(tx, start_node, edge_type, end_node)

我已经创建了 "node_match" 函数,并成功接收到搜索的节点。但现在我在创建两个节点之间的边缘时遇到问题。

我创建了 "create_edge" 函数,它看起来像这样:

from neo4j.graph import Node

def create_edge(tx, start_node: Node, edge_type: str, end_node: Node):
    result = tx.run(f"""
        CALL apoc.merge.relationship($start_node, $edge_type, {{}}, $end_node, {{}}) yield rel
        return rel
        """, start_node=start_node, edge_type=edge_type, end_node=end_node)
    return result

不幸的是,我不理解如何将之前查询的节点作为参数传递给查询。如何将 Node 对象传递给 Neo4j?这是否可能?

我也知道,我可以在我的 "create_edge" 函数中再次匹配这两个节点,但我相信(并希望)必须有一种更...更优雅的方式。如果有人知道如何实现这一点或知道类似的方法,我将很高兴向您学习 Neo4j Python Driver:将节点作为参数传递给数据库。

谢谢你的帮助!
问候,ottonormal

英文:

i want create a new edge between two nodes using the python driver for neo4j.

def cypher_orchestration(tx, start_identifier: str, end_identifier:str, edge_type: str):
    start_node = node_match(tx, start_identifier).single()
    end_node = node_match(tx, end_identifier).single()
    edge = create_edge(tx, start_node, edge_type, end_node)

I created the function "node_match" and can successfully receive the searched nodes.
But now i have problems with creating a edge between both nodes.

What i did, is creating the "create_edge" function which looks like this:

from neo4j.graph import Node

def create_edge(tx, start_node: Node, edge_type: str, end_node: Node):
    result = tx.run(f"""
        CALL apoc.merge.relationship($start_node, $edge_type, {{}}, $end_node, {{}}) yield rel
        return rel
        """, start_node=start_node, edge_type=edge_type, end_node=end_node)
    return result

Unfortunately i don't understand, how to pass a node which was queried before as parameter to a query. How can i pass a Node Object to neo4j? Is this even possible?

I also know, that i could match both nodes again in my "create edge" function but i believe (and hope) that there must be a more... more elegant way. If someone knows how to archieve this or knows a similar way, i would be happy to learn from you Neo4j Python Driver:将节点作为参数传递给数据库。

Thanks for your help!
Greetings, ottonormal

答案1

得分: 2

你不能将节点或关系对象作为查询参数传递。

apoc.merge.relationship的情况下,起始节点和结束节点在Cypher查询中定义。它看起来像:

MATCH (start:SomeLabel {some: "property"})
MATCH (end:SomeOtherLabel {some-other: "property"})
CALL apoc.merge.relationship(start, "SOME_REL_TYPE", {}, {}, end, {}) YIELD rel
RETURN rel
英文:

You cannot pass node or relationship objects as query parameters.

In the case of apoc.merge.relationship, the start and end node are defined within the Cypher query itself. It looks like:

MATCH (start:SomeLabel {some: "property"})
MATCH (end:SomeOtherLabel {some-other: "property"})
CALL apoc.merge.relationship(start, "SOME_REL_TYPE", {}, {}, end, {}) YIELD rel
RETURN rel

huangapple
  • 本文由 发表于 2023年5月22日 23:22:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定