mabr
mabr

Reputation: 380

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)"

Upvotes: 4

Views: 2057

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30407

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

Upvotes: 3

Related Questions