简体   繁体   English

Neo4j py2neo合并

[英]Neo4j py2neo merge

I have some questions on how to leverage graph.merge() in py2neo. 我对如何在py2neo中利用graph.merge()有一些疑问。 I am trying to search in Neo4j to find if there is an existed account_node and link it with payment_change_event. 我正在尝试在Neo4j中搜索以查找是否存在account_node并将其与payment_change_event链接。

        account_node = graph.find_one('Payment_account', property_key="payment_account_id", property_value=payment_account_id)
        if not account_node:
            account_node = Node('Payment_account', payment_account_id=payment_account_id,\
                email=email, bank_name=bank_name, account_number=account_number
        changed_to_edge = Relationship(payment_change_event_node, 'CHANGED_TO', account_node)
        graph.create(changed_to_edge)

I am thinking of changing the logic to use graph.merge() like below instead of using find_one() and update the graph. 我正在考虑更改逻辑以使用如下所示的graph.merge()而不是使用find_one()并更新图形。

            account_node = Node('Payment_account', payment_account_id=new_payment_account_id)
            graph.merge(account_node)
            account_node['email'], account_node['bank_name'] = email, bank_name
            account_node['account_numner'] = account_number
            graph.push(account_node)
            changed_to_edge = Relationship(payment_change_event_node, 'CHANGED_TO', account_node)
            graph.create(changed_to_edge)

However, my question is by doing the above I will need to rewrite all the nodes all the time. 但是,我的问题是通过执行上述操作,我将需要始终重写所有节点。

Is there any way for py2neo that can merge all the properties(even for non-constraint property) py2neo有什么办法可以合并所有属性(即使对于非约束属性)

Let's say account_node has payment_account_id as the constraint property and email, bank_name and account_number are non-constraint properties. 假设account_node具有payment_account_id作为约束属性,而电子邮件,bank_name和account_number是非约束属性。

Now I already have had this account_node in the neo4j. 现在,我已经在neo4j中拥有了这个account_node。

account_node: payment_account_id='123', email='abc@abc.com', bank_name='mybank', account_number='123456' account_node:Payment_account_id ='123',email='abc@abc.com',bank_name ='mybank',account_number ='123456'

Then I want to run the process to change it to check if this node is changed. 然后,我想运行该过程以对其进行更改,以检查此节点是否已更改。 If some non-constraint properties changed I want update them. 如果某些非约束属性发生变化,我想更新它们。

If not I don't want to touch it but just link it to a payment_change_event_node. 如果不是,我不想触摸它,而只是将其链接到payment_change_event_node。

I try to do 我尝试做

Node('Payment_account', payment_account_id=payment_account_id, email=email, bank_name=bank_name, account_number=account_number)

But it will complain on the merge(). 但是它将在merge()上抱怨。

File "test_graph.py", line 159, in main
    graph.merge(account_node)

py2neo.database.status.ConstraintError: Node(443971) already exists with label `Payment_account` and property `payment_account_id` = 'xxxxxxxxxxxxxxx'

Any efficient way to achieve that in py2neo? 有什么有效的方法来实现py2neo吗? Is there any way like this? 有什么办法吗? I hope there is a function only merges on constraint properties and update the changed non-constraint properties. 我希望有一个仅在约束属性上合并并更新更改的非约束属性的函数。

account_node = Node('Payment_account', payment_account_id=payment_account_id, email=email, bank_name=bank_name, account_number=account_number)
graph.merge(account_node)
graph.push(account_node)
changed_to_edge = Relationship(payment_change_event_node, 'CHANGED_TO', account_node)
graph.create(changed_to_edge)

When you MERGE a node neo4j looks for all properties of that node. 当您MERGE的Neo4j查找该节点的所有属性的节点。

If it does not find a node that has all properties, it will create a new one. 如果找不到具有所有属性的节点,它将创建一个新的节点。

In your case, a Payment_account node with the payment_account_id exists already. 在您的情况下,已经存在带有payment_account_idPayment_account节点。 When you merge a node with the same payment_account_id but different email , bank_name properties, neo4j tries to create a new node with the specified properties. 当您合并具有相同的payment_account_id但具有不同的emailbank_name属性的节点时,neo4j会尝试创建具有指定属性的新节点。

Since you have a uniqueness constraint on payment_account_id , the node cannot be created. 由于您在payment_account_id上具有唯一性约束,因此无法创建该节点。

The solution is to MERGE only on the unique property and then update the other properties (like you did in your example). 解决方案是仅对唯一属性进行MERGE ,然后更新其他属性(就像您在示例中所做的那样)。

If you do not want to set node properties every time, you have to check if the property exists already. 如果不想每次都设置节点属性,则必须检查该属性是否已经存在。

account_node = Node('Payment_account', payment_account_id=new_payment_account_id)
graph.merge(account_node)
# check if you have to set a property
if account_node['email'] != email:
    account_node['email'] = email   
graph.push(account_node)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM