Neo4j Community Edition - The world's leading Graph Database with Cypher query language and ACID transactions.
—
Database schema operations providing index creation, constraint management, and schema inspection capabilities for query optimization and data integrity enforcement with full lifecycle management.
Main interface for managing graph database schema including indexes and constraints.
/**
* Interface for managing graph database schema
*/
public interface Schema {
/**
* Create an index builder for the specified label
* @param label Label to create index for
* @return Index creator for building the index
*/
IndexCreator indexFor(Label label);
/**
* Create a constraint builder for the specified label
* @param label Label to create constraint for
* @return Constraint creator for building the constraint
*/
ConstraintCreator constraintFor(Label label);
/**
* Create a constraint builder for the specified relationship type
* @param relationshipType Relationship type to create constraint for
* @return Constraint creator for building the constraint
*/
ConstraintCreator constraintFor(RelationshipType relationshipType);
/**
* Get all indexes in the database
* @return Iterable of all index definitions
*/
Iterable<IndexDefinition> getIndexes();
/**
* Get indexes for a specific label
* @param label Label to get indexes for
* @return Iterable of index definitions for the label
*/
Iterable<IndexDefinition> getIndexes(Label label);
/**
* Get all constraints in the database
* @return Iterable of all constraint definitions
*/
Iterable<ConstraintDefinition> getConstraints();
/**
* Get constraints for a specific label
* @param label Label to get constraints for
* @return Iterable of constraint definitions for the label
*/
Iterable<ConstraintDefinition> getConstraints(Label label);
/**
* Get constraints for a specific relationship type
* @param relationshipType Relationship type to get constraints for
* @return Iterable of constraint definitions for the relationship type
*/
Iterable<ConstraintDefinition> getConstraints(RelationshipType relationshipType);
/**
* Get the state of a specific index
* @param index Index to check state for
* @return Current index state
*/
IndexState getIndexState(IndexDefinition index);
/**
* Get the population progress of an index
* @param index Index to check progress for
* @return Population progress percentage (0.0 to 1.0)
*/
double getIndexPopulationProgress(IndexDefinition index);
/**
* Get the failure message for a failed index
* @param index Index to get failure message for
* @return Failure message, or null if index is not failed
*/
String getIndexFailure(IndexDefinition index);
/**
* Wait for an index to be online
* @param index Index to wait for
* @param duration Maximum time to wait
* @param unit Time unit for duration
* @throws IllegalStateException if index fails to come online
*/
void awaitIndexOnline(IndexDefinition index, long duration, TimeUnit unit);
/**
* Wait for all indexes to be online
* @param duration Maximum time to wait
* @param unit Time unit for duration
*/
void awaitIndexesOnline(long duration, TimeUnit unit);
}Usage Examples:
import org.neo4j.graphdb.schema.Schema;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.graphdb.schema.ConstraintDefinition;
import java.util.concurrent.TimeUnit;
try (Transaction tx = graphDb.beginTx()) {
Schema schema = tx.schema();
// List all existing indexes
System.out.println("Existing indexes:");
for (IndexDefinition index : schema.getIndexes()) {
System.out.println(" " + index.getLabel() + ": " + index.getPropertyKeys());
System.out.println(" State: " + schema.getIndexState(index));
}
// List all existing constraints
System.out.println("Existing constraints:");
for (ConstraintDefinition constraint : schema.getConstraints()) {
System.out.println(" " + constraint.getLabel() + ": " +
constraint.getPropertyKeys() + " (" +
constraint.getConstraintType() + ")");
}
tx.commit();
}Builder interface for creating database indexes with property specification and configuration options.
/**
* Builder for creating database indexes
*/
public interface IndexCreator {
/**
* Specify a property key for the index
* @param propertyKey Property key to index
* @return This creator for method chaining
*/
IndexCreator on(String propertyKey);
/**
* Set the index type (if supported)
* @param indexType Type of index to create
* @return This creator for method chaining
*/
IndexCreator withIndexType(IndexType indexType);
/**
* Set the index configuration
* @param indexConfig Configuration map for the index
* @return This creator for method chaining
*/
IndexCreator withIndexConfiguration(Map<IndexSetting, Object> indexConfig);
/**
* Set a name for the index
* @param indexName Name for the index
* @return This creator for method chaining
*/
IndexCreator withName(String indexName);
/**
* Create the index
* @return Index definition for the created index
*/
IndexDefinition create();
}Usage Examples:
try (Transaction tx = graphDb.beginTx()) {
Schema schema = tx.schema();
// Create a simple property index
IndexDefinition nameIndex = schema.indexFor(label("Person"))
.on("name")
.create();
// Create a composite index on multiple properties
IndexDefinition personIndex = schema.indexFor(label("Person"))
.on("firstName")
.on("lastName")
.withName("person_full_name_index")
.create();
// Create a text index for full-text search
IndexDefinition textIndex = schema.indexFor(label("Article"))
.on("title")
.on("content")
.withIndexType(IndexType.FULLTEXT)
.withName("article_fulltext_index")
.create();
tx.commit();
}
// Wait for indexes to come online
try (Transaction tx = graphDb.beginTx()) {
Schema schema = tx.schema();
// Wait for a specific index
schema.awaitIndexOnline(nameIndex, 10, TimeUnit.SECONDS);
// Wait for all indexes
schema.awaitIndexesOnline(30, TimeUnit.SECONDS);
tx.commit();
}Builder interface for creating database constraints with validation rules and property specifications.
/**
* Builder for creating database constraints
*/
public interface ConstraintCreator {
/**
* Assert that a property must be unique
* @param propertyKey Property key that must be unique
* @return This creator for method chaining
*/
ConstraintCreator assertPropertyIsUnique(String propertyKey);
/**
* Assert that a property must exist (not null)
* @param propertyKey Property key that must exist
* @return This creator for method chaining
*/
ConstraintCreator assertPropertyExists(String propertyKey);
/**
* Assert that a property must be a specific type
* @param propertyKey Property key to type-check
* @param propertyType Expected property type
* @return This creator for method chaining
*/
ConstraintCreator assertPropertyIsOfType(String propertyKey, PropertyType propertyType);
/**
* Set a name for the constraint
* @param constraintName Name for the constraint
* @return This creator for method chaining
*/
ConstraintCreator withName(String constraintName);
/**
* Create the constraint
* @return Constraint definition for the created constraint
*/
ConstraintDefinition create();
}Usage Examples:
try (Transaction tx = graphDb.beginTx()) {
Schema schema = tx.schema();
// Create uniqueness constraint
ConstraintDefinition emailConstraint = schema.constraintFor(label("User"))
.assertPropertyIsUnique("email")
.withName("unique_user_email")
.create();
// Create existence constraint
ConstraintDefinition nameConstraint = schema.constraintFor(label("Person"))
.assertPropertyExists("name")
.withName("person_must_have_name")
.create();
// Create composite uniqueness constraint
ConstraintDefinition userConstraint = schema.constraintFor(label("User"))
.assertPropertyIsUnique("username")
.assertPropertyIsUnique("domain")
.withName("unique_user_in_domain")
.create();
tx.commit();
}Interface representing a database index with metadata and property information.
/**
* Definition of a database index
*/
public interface IndexDefinition {
/**
* Get the label this index is associated with
* @return Label for the index
*/
Label getLabel();
/**
* Get the property keys included in this index
* @return Iterable of property keys
*/
Iterable<String> getPropertyKeys();
/**
* Get the name of this index
* @return Index name, or null if not named
*/
String getName();
/**
* Get the type of this index
* @return Index type
*/
IndexType getIndexType();
/**
* Check if this is a composite index (multiple properties)
* @return true if composite index, false otherwise
*/
boolean isCompositeIndex();
/**
* Check if this is a multi-token index (multiple labels)
* @return true if multi-token index, false otherwise
*/
boolean isMultiTokenIndex();
/**
* Drop this index
*/
void drop();
}Interface representing a database constraint with validation rules and metadata.
/**
* Definition of a database constraint
*/
public interface ConstraintDefinition {
/**
* Get the label this constraint is associated with
* @return Label for the constraint
*/
Label getLabel();
/**
* Get the property keys included in this constraint
* @return Iterable of property keys
*/
Iterable<String> getPropertyKeys();
/**
* Get the name of this constraint
* @return Constraint name, or null if not named
*/
String getName();
/**
* Get the type of this constraint
* @return Constraint type (UNIQUENESS, NODE_PROPERTY_EXISTENCE, etc.)
*/
ConstraintType getConstraintType();
/**
* Check if this is a composite constraint (multiple properties)
* @return true if composite constraint, false otherwise
*/
boolean isCompositeConstraint();
/**
* Drop this constraint
*/
void drop();
}Supporting enums and types for schema management operations.
/**
* State of an index
*/
public enum IndexState {
/** Index is being created */
POPULATING,
/** Index is online and available for use */
ONLINE,
/** Index creation failed */
FAILED
}
/**
* Types of indexes supported
*/
public enum IndexType {
/** Standard B-tree index for exact lookups */
BTREE,
/** Full-text search index */
FULLTEXT,
/** Text index for string operations */
TEXT,
/** Range index for numeric and temporal data */
RANGE,
/** Point index for spatial data */
POINT
}
/**
* Types of constraints supported
*/
public enum ConstraintType {
/** Uniqueness constraint */
UNIQUENESS,
/** Node property existence constraint */
NODE_PROPERTY_EXISTENCE,
/** Relationship property existence constraint */
RELATIONSHIP_PROPERTY_EXISTENCE,
/** Node key constraint (unique + existence) */
NODE_KEY,
/** Property type constraint */
PROPERTY_TYPE
}
/**
* Property types for type constraints
*/
public enum PropertyType {
BOOLEAN,
BYTE,
SHORT,
INT,
LONG,
FLOAT,
DOUBLE,
STRING,
BYTE_ARRAY,
SHORT_ARRAY,
INT_ARRAY,
LONG_ARRAY,
FLOAT_ARRAY,
DOUBLE_ARRAY,
STRING_ARRAY,
BOOLEAN_ARRAY,
POINT,
DATE,
TIME,
LOCAL_TIME,
DATETIME,
LOCAL_DATETIME,
DURATION
}Advanced Usage Examples:
// Complete schema management workflow
try (Transaction tx = graphDb.beginTx()) {
Schema schema = tx.schema();
// Create indexes for performance
IndexDefinition userEmailIndex = schema.indexFor(label("User"))
.on("email")
.withName("user_email_index")
.create();
IndexDefinition productNameIndex = schema.indexFor(label("Product"))
.on("name")
.withIndexType(IndexType.TEXT)
.create();
// Create constraints for data integrity
ConstraintDefinition userEmailUnique = schema.constraintFor(label("User"))
.assertPropertyIsUnique("email")
.create();
ConstraintDefinition userNameExists = schema.constraintFor(label("User"))
.assertPropertyExists("name")
.create();
tx.commit();
}
// Monitor index creation progress
try (Transaction tx = graphDb.beginTx()) {
Schema schema = tx.schema();
for (IndexDefinition index : schema.getIndexes()) {
IndexState state = schema.getIndexState(index);
System.out.println("Index " + index.getName() + ": " + state);
if (state == IndexState.POPULATING) {
double progress = schema.getIndexPopulationProgress(index);
System.out.println(" Progress: " + (progress * 100) + "%");
} else if (state == IndexState.FAILED) {
String failure = schema.getIndexFailure(index);
System.out.println(" Failure: " + failure);
}
}
tx.commit();
}
// Drop indexes and constraints
try (Transaction tx = graphDb.beginTx()) {
Schema schema = tx.schema();
// Find and drop specific index
for (IndexDefinition index : schema.getIndexes(label("User"))) {
if (index.getPropertyKeys().iterator().next().equals("email")) {
index.drop();
System.out.println("Dropped index on User.email");
break;
}
}
// Find and drop specific constraint
for (ConstraintDefinition constraint : schema.getConstraints(label("User"))) {
if (constraint.getConstraintType() == ConstraintType.UNIQUENESS) {
constraint.drop();
System.out.println("Dropped uniqueness constraint");
break;
}
}
tx.commit();
}Install with Tessl CLI
npx tessl i tessl/maven-org-neo4j--neo4j