Neo4j 选择集合中的随机元素
从集合中随机选择项目的最佳方式是什么 例如,我正在尝试这样做:Neo4j 选择集合中的随机元素,neo4j,cypher,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(r
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"
我会使用这篇文章中提供的答案,但我不想要连续的节点。根据文档,round应该返回一个整数(尝试了这个,它确实给了我一个整数)。无论如何,round(rand())将为您提供0或1,对吗?因此,不妨尝试以下方法:
match (m:Merchant)
with collect(m) as allMerchants, length(collect(m)) as totalMerchants
return allMerchants[round(rand()*(totalMerchants-1))]
经过一些工作,我发现您可以按rand()对项目进行分组,然后按rand排序 例如: 将(m:Merchant)与m匹配,rand()作为rand按rand limit 1返回m订单
希望这对别人有帮助 我认为这是
round()
中的一个bugRETURN[1,2,3][round(rand())]
,RETURN[1,2,3][round(0.1)]
和将round(0.1)作为x RETURN[1,2,3][x]
都会抛出该异常,而RETURN[1,2,3][round(0.1)*1]
有效。与floor()
相同,它们只是不返回整数。除非新成员有其他的输入,否则你可能会考虑在Github上提出一个问题。这里有一个类似的(封闭的)问题:这比Luane的解决方案更昂贵。