Recreating Indexes and Constraints on 4.4 and above
This article describes the process to drop and recreate all indexes and constraints from Neo4j version 4.4.x and above. Please take care to ensure you have captured you index / constraint definitions before dropping.
Also please run drop and create from separate sessions, to avoid any caching retention of objects existing.
This can be done in either the Neo4j Browser or cypher-shell.
Depending on your version, you may need to strip out double quotes from the export.
Note: to list the current indexes and constraints please run either SHOW INDEXES; or SHOW CONSTRAINTS;
1 - Generating Cypher Scripts to Drop/Create Constraints:
NOTE: If it is uncertain that an index or constraint with a given name exists that you want to drop. Should you not want to receive an error if it doesn't, use IF EXISTS
with the queries that follow, for example DROP INDEX my_index_name IF EXISTS;
1.1 - Script to create Constraints
Run the Cypher : SHOW CONSTRAINTS YIELD createStatement
create-constraints.cypher
+---------------------------------------------------------------------------------------------------+
| "CREATE CONSTRAINT `constraint_182b7cf9` FOR (n:`Debit`) REQUIRE (n.`id`) IS UNIQUE OPTIONS {indexConfig: {`spatial.cartesian-3d.max`: [1000000.0, 1000000.0, 1000000.0],`spatial.cartesian-3d.min`: [-1000000.0, -1000000.0, -1000000.0],`spatial.cartesian.max`: [1000000.0, 1000000.0],`spatial.cartesian.min`: [-1000000.0, -1000000.0],`spatial.wgs-84-3d.max`: [180.0, 90.0, 1000000.0],`spatial.wgs-84-3d.min`: [-180.0, -90.0, -1000000.0],`spatial.wgs-84.max`: [180.0, 90.0],`spatial.wgs-84.min`: [-180.0, -90.0]}, indexProvider: 'native-btree-1.0'}" |
| "CREATE CONSTRAINT `constraint_5d4fabb7` FOR (n:`Mule`) REQUIRE (n.`id`) IS UNIQUE OPTIONS {indexConfig: {`spatial.cartesian-3d.max`: [1000000.0, 1000000.0, 1000000.0],`spatial.cartesian-3d.min`: [-1000000.0, -1000000.0, -1000000.0],`spatial.cartesian.max`: [1000000.0, 1000000.0],`spatial.cartesian.min`: [-1000000.0, -1000000.0],`spatial.wgs-84-3d.max`: [180.0, 90.0, 1000000.0],`spatial.wgs-84-3d.min`: [-180.0, -90.0, -1000000.0],`spatial.wgs-84.max`: [180.0, 90.0],`spatial.wgs-84.min`: [-180.0, -90.0]}, indexProvider: 'native-btree-1.0'}" |
| "CREATE CONSTRAINT `constraint_ee8a7860` FOR (n:`Merchant`) REQUIRE (n.`id`) IS UNIQUE OPTIONS {indexConfig: {`spatial.cartesian-3d.max`: [1000000.0, 1000000.0, 1000000.0],`spatial.cartesian-3d.min`: [-1000000.0, -1000000.0, -1000000.0],`spatial.cartesian.max`: [1000000.0, 1000000.0],`spatial.cartesian.min`: [-1000000.0, -1000000.0],`spatial.wgs-84-3d.max`: [180.0, 90.0, 1000000.0],`spatial.wgs-84-3d.min`: [-180.0, -90.0, -1000000.0],`spatial.wgs-84.max`: [180.0, 90.0],`spatial.wgs-84.min`: [-180.0, -90.0]}, indexProvider: 'native-btree-1.0'}" |
| "CREATE CONSTRAINT `constraint_fcf6553a` FOR (n:`Client`) REQUIRE (n.`id`) IS UNIQUE OPTIONS {indexConfig: {`spatial.cartesian-3d.max`: [1000000.0, 1000000.0, 1000000.0],`spatial.cartesian-3d.min`: [-1000000.0, -1000000.0, -1000000.0],`spatial.cartesian.max`: [1000000.0, 1000000.0],`spatial.cartesian.min`: [-1000000.0, -1000000.0],`spatial.wgs-84-3d.max`: [180.0, 90.0, 1000000.0],`spatial.wgs-84-3d.min`: [-180.0, -90.0, -1000000.0],`spatial.wgs-84.max`: [180.0, 90.0],`spatial.wgs-84.min`: [-180.0, -90.0]}, indexProvider: 'native-btree-1.0'}" |
+---------------------------------------------------------------------------------------------------+
1.2 - Script to drop Constraints
Run the Cypher SHOW CONSTRAINTS YIELD name RETURN 'DROP CONSTRAINT ' + name + ';'
Save this output to file. For example: drop-constraints.cypher
+----------------------------------------------+
| 'DROP CONSTRAINT ' + name +';' |
+----------------------------------------------+
| "DROP CONSTRAINT constraint_182b7cf9;" |
| "DROP CONSTRAINT constraint_5d4fabb7;" |
| "DROP CONSTRAINT constraint_ee8a7860;" |
| "DROP CONSTRAINT constraint_fcf6553a;" |
+----------------------------------------------+
2 - Generating Cypher Scripts to Drop/Manage Indexes:
2.1 - Script to create Indexes
SHOW INDEXES YIELD createStatement;
create-indexes.cypher
+----------------------------------------------------------------------------+
| createStatement |
+----------------------------------------------------------------------------+
| "CREATE LOOKUP INDEX `index_343aff4e` FOR (n) ON EACH labels(n)" |
| "CREATE LOOKUP INDEX `index_f7700477` FOR ()-[r]-() ON EACH type(r)" |
+----------------------------------------------------------------------------+
2.2 - Script to drop Indexes
Run this Cypher SHOW INDEXES YIELD name RETURN 'DROP INDEX ' + name +';'
Save this output to file. For example: drop-indexes.cypher
3 - Run scripts as you need to drop / recreate indexes
In either the Neo4j Browser or via cypher-shell, paste in the scriptname.cypher
script and execute. Monitor for any errors.
4 - Run scripts as you need to drop / recreate constraints
In either the Neo4j Browser or via cypher-shell, paste in the scriptname.cypher
script and execute. Monitor for any errors.
5 - Confirm all Indexes and Constraints are correct:
To confirm the indexes and constraints creation have succeeded you need not only to check they exists, but verify they are populated, online and also have the correct provider.
5.1 - Verify Indexes
Run the Cypher SHOW INDEXES YIELD name;
+------------------+
| name |
+------------------+
| "index_343aff4e" |
| "index_f7700477" |
+------------------+
5.2 - Verify Constraints
Run the Cypher SHOW CONSTRAINTS YIELD name;
+-----------------------+
| name |
+-----------------------+
| "constraint_182b7cf9" |
| "constraint_5d4fabb7" |
| "constraint_ee8a7860" |
| "constraint_fcf6553a" |
+-----------------------+
AuraDB Instance privileges required
Creating an index requires the CREATE INDEX
privilege, while dropping an index requires the DROP INDEX
privilege and listing indexes require the SHOW INDEX
privilege.
Note: Be sure to apply appropriate labels.
Comments
0 comments
Article is closed for comments.