gpt4 book ai didi

neo4j - 使用 NEO4j 显示具有多个关系的节点

转载 作者:行者123 更新时间:2023-12-01 22:28:25 25 4
gpt4 key购买 nike

我使用以下代码创建了一个图表。当每个用户连接到多个视频以及每个视频连接到多个用户时,如何不仅返回节点而且返回关系?

CREATE CONSTRAINT ON (u:User) ASSERT u.user IS UNIQUE;
CREATE CONSTRAINT ON (v:Video) ASSERT v.video IS UNIQUE;

USING PERIODIC COMMIT 100000
LOAD CSV WITH HEADERS FROM 'asdfjkl;' AS line
WITH distinct line.user as user_data
MERGE (:User {user: user_data });

USING PERIODIC COMMIT 100000
LOAD CSV WITH HEADERS FROM 'asdfjkl;' AS line
WITH distinct line.video as video_data
MERGE (:Video {video: video_data });

USING PERIODIC COMMIT 100000
LOAD CSV WITH HEADERS FROM 'asdfjkl;' AS line
MATCH (u:User {user: line.user })
MATCH (v:Video {video: line.video})
MERGE (u)-[:VIEW]->(v);

最佳答案

根据更新的问题进行编辑-获取连接到多个视频的用户的节点和关系:

MATCH (u:User)-[r:VIEW]->(:Video)
WITH u, count(r) AS count
WHERE count > 1
MATCH (u)-[r:VIEW]->(v:Video)
RETURN u,r,v;

获取连接到多个用户的视频的节点和关系:

MATCH (:User)-[r:VIEW]->(v:Video)
WITH v, count(r) AS count
WHERE count > 1
MATCH (u:User)-[r:VIEW]->(v)
RETURN u,r,v;

请注意,在这两种情况下,因为您也需要关系,所以每个 u、r、v 都会得到一行。如果您不想要关系,您可以收集所有视频,例如:

   MATCH (u:User)-[r:VIEW]->(:Video)
WITH u, count(r) AS count
WHERE count > 1
MATCH (u)-[r:VIEW]->(v:Video)
RETURN u,collect(v);

最后,获取已观看多个视频的用户,其中视频已被多个用户观看:

MATCH (u:User)-[r:VIEW]->(:Video)
WITH u, count(r) AS count
WHERE count > 1
MATCH (u)-[r:VIEW]->(v:Video)<-[r2:VIEW]-(other:User)
RETURN u,r,v,collect(other);

关于neo4j - 使用 NEO4j 显示具有多个关系的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30783783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com