Rechercher, créer ou supprimer un index dans ES - Ahp06/Monitoring GitHub Wiki
Pour pouvoir remplir la base de données il faut d’abord y mettre un index, se comportant comme le tout premier objet d’un certain type.
IndicesExistsResponse response = client.admin().indices().prepareExists("update")
.get(TimeValue.timeValueSeconds(1));
La classe IndicesExistsResponse permet de rechercher un index, et de vérifier son existence avec la méthode prepareExists(monindex).
La création d’index peut se faire de plusieurs manières :
- En spécifiant une source souvent au format JSON : setSource(myJSON)
- En utilisant une API d’elasticsearch : XContentFactory qui permet de créer son objet JSON :
client.prepareIndex("update","MachineUpdate").setSource(XContentFactory
.jsonBuilder().startObject()
.field("machineID",firstUpdate.getMachineID())
.field("machineName", firstUpdate.getMachineName())
.field("state", firstUpdate.getState())
.field("stateLabel", getStateLabel(firstUpdate.getState()))
.field("time",firstUpdate.getTime())
.endObject())
.execute().actionGet();
Pour supprimer un index nous pouvons utiliser la classe DeleteIndexRequest:
client.admin().indices().delete(new DeleteIndexRequest(indexName)).actionGet();