简体   繁体   中英

How to return random records in Neo4j using Cypher?

I have query like this:

$query = "MATCH (U:User)
          RETURN U
          ORDER BY RAND()
          LIMIT 100";

But when I run this, this shows errors. It says:

Caught Exception: Unable to execute query [400]: Headers: Array ( [Content-Type] => application/json; charset=UTF-8; stream=true [Access-Control-Allow-Origin] => * [Transfer-Encoding] => chunked [Server] => Jetty(9.0.z-SNAPSHOT) ) Body: Array ( [message] => ORDER BY expressions must be deterministic. For instance, you cannot use the rand() function in the expression [exception] => PatternException [fullname] => org.neo4j.cypher.PatternException [stacktrace] => Array ( [0] => org.neo4j.cypher.internal.compiler.v2_0.commands.SortItem.apply(SortItem.scala:30) [1] => org.neo4j.cypher.internal.compiler.v2_0.pipes.ExecutionContextComparer$class.compareBy(SortPipe.scala:43) [2] => org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe.compareBy(TopPipe.scala:33) [3] => org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe$$anonfun$1$$anonfun$apply$1.apply(TopPipe.scala:38) [4] => org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe$$anonfun$1$$anonfun$apply$1.apply(TopPipe.scala:38) [5] => sc ala.Option.forall(Option.scala:226) [6] => org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe$$anonfun$1.apply(TopPipe.scala:38) [7] => org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe$$anonfun$1.apply(TopPipe.scala:38) [8] => org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe$$anonfun$internalCreateResults$1.apply(TopPipe.scala:56) [9] => org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe$$anonfun$internalCreateResults$1.apply(TopPipe.scala:49) [10] => scala.collection.Iterator$class.foreach(Iterator.scala:727) [11] => org.neo4j.cypher.internal.compiler.v2_0.pipes.HeadAndTail.foreach(SlicePipe.scala:72) [12] => org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe.internalCreateResults(TopPipe.scala:49) [13] => org.neo4j.cypher.internal.compiler.v2_0.pipes.PipeWithSource.createResults(Pipe.scala:71) [14] => org.neo4j.cypher.internal.compiler.v2_0.pipes.PipeWithSource.createResults(Pipe.scala:68) [15] => org.neo4j.cypher.internal.compiler.v2_0.executionplan.ExecutionPlanB uilder.org$neo4j$cypher$internal$compiler$v2_0$executionplan$ExecutionPlanBuilder$$prepareStateAndResult(ExecutionPlanBuilder.scala:149) [16] => org.neo4j.cypher.internal.compiler.v2_0.executionplan.ExecutionPlanBuilder$$anonfun$2.apply(ExecutionPlanBuilder.scala:126) [17] => org.neo4j.cypher.internal.compiler.v2_0.executionplan.ExecutionPlanBuilder$$anonfun$2.apply(ExecutionPlanBuilder.scala:125) [18] => org.neo4j.cypher.internal.compiler.v2_0.executionplan.ExecutionPlanBuilder$$anon$6.execute(ExecutionPlanBuilder.scala:50) [19] => org.neo4j.cypher.internal.ExecutionPlanWrapperForV2_0.execute(CypherCompiler.scala:93) [20] => org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:61) [21] => org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:65) [22] => org.neo4j.cypher.javacompat.ExecutionEngine.execute(ExecutionEngine.java:78) [23] => org.neo4j.server.rest.web.CypherService.cypher(CypherService.java:100) [24] => java.lang.reflect.Method.invoke(Unknown Source) [25] => org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139) [26] => org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112) [27] => java.lang.Thread.run(Unknown Source) ) )

Please help me. Thanks.

You'll need to order by a node property, not a function. You could do the following (if your node contains eg the property 'name'):

MATCH (u:User)
WITH u, rand() AS number
RETURN u
ORDER BY number
LIMIT 100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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