Neo4j Cypher-如何限制50%的比赛结果

laik7k3q  于 2022-10-01  发布在  其他
关注(0)|答案(2)|浏览(191)

我想限制MATCH结果的50%,但看起来LIMIT不接受动态值。

我试过:

MATCH (:Profile) 
WITH COUNT(*) AS c
MATCH (n:Profile)
WITH n ORDER BY rand() LIMIT toInt(c * 0.5) 
RETURN n

然后我得到了错误:

It is not allowed to refer to variables in LIMIT

那么,有没有办法在不使用两个单独的查询的情况下做到这一点呢?

7lrncoxx

7lrncoxx1#

这就是我的看法。

1.获取所有配置文件,并为每行创建一个随机数字
1.将所有配置文件收集到配置文件列表中,并按随机数字(R)排序
1.计算配置文件列表大小的50%
1.从开始到cnt展开列表,然后返回每个节点

MATCH (n:Profile) 
WITH n, rand() as r ORDER by r 
WITH collect(n) as profile_lst
WITH profile_lst, toInt(size(profile_lst)/2) as cnt
UNWIND profile_lst[0..cnt] as prof
RETURN prof
35g0bw71

35g0bw712#

如果您安装了apoc插件,则可以使用apoc.coll.randomItems函数随机获得50%:

MATCH (n:Profile)
WITH COLLECT(n) AS people
WITH apoc.coll.randomItems(people, SIZE(people)/2) as profile_lst
UNWIND profile_lst as prof
RETURN prof

相关问题