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

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

问题描述

是否可以将关系从一个节点复制或移动到另一个节点?

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复制不同类型的关系

说我在图中有这个图案

(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

但是,这依赖于许多查询以及交易. (在我的简单视图中)能够在一个查询中实现这一点将是更可取的.但是,我不相信密码中会存在这样的东西.我错了吗?

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天全站免登陆