// Create ElasticSearch Index
// (Lomikel schema should be already loaded)
// (uses direct indexing, big data can use also MR indexing) 
// =========================================================

graph.tx().rollback()

mgmt = graph.openManagement()

// Properties
importDate = mgmt.getPropertyKey('importDate')
lbl        = mgmt.getPropertyKey('lbl'      )
objectId   = mgmt.getPropertyKey('objectId' )
ssnamenr   = mgmt.getPropertyKey('ssnamenr' )
candid     = mgmt.getPropertyKey('candid'   )
jd         = mgmt.getPropertyKey('jd'       )
ra         = mgmt.getPropertyKey('ra'       )
dec        = mgmt.getPropertyKey('dec'      )
direction  = mgmt.getPropertyKey('direction')
alert      = mgmt.getVertexLabel('alert'    )

//  Indexes                      
mgmt.buildIndex('byImportDateES', Vertex.class).addKey(importDate,     Mapping.TEXTSTRING.asParameter())       .buildMixedIndex('search')
mgmt.buildIndex('byObjectIdES',   Vertex.class).addKey(objectId,       Mapping.TEXTSTRING.asParameter())       .buildMixedIndex('search')
mgmt.buildIndex('bySsnamenrES',   Vertex.class).addKey(ssnamenr,       Mapping.TEXTSTRING.asParameter())       .buildMixedIndex('search')
//mgmt.buildIndex('byCandidES',     Vertex.class).addKey(candid)                                           .buildMixedIndex('search')
//mgmt.buildIndex('byJdES',         Vertex.class).addKey(jd)                                               .buildMixedIndex('search')
//mgmt.buildIndex('byRaDecES',      Vertex.class).addKey(ra).addKey(dec)                                   .buildMixedIndex('search')
mgmt.buildIndex('byDirectionES',  Vertex.class).addKey(direction,      Mapping.PREFIX_TREE.asParameter()).buildMixedIndex('search')

mgmt.commit()

// Wait for index
ManagementSystem.awaitGraphIndexStatus(graph, 'byImportDateES').status(SchemaStatus.REGISTERED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'byObjectIdES'  ).status(SchemaStatus.REGISTERED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'bySsnamenrES'  ).status(SchemaStatus.REGISTERED).call()
//ManagementSystem.awaitGraphIndexStatus(graph, 'byCandidES'    ).status(SchemaStatus.REGISTERED).call()
//ManagementSystem.awaitGraphIndexStatus(graph, 'byJdES'        ).status(SchemaStatus.REGISTERED).call()
//ManagementSystem.awaitGraphIndexStatus(graph, 'byRaDecES'     ).status(SchemaStatus.REGISTERED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'byDirectionES' ).status(SchemaStatus.REGISTERED).call()

// Reindex existing data
mgmt = graph.openManagement()
mgmt.updateIndex(mgmt.getGraphIndex('byImportDateES'), SchemaAction.REINDEX).get()
mgmt.updateIndex(mgmt.getGraphIndex('byObjectIdES'  ), SchemaAction.REINDEX).get()
mgmt.updateIndex(mgmt.getGraphIndex('bySsnamenrES'  ), SchemaAction.REINDEX).get()
//mgmt.updateIndex(mgmt.getGraphIndex('byCandidES'    ), SchemaAction.REINDEX).get()
//mgmt.updateIndex(mgmt.getGraphIndex('byJdES'        ), SchemaAction.REINDEX).get()
//mgmt.updateIndex(mgmt.getGraphIndex('byRaDecES'     ), SchemaAction.REINDEX).get()
mgmt.updateIndex(mgmt.getGraphIndex('byDirectionES' ), SchemaAction.REINDEX).get()
mgmt.getGraphIndexes(Vertex.class)
mgmt.getGraphIndexes(Edge.class)
mgmt.printIndexes()
mgmt.commit()

// Enable index
ManagementSystem.awaitGraphIndexStatus(graph, 'byImportDateES').status(SchemaStatus.ENABLED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'byObjectIdES'  ).status(SchemaStatus.ENABLED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'bySsnamenrES'  ).status(SchemaStatus.ENABLED).call()
//ManagementSystem.awaitGraphIndexStatus(graph, 'byCandidES'    ).status(SchemaStatus.ENABLED).call()
//ManagementSystem.awaitGraphIndexStatus(graph, 'byJdES'        ).status(SchemaStatus.ENABLED).call()
//ManagementSystem.awaitGraphIndexStatus(graph, 'byRaDecES'     ).status(SchemaStatus.ENABLED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'byDirectionES' ).status(SchemaStatus.ENABLED).call()
