Search in sources :

Example 1 with IndicesExists

use of io.searchbox.indices.IndicesExists in project jmxtrans by jmxtrans.

the class ElasticWriter method createMappingIfNeeded.

private static void createMappingIfNeeded(JestClient jestClient, String indexName, String typeName) throws ElasticWriterException, IOException {
    synchronized (CREATE_MAPPING_LOCK) {
        IndicesExists indicesExists = new IndicesExists.Builder(indexName).build();
        boolean indexExists = jestClient.execute(indicesExists).isSucceeded();
        if (!indexExists) {
            CreateIndex createIndex = new CreateIndex.Builder(indexName).build();
            jestClient.execute(createIndex);
            URL url = ElasticWriter.class.getResource("/elastic-mapping.json");
            String mapping = Resources.toString(url, Charsets.UTF_8);
            PutMapping putMapping = new PutMapping.Builder(indexName, typeName, mapping).build();
            JestResult result = jestClient.execute(putMapping);
            if (!result.isSucceeded()) {
                throw new ElasticWriterException(String.format("Failed to create mapping: %s", result.getErrorMessage()));
            } else {
                log.info("Created mapping for index {}", indexName);
            }
        }
    }
}
Also used : CreateIndex(io.searchbox.indices.CreateIndex) PutMapping(io.searchbox.indices.mapping.PutMapping) URL(java.net.URL) JestResult(io.searchbox.client.JestResult) IndicesExists(io.searchbox.indices.IndicesExists)

Aggregations

JestResult (io.searchbox.client.JestResult)1 CreateIndex (io.searchbox.indices.CreateIndex)1 IndicesExists (io.searchbox.indices.IndicesExists)1 PutMapping (io.searchbox.indices.mapping.PutMapping)1 URL (java.net.URL)1