2014-03-25 46 views
0

我試圖在neo4j 2.0.1數據庫中使用lucene索引對節點的name屬性中的特定術語進行模糊查詢。在執行這些查詢時,雖然lucene語法看起來不錯,但它會失敗並帶有一個神祕的NullPointerException。這可能是因爲我在lucene語法中犯了一些直接的錯誤,但我怎麼能發現那是什麼?在Cypher Lucene查詢中克服NullPointerException

 
     String query = "start n=node:node_auto_index({searchCriteria}) "+ 
       "match (n:Provenance) " + 
       "return n " + 
        "order by n.created desc " + 
        "limit 10"; 

     String expr = term; 

     if(term.contains(" ") || term.contains("\t")) 
      expr = "\"" + term + "\""; 

     expr = "'name:" + expr + "~'"; 

     log.info("search expression=" + expr); 

     Map<String,Object>params = new HashMap(); 
     params.put("searchCriteria", expr); 

當此方法與「跟蹤」的參數運行,輸出是這樣的:

 
INFO: search expression='name:track~' 
java.lang.NullPointerException 
    at org.apache.lucene.util.SimpleStringInterner.intern(SimpleStringInterner.java:54) 
    at org.apache.lucene.util.StringHelper.intern(StringHelper.java:39) 
    at org.apache.lucene.index.Term.(Term.java:38) 
    at org.apache.lucene.queryParser.QueryParser.getFieldQuery(QueryParser.java:643) 
    at org.apache.lucene.queryParser.QueryParser.Term(QueryParser.java:1436) 
    at org.apache.lucene.queryParser.QueryParser.Clause(QueryParser.java:1319) 
    at org.apache.lucene.queryParser.QueryParser.Query(QueryParser.java:1275) 
    at org.apache.lucene.queryParser.QueryParser.TopLevelQuery(QueryParser.java:1234) 
    at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:206) 
    at org.neo4j.index.impl.lucene.IndexType.query(IndexType.java:303) 
    at org.neo4j.index.impl.lucene.LuceneIndex.query(LuceneIndex.java:251) 
    at org.neo4j.index.impl.lucene.LuceneIndex.query(LuceneIndex.java:263) 
    at org.neo4j.kernel.impl.coreapi.AbstractAutoIndexerImpl$ReadOnlyIndexToIndexAdapter.query(AbstractAutoIndexerImpl.java:249) 
    at org.neo4j.cypher.internal.spi.v2_0.TransactionBoundExecutionContext$NodeOperations.indexQuery(TransactionBoundExecutionContext.scala:166) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.DelegatingOperations.indexQuery(DelegatingQueryContext.scala:113) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext$ExceptionTranslatingOperations.org$neo4j$cypher$internal$compiler$v2_0$spi$ExceptionTranslatingQueryContext$ExceptionTranslatingOperations$$super$indexQuery(ExceptionTranslatingQueryContext.scala:142) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext$ExceptionTranslatingOperations$$anonfun$indexQuery$1.apply(ExceptionTranslatingQueryContext.scala:142) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext$ExceptionTranslatingOperations$$anonfun$indexQuery$1.apply(ExceptionTranslatingQueryContext.scala:142) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext.org$neo4j$cypher$internal$compiler$v2_0$spi$ExceptionTranslatingQueryContext$$translateException(ExceptionTranslatingQueryContext.scala:149) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext$ExceptionTranslatingOperations.indexQuery(ExceptionTranslatingQueryContext.scala:142) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.DelegatingOperations.indexQuery(DelegatingQueryContext.scala:113) 
    at org.neo4j.cypher.internal.compiler.v2_0.executionplan.builders.EntityProducerFactory$$anonfun$2$$anonfun$applyOrElse$2.apply(EntityProducerFactory.scala:66) 
    at org.neo4j.cypher.internal.compiler.v2_0.executionplan.builders.EntityProducerFactory$$anonfun$2$$anonfun$applyOrElse$2.apply(EntityProducerFactory.scala:64) 
    at org.neo4j.cypher.internal.compiler.v2_0.executionplan.builders.EntityProducerFactory$$anon$1.apply(EntityProducerFactory.scala:35) 
    at org.neo4j.cypher.internal.compiler.v2_0.executionplan.builders.EntityProducerFactory$$anon$1.apply(EntityProducerFactory.scala:34) 
    at org.neo4j.cypher.internal.compiler.v2_0.pipes.StartPipe$$anonfun$internalCreateResults$1.apply(StartPipe.scala:34) 
    at org.neo4j.cypher.internal.compiler.v2_0.pipes.StartPipe$$anonfun$internalCreateResults$1.apply(StartPipe.scala:33) 
    at scala.collection.Iterator$$anon$13.hasNext(Iterator.scala:371) 
    at scala.collection.Iterator$$anon$14.hasNext(Iterator.scala:388) 
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:327) 
    at scala.collection.Iterator$class.isEmpty(Iterator.scala:256) 
    at scala.collection.AbstractIterator.isEmpty(Iterator.scala:1157) 
    at org.neo4j.cypher.internal.compiler.v2_0.pipes.TopPipe.internalCreateResults(TopPipe.scala:42) 
    at org.neo4j.cypher.internal.compiler.v2_0.pipes.PipeWithSource.createResults(Pipe.scala:71) 
    at org.neo4j.cypher.internal.compiler.v2_0.pipes.PipeWithSource.createResults(Pipe.scala:68) 
    at org.neo4j.cypher.internal.compiler.v2_0.executionplan.ExecutionPlanBuilder.org$neo4j$cypher$internal$compiler$v2_0$executionplan$ExecutionPlanBuilder$$prepareStateAndResult(ExecutionPlanBuilder.scala:149) 
    at org.neo4j.cypher.internal.compiler.v2_0.executionplan.ExecutionPlanBuilder$$anonfun$2.apply(ExecutionPlanBuilder.scala:126) 
    at org.neo4j.cypher.internal.compiler.v2_0.executionplan.ExecutionPlanBuilder$$anonfun$2.apply(ExecutionPlanBuilder.scala:125) 
    at org.neo4j.cypher.internal.compiler.v2_0.executionplan.ExecutionPlanBuilder$$anon$6.execute(ExecutionPlanBuilder.scala:50) 
    at org.neo4j.cypher.internal.ExecutionPlanWrapperForV2_0.execute(CypherCompiler.scala:93) 
    at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:61) 
    at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:65) 
    at org.neo4j.cypher.javacompat.ExecutionEngine.execute(ExecutionEngine.java:78) 
    at org.mitre.provenance.db.neo4j.Neo4JStorage.execute(Neo4JStorage.java:1216) 

回答

2

你Lucene的語法是正確的。如果您將查詢作爲參數提供,則必須不使用「」「字符。

所以使用

expr = "name:" + expr + "~"; 

,而不是

expr = "'name:" + expr + "~'"; 

你也可以提供一個模糊的因素如下:

expr = "name:" + expr + "~0.8"; 
+0

那麼什麼可以解釋NullPointerException - 如果Lucene的查詢語法正確? – FrobberOfBits

+0

我認爲在異常消息方面肯定有改進的餘地,但重點是lucene查詢無法處理。當您將查詢作爲參數傳遞時,我猜Cypher已經將查詢放在查詢的附近,所以最後會出現雙引號,這是錯誤的。 – tstorms