I was trying to create indices for neo4j graph via native api, and I am using org.neo4j:neo4j:3.4.4
, here is my code:
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;
}
This code is completely from official example, and I can see the log Percent complete: 100%
as logged in the code. However, when I open the graph with neo4j browser and query :schema
, it says the indices are still populating, which means the method schema.getIndexPopulationProgress(indexDefinition)
does not actually work.
I want to ask is there any solution to query the real status of index or any idea on creating index synchronously instead of the official method which is asynchronously.
Thanks for your time!