// Create HBase 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('byImportDate', Vertex.class).addKey(importDate,     Mapping.STRING.asParameter())      .buildCompositeIndex()
//mgmt.buildIndex('byObjectIdx',   Vertex.class).addKey(objectIdx,     Mapping.STRING.asParameter())      .buildCompositeIndex()
//mgmt.buildIndex('bySsnamenr',   Vertex.class).addKey(ssnamenr,       Mapping.STRING.asParameter())      .buildCompositeIndex()
mgmt.buildIndex('byCandid',     Vertex.class).addKey(candid)        .buildCompositeIndex()
mgmt.buildIndex('byJd',         Vertex.class).addKey(jd)            .buildCompositeIndex()
mgmt.buildIndex('byRaDec',      Vertex.class).addKey(ra).addKey(dec).buildCompositeIndex()
//mgmt.buildIndex('byDirection',  Vertex.class).addKey(direction)     .buildCompositeIndex()
                                                                      
mgmt.commit()

// Wait for index
//ManagementSystem.awaitGraphIndexStatus(graph, 'byImportDate').status(SchemaStatus.REGISTERED).call()
//ManagementSystem.awaitGraphIndexStatus(graph, 'byObjectId'  ).status(SchemaStatus.REGISTERED).call()
//ManagementSystem.awaitGraphIndexStatus(graph, 'bySsnamenr'  ).status(SchemaStatus.REGISTERED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'byCandid'    ).status(SchemaStatus.REGISTERED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'byJd'        ).status(SchemaStatus.REGISTERED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'byRaDec'     ).status(SchemaStatus.REGISTERED).call()
//ManagementSystem.awaitGraphIndexStatus(graph, 'byDirection' ).status(SchemaStatus.REGISTERED).call()

// Reindex existing data
mgmt = graph.openManagement()
//mgmt.updateIndex(mgmt.getGraphIndex('byImportDate'), SchemaAction.REINDEX).get()
//mgmt.updateIndex(mgmt.getGraphIndex('byObjectId'  ), SchemaAction.REINDEX).get()
//mgmt.updateIndex(mgmt.getGraphIndex('bySsnamenr'  ), SchemaAction.REINDEX).get()
mgmt.updateIndex(mgmt.getGraphIndex('byCandid'    ), SchemaAction.REINDEX).get()
mgmt.updateIndex(mgmt.getGraphIndex('byJd'        ), SchemaAction.REINDEX).get()
mgmt.updateIndex(mgmt.getGraphIndex('byRaDec'     ), SchemaAction.REINDEX).get()
//mgmt.updateIndex(mgmt.getGraphIndex('byDirection' ), SchemaAction.REINDEX).get()
mgmt.getGraphIndexes(Vertex.class)
mgmt.getGraphIndexes(Edge.class)
mgmt.printIndexes()
mgmt.commit()

// Enable index
//ManagementSystem.awaitGraphIndexStatus(graph, 'byImportDate').status(SchemaStatus.ENABLED).call()
//ManagementSystem.awaitGraphIndexStatus(graph, 'byObjectId'  ).status(SchemaStatus.ENABLED).call()
//ManagementSystem.awaitGraphIndexStatus(graph, 'bySsnamenr'  ).status(SchemaStatus.ENABLED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'byCandid'    ).status(SchemaStatus.ENABLED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'byJd'        ).status(SchemaStatus.ENABLED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'byRaDec'     ).status(SchemaStatus.ENABLED).call()
//ManagementSystem.awaitGraphIndexStatus(graph, 'byDirection' ).status(SchemaStatus.ENABLED).call()
