Schema Information

To drop, create, or show indexes and constraints, you can use the following procedure:

Qualified Name Type

apoc.schema.assert

apoc.schema.assert({indexLabel:, …​}, {constraintLabel:[constraintKeys], …​}, dropExisting : true) yield label, key, keys, unique, action - drops all other existing indexes and constraints when dropExisting is true (default is true), and asserts that at the end of the operation the given indexes and unique constraints are there, each label:key pair is considered one constraint/label. Non-constraint indexes can define compound indexes with label:[key1,key2…​] pairings.

Procedure

apoc.schema.nodes

apoc.schema.nodes(config MAP<STRING, ANY>) - returns all indexes and constraints information for all NODE labels in the database. It is possible to define a set of labels to include or exclude in the config parameters.

Procedure

apoc.schema.relationships

apoc.schema.relationships(config MAP<STRING, ANY>) - returns the indexes and constraints information for all the relationship types in the database. It is possible to define a set of relationship types to include or exclude in the config parameters.

Procedure

apoc.schema.node.constraintExists

RETURN apoc.schema.node.constraintExists(labelName, propertyNames)

Function

apoc.schema.relationship.constraintExists

RETURN apoc.schema.relationship.constraintExists(type, propertyNames)

Function

apoc.schema.node.indexExists

RETURN apoc.schema.node.indexExists(labelName, propertyNames)

Function

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

STRING

the name of the index/constraint

label

STRING or LIST<STRING>

the label (or list of labels) of the index/constraint

properties

LIST<STRING>

the property keys that are affected by the index/constraint

status

STRING

indexes can have one of the following values: ONLINE, POPULATING and FAILED. Constraints have an empty string as a value

type

STRING

the index or constraint type. See the table below for possible values

failure

STRING

In case of index with status "FAILED" returns the failure message of a failed index (matches the output of SHOW INDEX YIELD failureMessage). Otherwise, it returns "NO FAILURE"

populationProgress

FLOAT

the percentage of affected entities that were scanned by the index (matches the output of SHOW INDEX YIELD populationPercent). Constraints have 0.0 as a value

size

INTEGER

number of entities affected by the index. Constraints have 0 as a value.

valuesSelectivity

FLOAT

number of distinct property values existing for the affected property keys divided by size.

For example, with an index CREATE INDEX FOR (n:Node) ON (n.foo), the number of distinct property values is MATCH (n:Node) RETURN count(DISTINCT n.foo).

Constraints have 0.0 as a value

userDescription

STRING

a description of the index/constraint

The type result can have one of the following values:

Table 1. type output
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

STRING

the name of the index/constraint

type

STRING

the index or constraint type. See the table below for possible values

properties

LIST<STRING>

the property keys that are affected by the index/constraint

status

STRING

indexes can have one of the following values: ONLINE, POPULATING and FAILED. Constraints have an empty string as a value

relationshipType

STRING or LIST<STRING>

the type (or list of types) of the index/constraint

The type result can have one of the following values:

Table 2. type output
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:

apoc.schema.assert.index

When you run the following query:

CALL apoc.schema.assert(null,{Foo:['bar']})

you will receive this result:

apoc.schema.assert.constraint

When you run the following query:

CALL apoc.schema.assert(null,null)

you will receive this result:

apoc.schema.assert.drop

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:

apoc.schema.nodes

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:

apoc.schema.relationships

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:

apoc.schema.node.indexExists

Given the previous constraint definitions, running this statement:

RETURN apoc.schema.node.constraintExists("Bar", ["foobar"])

produces the following output:

apoc.schema.node.constraintExists

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:

apoc.schema.relationship.constraintExists