cancel
Showing results for 
Search instead for 
Did you mean: 

Index creation

paolodipietro58
Graph Voyager

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

paolodipietro58
Graph Voyager

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

paolodipietro58
Graph Voyager

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?

paolodipietro58
Graph Voyager

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
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.