2014-03-04 53 views
6

我有這樣的查詢:如何使用Cypher在Neo4j中返回隨機記錄?

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

但是當我運行這個,這個顯示錯誤。它說:

捕獲的異常:無法執行查詢[400]:集管:陣列( [內容類型] =>應用/ JSON;字符集= UTF-8;流=真 [訪問控制-Allow-Origin] => * [Transfer-Encoding] => chunked [Server] => Jetty(9.0.z-SNAPSHOT))Body:Array([message] => ORDER BY表達式必須是確定性的。 ,您無法在表達式[exception] => PatternException [fullname] => org.neo4j.cypher.PatternException [stacktrace] => Array ([0] => org.neo4j中使用 rand()函數。 cypher.internal.compiler.v2_0.commands.SortItem.apply(SortItem.sca la: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) (選項卡:226)[6] => org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe $$ anonfun $ 1.apply(TopPipe.scala: 38) [7] => org.neo4j.cypher.internal.compile r.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.scal a)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 $ 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。階:61) [21] => org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:65) [22] => org.neo4j.cypher.javacompat.ExecutionEngine。執行(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(來源不明)))

請幫助我。謝謝。

回答

10

您需要按節點屬性進行排序,而不是函數。您可以執行以下操作(如果您的節點包含例如屬性「名稱」):

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

在大型數據集中可能會很慢。 – programmer5000

+0

你的編輯使得代碼與散文不符,使整個回答有點荒謬。 -1。 –