Question

I'm actually building onto the question: How to check if a schema index already exists for a node's property in neo4j?

I can however not find how to do the actual if clause. For example:

label = DynamicLabel.label("Label");
Iterable<IndexDefinition> indexes = schema.getIndexes(label);
for(IndexDefinition index : indexes) {
    if(index.equals(schema.indexFor(label).on("id"))) {
        // index exists on property "id" on label "Label"!
    }
}

This however isn't working!

Was it helpful?

Solution

The following snippet should help:

label = DynamicLabel.label("Label");
Iterable<IndexDefinition> indexes = schema.getIndexes(label);
for(IndexDefinition index : indexes) {
    for (String key: index.getPropertyKeys()) {
         if (key.equals("id")) {
              return true; // index for label and property exists
         }
    }
}
return false; // no matching schema index

Please note that in Neo4j 2.0/2.1 each index has only one single property. Multi property indexes are not yet supported - however the API is already designed for that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top