Schema Information
To drop, create, or show indexes and constraints, you can use the following procedure:
Qualified Name | Type |
---|---|
apoc.schema.assert |
|
apoc.schema.nodes |
|
apoc.schema.relationships |
|
apoc.schema.node.constraintExists |
|
apoc.schema.relationship.constraintExists |
|
apoc.schema.node.indexExists |
|
CALL apoc.schema.assert({indexLabel:[[indexKeys]], ...}, {constraintLabel:[constraintKeys], ...}, dropExisting : true)
YIELD label, key, keys, unique, action
Where the outputs are:
-
label
-
key
-
keys, list of the key
-
unique, if the index or constraint are unique
-
action, can be the following values: DROPPED, CREATED
To retrieve indexes and constraints information for all the node labels in your database, you can use the following procedure:
CALL apoc.schema.nodes()
Where the outputs are:
name | type | description |
---|---|---|
name |
|
the name of the index/constraint |
label |
|
the label (or list of labels) of the index/constraint |
properties |
|
the property keys that are affected by the index/constraint |
status |
|
indexes can have one of the following values: |
type |
|
the index or constraint type. See the table below for possible values |
failure |
|
In case of index with status "FAILED" returns the failure message of a failed index
(matches the output of |
populationProgress |
|
the percentage of affected entities that were scanned by the index
(matches the output of |
size |
|
number of entities affected by the index. Constraints have |
valuesSelectivity |
|
number of distinct property values existing for the affected property keys divided by For example, with an index Constraints have |
userDescription |
|
a description of the index/constraint |
The type
result can have one of the following values:
name | Schema type |
---|---|
"UNIQUENESS" |
Unique node property constraint |
"NODE_PROPERTY_EXISTENCE" |
Node property existence constraint |
"NODE_KEY" |
Node key constraint |
"FULLTEXT" |
Full-text index |
"TEXT" |
Text index |
"RANGE" |
Range index |
"POINT" |
Point index |
"LOOKUP" |
Lookup index |
To retrieve the indexes and constraint information for all the relationship types in your database, you can use the following procedure:
CALL apoc.schema.relationships()
Where the outputs are:
name | type | description |
---|---|---|
name |
|
the name of the index/constraint |
type |
|
the index or constraint type. See the table below for possible values |
properties |
|
the property keys that are affected by the index/constraint |
status |
|
indexes can have one of the following values: |
relationshipType |
|
the type (or list of types) of the index/constraint |
The type
result can have one of the following values:
name | Schema type |
---|---|
"RELATIONSHIP_PROPERTY_EXISTENCE" |
Relationship property existence constraints |
"FULLTEXT" |
Full-text index |
"TEXT" |
Text index |
"RANGE" |
Range index |
"POINT" |
Point index |
"LOOKUP" |
Lookup index |
CALL apoc.schema.nodes({labels:['Book']})
N.B. Constraints for property existence on nodes and relationships are available only for the Enterprise Edition.
To retrieve the index existence on node, you can use the following user function:
RETURN apoc.schema.node.indexExists(labelName, propertyNames)
The output return the index existence on node is present or not
To retrieve if the constraint exists on node, you can use the following user function:
RETURN apoc.schema.node.constraintExists(labelName, propertyNames)
The output return the constraint existence on node.
To retrieve if the constraint exists on relationship, you can use the following user function:
RETURN apoc.schema.relationship.constraintExists(type, propertyNames)
The output return the constraint on the relationship is present or not
Examples
List Schema assert
When you run the following query:
CALL apoc.schema.assert({Foo:['bar']},null)
you will receive this result:
When you run the following query:
CALL apoc.schema.assert(null,{Foo:['bar']})
you will receive this result:
When you run the following query:
CALL apoc.schema.assert(null,null)
you will receive this result:
List indexes and constraints for nodes
Given the following cypher statements:
CREATE CONSTRAINT FOR (bar:Bar) REQUIRE bar.foobar IS NOT NULL
CREATE CONSTRAINT FOR (bar:Bar) REQUIRE bar.foo IS UNIQUE
CREATE INDEX FOR (n:Person) ON (n.name)
CREATE INDEX FOR (n:Publication) ON (n.name)
CREATE INDEX FOR (n:Source) ON (n.name)
When you run the following query:
CALL apoc.schema.nodes()
you will receive this result:
List constraints for relationships
Given the following cypher statements:
CREATE CONSTRAINT FOR ()-[like:LIKED]-() REQUIRE like.day IS NOT NULL
CREATE CONSTRAINT FOR ()-[starred:STARRED]-() REQUIRE starred.month IS NOT NULL
When you run the following query:
CALL apoc.schema.relationships()
you will receive this result:
Check if an index or a constraint exists for a Label and property
Given the previous index definitions, running this statement:
RETURN apoc.schema.node.indexExists("Publication", ["name"])
produces the following output:
Given the previous constraint definitions, running this statement:
RETURN apoc.schema.node.constraintExists("Bar", ["foobar"])
produces the following output:
If you want to check if a constraint exists for a relationship you can run this statement:
RETURN apoc.schema.relationship.constraintExists('LIKED', ['day'])
and you get the following result: