0
# Schema Management
1
2
Database schema operations providing index creation, constraint management, and schema inspection capabilities for query optimization and data integrity enforcement with full lifecycle management.
3
4
## Capabilities
5
6
### Schema Interface
7
8
Main interface for managing graph database schema including indexes and constraints.
9
10
```java { .api }
11
/**
12
* Interface for managing graph database schema
13
*/
14
public interface Schema {
15
16
/**
17
* Create an index builder for the specified label
18
* @param label Label to create index for
19
* @return Index creator for building the index
20
*/
21
IndexCreator indexFor(Label label);
22
23
/**
24
* Create a constraint builder for the specified label
25
* @param label Label to create constraint for
26
* @return Constraint creator for building the constraint
27
*/
28
ConstraintCreator constraintFor(Label label);
29
30
/**
31
* Create a constraint builder for the specified relationship type
32
* @param relationshipType Relationship type to create constraint for
33
* @return Constraint creator for building the constraint
34
*/
35
ConstraintCreator constraintFor(RelationshipType relationshipType);
36
37
/**
38
* Get all indexes in the database
39
* @return Iterable of all index definitions
40
*/
41
Iterable<IndexDefinition> getIndexes();
42
43
/**
44
* Get indexes for a specific label
45
* @param label Label to get indexes for
46
* @return Iterable of index definitions for the label
47
*/
48
Iterable<IndexDefinition> getIndexes(Label label);
49
50
/**
51
* Get all constraints in the database
52
* @return Iterable of all constraint definitions
53
*/
54
Iterable<ConstraintDefinition> getConstraints();
55
56
/**
57
* Get constraints for a specific label
58
* @param label Label to get constraints for
59
* @return Iterable of constraint definitions for the label
60
*/
61
Iterable<ConstraintDefinition> getConstraints(Label label);
62
63
/**
64
* Get constraints for a specific relationship type
65
* @param relationshipType Relationship type to get constraints for
66
* @return Iterable of constraint definitions for the relationship type
67
*/
68
Iterable<ConstraintDefinition> getConstraints(RelationshipType relationshipType);
69
70
/**
71
* Get the state of a specific index
72
* @param index Index to check state for
73
* @return Current index state
74
*/
75
IndexState getIndexState(IndexDefinition index);
76
77
/**
78
* Get the population progress of an index
79
* @param index Index to check progress for
80
* @return Population progress percentage (0.0 to 1.0)
81
*/
82
double getIndexPopulationProgress(IndexDefinition index);
83
84
/**
85
* Get the failure message for a failed index
86
* @param index Index to get failure message for
87
* @return Failure message, or null if index is not failed
88
*/
89
String getIndexFailure(IndexDefinition index);
90
91
/**
92
* Wait for an index to be online
93
* @param index Index to wait for
94
* @param duration Maximum time to wait
95
* @param unit Time unit for duration
96
* @throws IllegalStateException if index fails to come online
97
*/
98
void awaitIndexOnline(IndexDefinition index, long duration, TimeUnit unit);
99
100
/**
101
* Wait for all indexes to be online
102
* @param duration Maximum time to wait
103
* @param unit Time unit for duration
104
*/
105
void awaitIndexesOnline(long duration, TimeUnit unit);
106
}
107
```
108
109
**Usage Examples:**
110
111
```java
112
import org.neo4j.graphdb.schema.Schema;
113
import org.neo4j.graphdb.schema.IndexDefinition;
114
import org.neo4j.graphdb.schema.ConstraintDefinition;
115
import java.util.concurrent.TimeUnit;
116
117
try (Transaction tx = graphDb.beginTx()) {
118
Schema schema = tx.schema();
119
120
// List all existing indexes
121
System.out.println("Existing indexes:");
122
for (IndexDefinition index : schema.getIndexes()) {
123
System.out.println(" " + index.getLabel() + ": " + index.getPropertyKeys());
124
System.out.println(" State: " + schema.getIndexState(index));
125
}
126
127
// List all existing constraints
128
System.out.println("Existing constraints:");
129
for (ConstraintDefinition constraint : schema.getConstraints()) {
130
System.out.println(" " + constraint.getLabel() + ": " +
131
constraint.getPropertyKeys() + " (" +
132
constraint.getConstraintType() + ")");
133
}
134
135
tx.commit();
136
}
137
```
138
139
### Index Creator
140
141
Builder interface for creating database indexes with property specification and configuration options.
142
143
```java { .api }
144
/**
145
* Builder for creating database indexes
146
*/
147
public interface IndexCreator {
148
149
/**
150
* Specify a property key for the index
151
* @param propertyKey Property key to index
152
* @return This creator for method chaining
153
*/
154
IndexCreator on(String propertyKey);
155
156
/**
157
* Set the index type (if supported)
158
* @param indexType Type of index to create
159
* @return This creator for method chaining
160
*/
161
IndexCreator withIndexType(IndexType indexType);
162
163
/**
164
* Set the index configuration
165
* @param indexConfig Configuration map for the index
166
* @return This creator for method chaining
167
*/
168
IndexCreator withIndexConfiguration(Map<IndexSetting, Object> indexConfig);
169
170
/**
171
* Set a name for the index
172
* @param indexName Name for the index
173
* @return This creator for method chaining
174
*/
175
IndexCreator withName(String indexName);
176
177
/**
178
* Create the index
179
* @return Index definition for the created index
180
*/
181
IndexDefinition create();
182
}
183
```
184
185
**Usage Examples:**
186
187
```java
188
try (Transaction tx = graphDb.beginTx()) {
189
Schema schema = tx.schema();
190
191
// Create a simple property index
192
IndexDefinition nameIndex = schema.indexFor(label("Person"))
193
.on("name")
194
.create();
195
196
// Create a composite index on multiple properties
197
IndexDefinition personIndex = schema.indexFor(label("Person"))
198
.on("firstName")
199
.on("lastName")
200
.withName("person_full_name_index")
201
.create();
202
203
// Create a text index for full-text search
204
IndexDefinition textIndex = schema.indexFor(label("Article"))
205
.on("title")
206
.on("content")
207
.withIndexType(IndexType.FULLTEXT)
208
.withName("article_fulltext_index")
209
.create();
210
211
tx.commit();
212
}
213
214
// Wait for indexes to come online
215
try (Transaction tx = graphDb.beginTx()) {
216
Schema schema = tx.schema();
217
218
// Wait for a specific index
219
schema.awaitIndexOnline(nameIndex, 10, TimeUnit.SECONDS);
220
221
// Wait for all indexes
222
schema.awaitIndexesOnline(30, TimeUnit.SECONDS);
223
224
tx.commit();
225
}
226
```
227
228
### Constraint Creator
229
230
Builder interface for creating database constraints with validation rules and property specifications.
231
232
```java { .api }
233
/**
234
* Builder for creating database constraints
235
*/
236
public interface ConstraintCreator {
237
238
/**
239
* Assert that a property must be unique
240
* @param propertyKey Property key that must be unique
241
* @return This creator for method chaining
242
*/
243
ConstraintCreator assertPropertyIsUnique(String propertyKey);
244
245
/**
246
* Assert that a property must exist (not null)
247
* @param propertyKey Property key that must exist
248
* @return This creator for method chaining
249
*/
250
ConstraintCreator assertPropertyExists(String propertyKey);
251
252
/**
253
* Assert that a property must be a specific type
254
* @param propertyKey Property key to type-check
255
* @param propertyType Expected property type
256
* @return This creator for method chaining
257
*/
258
ConstraintCreator assertPropertyIsOfType(String propertyKey, PropertyType propertyType);
259
260
/**
261
* Set a name for the constraint
262
* @param constraintName Name for the constraint
263
* @return This creator for method chaining
264
*/
265
ConstraintCreator withName(String constraintName);
266
267
/**
268
* Create the constraint
269
* @return Constraint definition for the created constraint
270
*/
271
ConstraintDefinition create();
272
}
273
```
274
275
**Usage Examples:**
276
277
```java
278
try (Transaction tx = graphDb.beginTx()) {
279
Schema schema = tx.schema();
280
281
// Create uniqueness constraint
282
ConstraintDefinition emailConstraint = schema.constraintFor(label("User"))
283
.assertPropertyIsUnique("email")
284
.withName("unique_user_email")
285
.create();
286
287
// Create existence constraint
288
ConstraintDefinition nameConstraint = schema.constraintFor(label("Person"))
289
.assertPropertyExists("name")
290
.withName("person_must_have_name")
291
.create();
292
293
// Create composite uniqueness constraint
294
ConstraintDefinition userConstraint = schema.constraintFor(label("User"))
295
.assertPropertyIsUnique("username")
296
.assertPropertyIsUnique("domain")
297
.withName("unique_user_in_domain")
298
.create();
299
300
tx.commit();
301
}
302
```
303
304
### Index Definition
305
306
Interface representing a database index with metadata and property information.
307
308
```java { .api }
309
/**
310
* Definition of a database index
311
*/
312
public interface IndexDefinition {
313
314
/**
315
* Get the label this index is associated with
316
* @return Label for the index
317
*/
318
Label getLabel();
319
320
/**
321
* Get the property keys included in this index
322
* @return Iterable of property keys
323
*/
324
Iterable<String> getPropertyKeys();
325
326
/**
327
* Get the name of this index
328
* @return Index name, or null if not named
329
*/
330
String getName();
331
332
/**
333
* Get the type of this index
334
* @return Index type
335
*/
336
IndexType getIndexType();
337
338
/**
339
* Check if this is a composite index (multiple properties)
340
* @return true if composite index, false otherwise
341
*/
342
boolean isCompositeIndex();
343
344
/**
345
* Check if this is a multi-token index (multiple labels)
346
* @return true if multi-token index, false otherwise
347
*/
348
boolean isMultiTokenIndex();
349
350
/**
351
* Drop this index
352
*/
353
void drop();
354
}
355
```
356
357
### Constraint Definition
358
359
Interface representing a database constraint with validation rules and metadata.
360
361
```java { .api }
362
/**
363
* Definition of a database constraint
364
*/
365
public interface ConstraintDefinition {
366
367
/**
368
* Get the label this constraint is associated with
369
* @return Label for the constraint
370
*/
371
Label getLabel();
372
373
/**
374
* Get the property keys included in this constraint
375
* @return Iterable of property keys
376
*/
377
Iterable<String> getPropertyKeys();
378
379
/**
380
* Get the name of this constraint
381
* @return Constraint name, or null if not named
382
*/
383
String getName();
384
385
/**
386
* Get the type of this constraint
387
* @return Constraint type (UNIQUENESS, NODE_PROPERTY_EXISTENCE, etc.)
388
*/
389
ConstraintType getConstraintType();
390
391
/**
392
* Check if this is a composite constraint (multiple properties)
393
* @return true if composite constraint, false otherwise
394
*/
395
boolean isCompositeConstraint();
396
397
/**
398
* Drop this constraint
399
*/
400
void drop();
401
}
402
```
403
404
### Enums and Types
405
406
Supporting enums and types for schema management operations.
407
408
```java { .api }
409
/**
410
* State of an index
411
*/
412
public enum IndexState {
413
/** Index is being created */
414
POPULATING,
415
416
/** Index is online and available for use */
417
ONLINE,
418
419
/** Index creation failed */
420
FAILED
421
}
422
423
/**
424
* Types of indexes supported
425
*/
426
public enum IndexType {
427
/** Standard B-tree index for exact lookups */
428
BTREE,
429
430
/** Full-text search index */
431
FULLTEXT,
432
433
/** Text index for string operations */
434
TEXT,
435
436
/** Range index for numeric and temporal data */
437
RANGE,
438
439
/** Point index for spatial data */
440
POINT
441
}
442
443
/**
444
* Types of constraints supported
445
*/
446
public enum ConstraintType {
447
/** Uniqueness constraint */
448
UNIQUENESS,
449
450
/** Node property existence constraint */
451
NODE_PROPERTY_EXISTENCE,
452
453
/** Relationship property existence constraint */
454
RELATIONSHIP_PROPERTY_EXISTENCE,
455
456
/** Node key constraint (unique + existence) */
457
NODE_KEY,
458
459
/** Property type constraint */
460
PROPERTY_TYPE
461
}
462
463
/**
464
* Property types for type constraints
465
*/
466
public enum PropertyType {
467
BOOLEAN,
468
BYTE,
469
SHORT,
470
INT,
471
LONG,
472
FLOAT,
473
DOUBLE,
474
STRING,
475
BYTE_ARRAY,
476
SHORT_ARRAY,
477
INT_ARRAY,
478
LONG_ARRAY,
479
FLOAT_ARRAY,
480
DOUBLE_ARRAY,
481
STRING_ARRAY,
482
BOOLEAN_ARRAY,
483
POINT,
484
DATE,
485
TIME,
486
LOCAL_TIME,
487
DATETIME,
488
LOCAL_DATETIME,
489
DURATION
490
}
491
```
492
493
**Advanced Usage Examples:**
494
495
```java
496
// Complete schema management workflow
497
try (Transaction tx = graphDb.beginTx()) {
498
Schema schema = tx.schema();
499
500
// Create indexes for performance
501
IndexDefinition userEmailIndex = schema.indexFor(label("User"))
502
.on("email")
503
.withName("user_email_index")
504
.create();
505
506
IndexDefinition productNameIndex = schema.indexFor(label("Product"))
507
.on("name")
508
.withIndexType(IndexType.TEXT)
509
.create();
510
511
// Create constraints for data integrity
512
ConstraintDefinition userEmailUnique = schema.constraintFor(label("User"))
513
.assertPropertyIsUnique("email")
514
.create();
515
516
ConstraintDefinition userNameExists = schema.constraintFor(label("User"))
517
.assertPropertyExists("name")
518
.create();
519
520
tx.commit();
521
}
522
523
// Monitor index creation progress
524
try (Transaction tx = graphDb.beginTx()) {
525
Schema schema = tx.schema();
526
527
for (IndexDefinition index : schema.getIndexes()) {
528
IndexState state = schema.getIndexState(index);
529
System.out.println("Index " + index.getName() + ": " + state);
530
531
if (state == IndexState.POPULATING) {
532
double progress = schema.getIndexPopulationProgress(index);
533
System.out.println(" Progress: " + (progress * 100) + "%");
534
} else if (state == IndexState.FAILED) {
535
String failure = schema.getIndexFailure(index);
536
System.out.println(" Failure: " + failure);
537
}
538
}
539
540
tx.commit();
541
}
542
543
// Drop indexes and constraints
544
try (Transaction tx = graphDb.beginTx()) {
545
Schema schema = tx.schema();
546
547
// Find and drop specific index
548
for (IndexDefinition index : schema.getIndexes(label("User"))) {
549
if (index.getPropertyKeys().iterator().next().equals("email")) {
550
index.drop();
551
System.out.println("Dropped index on User.email");
552
break;
553
}
554
}
555
556
// Find and drop specific constraint
557
for (ConstraintDefinition constraint : schema.getConstraints(label("User"))) {
558
if (constraint.getConstraintType() == ConstraintType.UNIQUENESS) {
559
constraint.drop();
560
System.out.println("Dropped uniqueness constraint");
561
break;
562
}
563
}
564
565
tx.commit();
566
}
567
```