簡體   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