简体   繁体   English

密码:在Neo4j中匹配随机节点

[英]Cypher: Match random node in Neo4j

I have a database with 3.4 millions of nodes and want to select a random node. 我有一个包含340万个节点的数据库,并且想要选择一个随机节点。

I tried using something like 我尝试使用类似

MATCH (n) 
WHERE rand() <= 0.01
RETURN n 
LIMIT 1

but it seems like the algorithm always starts with the same nodes and selects the first one whose random number is below 0.01, which means in most cases the "random" node is one of the first 100 checked nodes. 但是似乎算法总是从相同的节点开始,并选择随机数低于0.01的第一个节点,这意味着在大多数情况下,“随机”节点是前100个检查的节点之一。

Is there a better query, to select a completely random one of all my nodes? 是否有更好的查询来选择我所有节点中的一个完全随机的节点?

You could generate an random ID from the rand() function and multiply it by the number of nodes. 您可以从rand()函数生成一个随机ID,然后将其乘以节点数。 This should generally return a more random node. 通常,这应该返回一个更随机的节点。

MATCH (n) 
WHERE id(n) = toInteger(rand() * 3400000)

Once there is some space created within your nodes (ie they are no longer perfectly contiguous due to deletes) you might miss a few here and there. 一旦在节点内创建了一些空间(即由于删除,它们不再是完美连续的),您可能会在这里和那里错过一些空间。 In that case you could always range the random number +/- a few on either side and return the first row of the result. 在这种情况下,您总是可以将随机数的范围设置为+/-几,然后返回结果的第一行。

WITH toInteger(rand() * 3400000) AS rand_node, 5 AS offset
WITH range(rand_node - offset, rand_node + offset) AS rand_range
MATCH (n) 
WHERE id(n) IN rand_range
RETURN n
LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM