建立索引后,用Java的嵌入式开发模式的findNodes()函数报错
发布于 6 年前 作者 charlieguo 2317 次浏览 来自 问答

我在neo4j数据库中插入很多节点后,在cypher-shell中为节点的chinese_name属性建立了索引,不过并非所有节点都有chinese_name这个属性。 之后在用Java进行嵌入式开发时,执行findNodes()函数时抛出异常,看stacktrace应该是读取索引时出现的问题。后来把索引删除后可以正常执行了。一下是我写的一个测试代码和异常结果。请问这是什么问题呢? 代码:

private static final File DB_PATH = new File("/home/charlie/Projects/databases/neo4j_demo.db");
private static GraphDatabaseService graphDb;
public static void testGetNode() {
    graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
    registerShutdownHook(graphDb);
    try (Transaction tx = graphDb.beginTx()){
        ResourceIterator<Node> iter = graphDb.findNodes(KgEnum.KgLabel.PERSON, "chinese_name", "刘德华");
        if (iter.hasNext()) {
            System.out.println("got one or more nodes");
        } else {
            System.out.println("no node got");
        }
        tx.success();
    } finally {
        graphDb.shutdown();
    }
}

异常信息:

Exception in thread "main" java.lang.UnsupportedOperationException
at org.neo4j.kernel.impl.api.index.AbstractSwallowingIndexProxy.newReader(AbstractSwallowingIndexProxy.java:110)
at org.neo4j.kernel.impl.api.index.FlippableIndexProxy.newReader(FlippableIndexProxy.java:308)
at org.neo4j.kernel.impl.api.index.AbstractDelegatingIndexProxy.newReader(AbstractDelegatingIndexProxy.java:116)
at org.neo4j.kernel.impl.api.IndexReaderFactory$Caching.newUnCachedReader(IndexReaderFactory.java:70)
at org.neo4j.kernel.impl.api.IndexReaderFactory$Caching.newReader(IndexReaderFactory.java:60)
at org.neo4j.kernel.impl.storageengine.impl.recordstorage.StoreStatement.getIndexReader(StoreStatement.java:256)
at org.neo4j.kernel.impl.newapi.AllStoreHolder.indexReader(AllStoreHolder.java:255)
at org.neo4j.kernel.impl.newapi.Read.nodeIndexSeek(Read.java:113)
at org.neo4j.kernel.impl.factory.GraphDatabaseFacade.nodesByLabelAndProperty(GraphDatabaseFacade.java:718)
at org.neo4j.kernel.impl.factory.GraphDatabaseFacade.findNodes(GraphDatabaseFacade.java:593)
at Test.testGetNode(Test.java:65)
at Test.main(Test.java:24)
4 回复

会不会是中文 加索引 就出问题啊,你试试 把索引加在无中文的属性上报错吗?

@pangguoming 试了下,在无中文的属性上加索引,也会出现同样的问题。

估计是bug,你数据库什么版本,maven 下载的neo4japi 什么版本?

@pangguoming 数据库是3.4.1版本,用cypher-shell连接登录数据库后提示语为:Connected to Neo4j 3.4.1 at bolt://localhost:7687 as user neo4j. 在maven中的配置为 <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j</artifactId> <version>3.4.1</version> </dependency> 这样的版本应该是匹配的吧?

回到顶部