简体   繁体   中英

Neo4j index not finding Node

I tried to follow the documentation and I ended up with this piece of code for Neo4j 1.8:

graphDB = new GraphDatabaseFactory()
            .newEmbeddedDatabaseBuilder( BASE_FOLDER + NEO4J_PATH )
            .newGraphDatabase();

registerShutdownHook();

//Check if there are any indexes
System.out.println(Arrays.toString(graphDB.index().nodeIndexNames()));
Index<Node> testIndex = graphDB.index().forNodes("test");

Transaction tx = graphDB.beginTx();
try {
    String nameKey = "name";
    String nameValue = "Gevorg";

    //The following 3 lines will be commented out 
    //when I run the program the second time
    Node me = graphDB.createNode();
    me.setProperty(nameKey, nameValue);
    testIndex.add(me, nameKey, nameValue);

    Node meAgain = testIndex.get(nameKey, nameValue).getSingle();
    System.out.println(meAgain.getProperty(nameKey));

} finally {
    tx.finish();
}

This prints the following as expected:

[] //There is no index at the very beginning
Gevorg

After the program terminates, I commented the creation of the node/index and I run the program again to hit a NullPointerException (meAgain is null). The index is retrieved correctly since the program prints [test] first but then Node meAgain = testIndex.get(nameKey, nameValue).getSingle(); fails to retrieve the node. I tried both with and without using the Transaction. What am I doing wrong??

You need to mark your Tx as successful, before calling tx.finish

tx.success()

HTH

/peter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM