0
我試圖按照文檔和我結束了這段代碼爲Neo4j的1.8:Neo4j的索引沒有找到節點
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();
}
這將打印以下預期:
[] //There is no index at the very beginning
Gevorg
後,程序終止,我評論了節點/索引的創建,然後再次運行該程序以觸發NullPointerException(meAgain爲null)。由於程序首先打印[test]
,但是Node meAgain = testIndex.get(nameKey, nameValue).getSingle();
未能檢索到節點,因此索引被正確檢索。我嘗試使用和不使用交易。我究竟做錯了什麼??