cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Index creation

Server community edition 4.4.7
Is there a way to create indexes for all the nodes who have a property with a given name?

1 ACCEPTED SOLUTION

This is the solution if someone is interested:

// Create an index on all 
CALL db.labels() YIELD label

UNWIND label as lab

WITH 
"CREATE INDEX index_created_at_" + lab + " IF NOT EXISTS FOR (n:" + lab + ") ON (n.CREATED_AT)" as v1,
"CREATE INDEX index_updated_at_" + lab + " IF NOT EXISTS FOR (n:" + lab + ") ON (n.UPDATED_AT)" as v2,
"CREATE INDEX index_deleted_at_" + lab + " IF NOT EXISTS FOR (n:" + lab + ") ON (n.DELETED_AT)" as v3

call apoc.cypher.runSchema(v1,{}) YIELD value as v11
call apoc.cypher.runSchema(v2,{}) YIELD value as v12
call apoc.cypher.runSchema(v3,{}) YIELD value as v13

return v11, v12, v13

View solution in original post

2 REPLIES 2

I tried with:

CALL db.labels() YIELD label

UNWIND label as lab

call apoc.cypher.run("CREATE INDEX index_created_at_" + lab + " IF NOT EXISTS FOR (n:" + lab + ") on (n.'CREATED_AT')",{lab: lab}) YIELD value as v1

but it returns an error:

Neo.ClientError.Procedure.ProcedureCallFailed
Failed to invoke procedure `apoc.cypher.run`: Caused by: org.neo4j.exceptions.SyntaxException: Invalid input 'INDEX': expected "(", "allShortestPaths" or "shortestPath" (line 1, column 31 (offset: 30))
" WITH  $`lab` as `lab` CREATE INDEX index_created_at_ErrorArea IF NOT EXISTS FOR (n:ErrorArea) on (n.'CREATED_AT')"
                               ^

where ErrorArea is the first returned label.

Any suggestion?

This is the solution if someone is interested:

// Create an index on all 
CALL db.labels() YIELD label

UNWIND label as lab

WITH 
"CREATE INDEX index_created_at_" + lab + " IF NOT EXISTS FOR (n:" + lab + ") ON (n.CREATED_AT)" as v1,
"CREATE INDEX index_updated_at_" + lab + " IF NOT EXISTS FOR (n:" + lab + ") ON (n.UPDATED_AT)" as v2,
"CREATE INDEX index_deleted_at_" + lab + " IF NOT EXISTS FOR (n:" + lab + ") ON (n.DELETED_AT)" as v3

call apoc.cypher.runSchema(v1,{}) YIELD value as v11
call apoc.cypher.runSchema(v2,{}) YIELD value as v12
call apoc.cypher.runSchema(v3,{}) YIELD value as v13

return v11, v12, v13