计算交叉点Neo4j

zpgglvta  于 2022-10-01  发布在  其他
关注(0)|答案(1)|浏览(184)

我有一个(:User)-[:Follows]->(:Channel)Schama。我想计算一下所有可能的航道交叉口。为了找到所有频道组合,我使用了apoc.coll.combination。在搜索了一下后,发现这个过滤器将节点共同按输入节点的个数进行了匹配,尝试应用相同的思想,把它跟进去了。

match (c:Channel)
with collect(c) as channels
with apoc.coll.combinations(channels, 1, size(channels)) as combinations
unwind combinations as combination
match (u:User)-->(c:Channel) where c in combination
with *, count(DISTINCT c) as ccount, size(combinations) as csize
where ccount = csize
return c.name, count(distinct u)

只是为了测试,我将组合调用更改为最小和最大2。但得到的结果是空集。我是不是遗漏了什么?根据一系列频道统计用户的最简单方法是什么?

Neo4J是这项工作的合适工具吗?

xpcnnkqh

xpcnnkqh1#

您可以尝试此查询:

match (u:User)-[:SUBSCRIBE]->(c:Channel) WITH u, collect(id(c)) AS userChannels, collect(c.name) AS channelNames
match (c:Channel)
with u, userChannels, channelNames, collect(id(c)) as channels
with apoc.coll.combinations(channels, 1, size(channels)) as combinations, u, userChannels, channelNames
unwind combinations as combination
WITH u, userChannels, channelNames, combination WHERE size(combination) = size(userChannels) AND ALL(x IN userChannels WHERE x IN combination)
RETURN channelNames, count(u) AS usersCount

在这个查询中,我们在两个列表中为每个用户收集频道ID和名称。然后,我们计算所有的通道节点组合。在解开组合后的最后,我们检查combination列表和userChannels列表的大小是否相同,如果是,我们比较内容。最后,对于每个组合,我们返回用户数。

要回答neo4j是否是适合这项工作的工具,可以提供数据集在某种意义上是有界的。当前的查询基本上是遍历数据库中存在的每个节点和关系,而且随着时间的推移,组合也会增加,因此扩展可能会成为一个问题。

尝试此查询,这将给出预期的计数:

match (c:Channel)
with collect(c.name) as channels
with apoc.coll.combinations(channels, 1, size(channels)) as combinations
match (u:User)-[:SUBSCRIBE]->(c:Channel)
with u, collect(c.name) AS userChannels, combinations
UNWIND combinations AS combination
WITH combination, u WHERE size(combination) <= size(userChannels) and apoc.coll.containsAll(userChannels, combination)
return combination, count(distinct u)

相关问题