neo4j: Is there a way/how to select random nodes?

2020-07-02 09:05发布

问题:

I would like to retrieve a specific number of random nodes. The graph consists of 3 000 000 nodes where some of them are sources, some are target and some are both.

The aim is to retrieve random sources and as I don't know how to select random, the program generates k random numbers from 1 to 3 000 000 which represent node IDs and then discards all randomly selected nodes that are not sources. As this procedure is time-consuming, I wonder whether it is possible to directly select random sources with cypher query.

In case to select all sources, the query would be the following

START t=node(*) MATCH (a)-[:LEADS_TO]->(t) RETURN a

Does anyone know how would it be possible to select the limited number of random nodes directly with a cypher or, if not possible, suggest any workaround?

回答1:

You can limit your query with skip/limit so you could do

START t=node(*) 
MATCH (a)-[:LEADS_TO]->(t) 
RETURN a
SKIP {randomoffset} LIMIT {randomcount} 

Otherwise you can also create a set of random node-id's and pass them as parameter to the cypher statement.



回答2:

You can use such construction:

MATCH (a)-[:LEADS_TO]->(t) 
RETURN a, rand() as r
ORDER BY r

It should return you random set of object.

Tested with Neo4j 2.1.3



回答3:

Another way of the one suggested here, for case you want a random Start nodes with all there connections is:

MATCH (a)-[:LEADS_TO]->[]
WITH a,rand() AS rand
ORDER BY rand LIMIT {YourLimit}
MATCH (a)-[l:LEADS_TO]->(t)
RETURN a,l,t


回答4:

MATCH (n:Label)
WITH n, rand() AS r
ORDER BY r
RETURN n LIMIT <no. of random nodes>


标签: neo4j cypher