如何使用Cypher在Neo4j中返回随机记录?

时间:2014-03-04 18:24:15

标签: random neo4j cypher

我有这样的查询:

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

但是当我运行它时,这会显示错误。它说:

  

捕获异常:无法执行查询[400]:标题:数组(   [Content-Type] =>应用/ JSON;字符集= UTF-8;流=真   [Access-Control-Allow-Origin] => * [Transfer-Encoding] =>分块   [服务器] => Jetty(9.0.z-SNAPSHOT))正文:数组([message] =>订购   BY表达式必须是确定性的。例如,你不能使用   表达式[exception] =>中的rand()函数PatternException   [fullname] => org.neo4j.cypher.PatternException [stacktrace] =>排列   ([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 $ $适用1.适用(TopPipe.scala:38)   [4] =>   org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe $$ anonfun $ 1 $$ anonfun $ $适用1.适用(TopPipe.scala:38)   [5] => scala.Option.forall(Option.scala:226)[6] =>   org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe $$ anonfun $ 1.适用(TopPipe.scala:38)   [7] =>   org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe $$ anonfun $ 1.适用(TopPipe.scala:38)   [8] =>   org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe $$ anonfun $ internalCreateResults $ 1.适用(TopPipe.scala:56)   [9] =>   org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe $$ anonfun $ internalCreateResults $ 1.适用(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.ExecutionPlanBuilder.org $ Neo4j的$暗号$内部$ $编译$ V2_0 $ executionplan $$ ExecutionPlanBuilder prepareStateAndResult(ExecutionPlanBuilder.scala:149)   [16] =>   org.neo4j.cypher.internal.compiler.v2_0.executionplan.ExecutionPlanBuilder $$ anonfun $ 2.适用(ExecutionPlanBuilder.scala:126)   [17] =>   org.neo4j.cypher.internal.compiler.v2_0.executionplan.ExecutionPlanBuilder $$ anonfun $ 2.适用(ExecutionPlanBuilder.scala:125)   [18] =>   org.neo4j.cypher.internal.compiler.v2_0.executionplan.ExecutionPlanBuilder $$匿名$ 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)))

请帮帮我。感谢。

1 个答案:

答案 0 :(得分:12)

您需要按节点属性排序,而不是按函数排序。您可以执行以下操作(如果您的节点包含例如属性'name'):

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