我试图通过本机api为neo4j图创建索引,并且我正在使用org.neo4j:neo4j:3.4.4
,这是我的代码:
public static Boolean createIndex(GraphDatabaseService graphDb, Label label, String property){
Boolean ret = true;
try {
// END SNIPPET: startDb
{
// START SNIPPET: createIndex
IndexDefinition indexDefinition;
try (Transaction tx = graphDb.beginTx()) {
Schema schema = graphDb.schema();
indexDefinition = schema.indexFor(label)
.on(property)
.create();
tx.success();
tx.close();
}
// END SNIPPET: createIndex
// START SNIPPET: wait
try (Transaction tx = graphDb.beginTx()) {
Schema schema = graphDb.schema();
schema.awaitIndexOnline(indexDefinition, 1000, TimeUnit.SECONDS);
tx.success();
tx.close();
}
// END SNIPPET: wait
// START SNIPPET: progress
try (Transaction tx = graphDb.beginTx()) {
Schema schema = graphDb.schema();
System.out.println(String.format("Percent complete: %1.0f%%",
schema.getIndexPopulationProgress(indexDefinition).getCompletedPercentage()));
tx.success();
tx.close();
}
// END SNIPPET: progress
}
} catch (ConstraintViolationException e) {
System.out.println(e);
ret = true;
}
catch (Exception e){
System.out.println(e);
ret = false;
};
return ret;
}
此代码完全来自official example,我可以看到代码中记录的日志Percent complete: 100%
。但是,当我使用neo4j浏览器打开图形并查询:schema
时,它表示索引仍在填充,这意味着schema.getIndexPopulationProgress(indexDefinition)
方法实际上不起作用。
我想问的是,有什么解决方案可以查询索引的真实状态,或者有什么想法可以同步创建索引,而不是异步创建官方方法。
感谢您的时间!