将所有关系移动/复制到不同的节点 [英] Move/copy all relationships to different node

查看:20
本文介绍了将所有关系移动/复制到不同的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将关系从一个节点复制或移动到另一个节点?

Is there any way to copy or move a relationship from one node to another?

我这里也有类似的情况:

I have a situation similar to that here:

neo4j 合并 2 个或多个重复节点

这里:

使用 Cypher 复制不同类型的关系

假设我在图中有这种模式

Say I have this pattern in the graph

(a)-[r:FOO]->(b)
(a)<-[r2:BAR]-(c)

然后我有另一个节点,(d),它可能是也可能不是 (a) 的副本.我的想法是,从功能的角度来看,节点是否重复并不重要.我希望能够移动或复制关系 r:FOOr2:BAR 以便图形现在包括

I then have another node, (d), which may or may not be a duplicate of (a). My thinking is that it does not matter whether the nodes are duplicate or not from a functionality point of view. I want to be able to move or copy the relationships r:FOO and r2:BAR so that the graph now includes

(d)-[r:FOO]->(b)
(d)<-[r2:BAR]-(c)

如果我在有重复项时这样做来合并节点,我希望能够移动关系而不是复制然后(也许可选)删除 (a).请注意,有不止一种关系类型,我不确定这些类型是什么.我意识到我可以分阶段执行此操作,但认为如果有一种有效的方法可以在一个密码查询中执行此操作,那就太好了.我目前的策略类似于(不是确切的语法,只是提供一个想法)

If I was then doing this to merge nodes when I have duplicates I would like to be able to move the relationships as opposed to copy and then (perhaps optionally) delete (a). Note that there is more than one relationship type and I do not know for sure what the types will be. I realise I can do this in stages but thought it would be great if there was an efficient way to do it in one cypher query. My current strategy is something like (not exact syntax but just to give an idea)

// get all outgoing relationships
MATCH (a:Label1 { title : 'blah' })-[r]->(o)
RETURN r
// returns FOO and BAR

// for each relationship type, create one from (d) and copy the properties over
MATCH (a:Label1 { title : 'blah' })-[r:FOO]->(o), (d:Label1 { title : 'blah blah' })
CREATE (d)-[r2:FOO]->(o)
SET r2 = r
...etc...

// now do the same for incoming relationships
MATCH (a:Label1 { title : 'blah' })<-[r]-(o)
RETURN r
// returns FOO and BAR
// for each relationship type, create one from (d) and copy the properties over
MATCH (a:Label1 { title : 'blah' })<-[r:FOO]-(o), (d:Label1 { title : 'blah blah' })
CREATE (d)<-[r2:FOO]-(o)
SET r2 = r
...etc...

// finally delete node and relationships (if required)
MATCH (a:Label1 { title : 'blah' })-[r]-(o)
DELETE r, a

然而,这依赖于许多查询和事务.能够在一个查询中实现这一点会更可取(在我的简单观点中).但是,我不相信 cypher 中存在这样的东西.我错了吗?

However this relies on a number of queries and hence transactions. It would be much preferable (in my simpleton view) to be able to achieve this in one query. However, I do not believe anything like this exists in cypher. Am I wrong?

有什么想法吗?如果不清楚,请告诉我,我会尽量详细说明和进一步解释.

Any ideas? Let me know if this is not clear and I shall try to elaborate and explain further.

有关信息,我使用的是 Neo4j 2.1.6 社区版(使用来自 .NET 应用程序的 neo4jclient).

For info, I am using Neo4j 2.1.6 community edition (with neo4jclient from a .NET application).

刚刚意识到我还必须重复我的过程来解释关系的方向,除非我弄错了?即从 (a) 获取所有传出关系,将它们重新创建为从 (d) 传出,然后对所有传入关系执行相同操作.以上密码已相应编辑.

Just realised that I would also have to repeat my process to account for the direction of the relationship unless I am mistaken? i.e. get all outgoing relationships from (a), recreate them as outgoing from (d), and then do the same for all incoming relationships. Cypher above has been edited accordingly.

更新:我猜这是一个白日梦,根本不可能.任何人都可以证实这一点吗?即使是不!",也有明确的答案.如果是这样,我会考虑询问 Neo4j 人员此功能是否可行且值得考虑.

UPDATE: I am guessing this is a pipe dream and not at all possible. Can anyone confirm this? It would be good to have a definitive answer even if it is "No!". If so I would consider asking the Neo4j guys if this functionality is even feasible and worth considering.

更新 2:由于缺乏想法,我猜这是不可能的.我当然没有进一步的研究或实验.看起来功能请求是要走的路.我不可能是唯一一个会发现此功能非常有用的人.

UPDATE 2: From the lack of ideas I am guessing this can't be done. I certainly have got no further in my research or experimentation. Looks like a feature request is the way to go. I can't be the only person who would find this functionality exceptionally useful.

推荐答案

我认为你可以将这些链接在一起:

I think you can just chain these together:

// get all relationships
MATCH
  (a:Label1 { title : 'blah' })-[r]-(o),
  (d:Label1 { title : 'blah blah' })
CREATE (d)-[r2:type(r)]-(o)
DELETE r, a

我唯一不完全确定的是是否能够使用 type() 函数在那里使用它.我现在就试试

The only thing I'm not entirely sure about is the ability to use the type() function where it's being used there. I'll try it out now

这篇关于将所有关系移动/复制到不同的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆