1

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!

Victor Liu
  • 53
  • 3
  • Double-check that the documentation you're working off of matches the version of Neo4j you're running. Creating indices changed dramatically in syntax going from 3 to 4. – Adrian Keister Nov 02 '20 at 19:00
  • Thanks for your kind help, the APIs and neo4j I used is 3.4.4, and the document I referred to is for 3.5, I can not find document for 3.4.4, but 3.4.4 is quite close to 3.5, so I think there should not be much difference – Victor Liu Nov 04 '20 at 08:07

0 Answers0