选择集合的随机元素

时间:2014-01-13 17:39:26

标签: neo4j cypher

从集合中随机选择项目的最佳方法是什么?

例如,我正在尝试这样做:

    match (m:Merchant) return collect(m)[round(rand())]

但是我收到此错误,这表明round()返回一个double:

    Type mismatch: expected Integer or Long but was Double (line 1, column 38)
    "match (m:Merchant) return collect(m)[round(rand())] limit 10"

我会使用这篇文章neo4j: Is there a way/how to select random nodes?中提供的答案,但我不想要连续的节点。

2 个答案:

答案 0 :(得分:1)

根据文档,round应该返回一个整数(试过这个,确实给了我一个整数)。任何方式,round(rand())会给你0或1对吗?所以也许尝试类似的东西:

match (m:Merchant)
with collect(m) as allMerchants, length(collect(m)) as totalMerchants
return allMerchants[round(rand()*(totalMerchants-1))]

答案 1 :(得分:0)

经过一些工作,我发现你可以按照rand()对项目进行分组,然后按rand排序。

例如:        匹配(m:商家)与m,rand()作为兰特返回m次按兰特限制1

希望这有助于某人!

相关问题