Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j: Create an index on all labels

i try to create indexes on all of the labels in the DB

this is what i have:

CALL db.labels()
YIELD label
CREATE INDEX ON :label(objectID)

but this gives an error:

Invalid input 'N': expected 'p/P' (line 3, column 15 (offset: 44)) "CREATE INDEX ON :label(webint_keys_objectID)"

like image 630
mabr Avatar asked Sep 13 '25 01:09

mabr


1 Answers

You can't use variables for labels in match, create, or schema operations. You must use the actual label for these operations.

There is a way to add indexes with dynamic labels using APOC Procedures using apoc.schema.assert(), but it will drop all indexes and constraints that aren't present in the assert() call. This would require transforming the labels from db.labels() into keys of a map, however, so you'd need other APOC procedures for that operation.

Since indexes and constraints only need to be created once, it's usually better to do these manually.

But if you do need such a query, this should do the trick:

call db.labels() yield label
with label, ['objectID'] as indexProp
with collect(label) as labels, collect(indexProp) as indexProps
with apoc.map.fromLists(labels, indexProps) as indexMap
call apoc.schema.assert(indexMap,{}) yield label, key, unique, action
return label, key, unique, action
like image 180
InverseFalcon Avatar answered Sep 15 '25 14:09

InverseFalcon