繁体   English   中英

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

[英]How to return random records in Neo4j using Cypher?

我有这样的查询:

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

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

捕获异常:无法执行查询[400]:标题:数组([Content-Type] => application / json; charset = UTF-8; stream = true [Access-Control-Allow-Origin] => * [转移 - 编码] => chunked [Server] => Jetty(9.0.z-SNAPSHOT))正文:数组([message] => ORDER BY表达式必须是确定性的。例如,你不能在表达式中使用rand()函数[ 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(未知来源)[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(未知来源)))

请帮我。 谢谢。

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

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

暂无
暂无

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

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