根据节点之间现有的关系创建节点之间的关系

发布于 2025-01-13 21:40:38 字数 400 浏览 0 评论 0原文

我在两个用户之间有多个关系,如下所示:

(u1:User)-[:BR]-(b:Buyer {value:"A"})-[:BR]-(u2:User)
(u1:User)-[:BR]-(b:Buyer {value:"B"})-[:BR]-(u2:User)
(u1:User)-[:BR]-(b:Buyer {value:"C"})-[:BR]-(u2:User)

我想将所有这些关系组合成两个用户之间的单个关系(同时保留旧的关系),同时保留节点的值来创建列表并设置如下新关系的价值是这样的:

(u1:User)-[:R {data:["A", "B", "C"]}]-(u2:User)

有什么办法可以做到这一点吗?

编辑:关键字可以出现多次。

I have multiple relationships between two users that are as follow:

(u1:User)-[:BR]-(b:Buyer {value:"A"})-[:BR]-(u2:User)
(u1:User)-[:BR]-(b:Buyer {value:"B"})-[:BR]-(u2:User)
(u1:User)-[:BR]-(b:Buyer {value:"C"})-[:BR]-(u2:User)

I'd like to combine all of them into a single relationship (while keeping the old ones) between the two users, while keeping the node's value to create a list and set is as the new relationship's value like this:

(u1:User)-[:R {data:["A", "B", "C"]}]-(u2:User)

Is there any way to do this?

Edit: keywords can appear more than once.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心小果奶 2025-01-20 21:40:38

这是我的测试数据。我创建了不同场景的示例数据。问题是 u1 和 u2 具有非方向关系,因此您将获得从 u1 到 u2 以及 u2 到 u1 的重复路径。

CREATE (u1:User)
CREATE (u2:User)
CREATE (u3:User)
CREATE (ba:Buyer {value:"A"})
CREATE (bb:Buyer {value:"B"})
CREATE (bc:Buyer {value:"C"})
CREATE (bd:Buyer {value:"D"})
MERGE (u1)-[:BR]-(ba)-[:BR]-(u2)
MERGE (u1)-[:BR]-(bb)-[:BR]-(u2)
MERGE (u1)-[:BR]-(bc)-[:BR]-(u2)
MERGE (u2)-[:BR]-(bb)-[:BR]-(u3)
MERGE (u2)-[:BR]-(bd)-[:BR]-(u3)

步骤1是确保u1和u2不相同。然后收集不同的值并按该值对其进行排序。然后我创建了所有节点的集合来生成行号(称为 uw 或 row)。然后仅为偶数行创建关系,以便不存在重复项。最后,使用值作为数据创建从 u1 到 u2 的关系。

MATCH (u1:User)-[:BR]-(b:Buyer)-[:BR]-(u2:User) where u1 <> u2
WITH u1, u2, collect(distinct b.value) as v  order by v
WITH collect([u1,u2,v]) as cols 
UNWIND range(1, size(cols)) as uw 
WITH uw as row, cols[uw-1][0] as u1, cols[uw-1][1] as u2, cols[uw-1][2] as values WHERE row%2=0
MERGE (u1)-[:R {data: values}]-(u2)

结果:
输入图片此处描述

This is my test data. I created sample data with different scenarios. The problem is u1 and u2 have non-directional relationships so you will get duplicated paths from u1 to u2 and u2 to u1.

CREATE (u1:User)
CREATE (u2:User)
CREATE (u3:User)
CREATE (ba:Buyer {value:"A"})
CREATE (bb:Buyer {value:"B"})
CREATE (bc:Buyer {value:"C"})
CREATE (bd:Buyer {value:"D"})
MERGE (u1)-[:BR]-(ba)-[:BR]-(u2)
MERGE (u1)-[:BR]-(bb)-[:BR]-(u2)
MERGE (u1)-[:BR]-(bc)-[:BR]-(u2)
MERGE (u2)-[:BR]-(bb)-[:BR]-(u3)
MERGE (u2)-[:BR]-(bd)-[:BR]-(u3)

Step1 is to ensure that u1 and u2 are not the same. Then collect distinct value and sort it by this values. Then I created a collection of all the nodes to generate a row number (called it uw or row). Then create the relationship for even rows only so that there is no duplicates. Lastly, create the relationship from u1 to u2 using values as data.

MATCH (u1:User)-[:BR]-(b:Buyer)-[:BR]-(u2:User) where u1 <> u2
WITH u1, u2, collect(distinct b.value) as v  order by v
WITH collect([u1,u2,v]) as cols 
UNWIND range(1, size(cols)) as uw 
WITH uw as row, cols[uw-1][0] as u1, cols[uw-1][1] as u2, cols[uw-1][2] as values WHERE row%2=0
MERGE (u1)-[:R {data: values}]-(u2)

Result:
enter image description here

阳光下的泡沫是彩色的 2025-01-20 21:40:38

我不知道这是否是最有效的方法,但我可以使用以下命令来实现此目的:

match (u1:User)-[:BR]-(b:Buyers)-[:BR]-(u2:User) with u1, u2, collect(b.value) as bys merge (u1)-[:R {data:bys}]-(u2)

编辑:然后删除重复的关系:

match (u1:User {userid:"622f0137d799ed4369b077e1"})-[r:R]->(u2)-[:R]->(u1)
DELETE r

I don't know if this is the most efficient way but I'm able to achieve this with thee following command:

match (u1:User)-[:BR]-(b:Buyers)-[:BR]-(u2:User) with u1, u2, collect(b.value) as bys merge (u1)-[:R {data:bys}]-(u2)

Edit: And then remove the duplicate relationships with:

match (u1:User {userid:"622f0137d799ed4369b077e1"})-[r:R]->(u2)-[:R]->(u1)
DELETE r
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文