克服Cypher Lucene查询中的NullPointerException

时间:2014-03-25 14:41:42

标签: java lucene neo4j cypher

我试图在neo4j 2.0.1数据库中使用lucene索引对节点的name属性中的特定术语进行模糊查询。执行这些查询时,尽管lucene语法看起来没问题,但它会因神秘的NullPointerException而失败。可能是我在lucene语法中犯了一些直截了当的错误,但我怎么能发现它是什么呢?

        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);

当使用“track”参数运行此方法时,输出为:

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)

1 个答案:

答案 0 :(得分:2)

您的Lucene语法是正确的。如果您将查询作为参数提供,那么您必须在没有&#34;&#39;&#34;&#34;但是人物。

所以使用

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

而不是

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

您还可以提供如下模糊因子:

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