Search in sources :

Example 1 with CreateIndex

use of io.searchbox.indices.CreateIndex 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)

Example 2 with CreateIndex

use of io.searchbox.indices.CreateIndex in project opennms by OpenNMS.

the class EventToIndex method createIndex.

private static void createIndex(JestClient client, String name, String type) throws IOException {
    // create new index
    CreateIndex createIndex = new CreateIndex.Builder(name).build();
    JestResult result = new OnmsJestResult(client.execute(createIndex));
    if (LOG.isDebugEnabled()) {
        LOG.debug("created new alarm index: {} type: {}" + "\n   received search result: {}" + "\n   response code: {}" + "\n   error message: {}", name, type, result.getJsonString(), result.getResponseCode(), result.getErrorMessage());
    }
}
Also used : CreateIndex(io.searchbox.indices.CreateIndex) JestResult(io.searchbox.client.JestResult)

Aggregations

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