Search in sources :

Example 1 with JestResult

use of io.searchbox.client.JestResult 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 JestResult

use of io.searchbox.client.JestResult in project jmxtrans by jmxtrans.

the class ElasticWriterTests method sendMessageToElasticWriteThrowsException.

@Test(expected = IOException.class)
public void sendMessageToElasticWriteThrowsException() throws Exception {
    // return for call, is index created
    when(mockClient.execute(isA(Action.class))).thenThrow(new IOException("Failed to add index entry to elastic."));
    writer.doWrite(dummyServer(), dummyQuery(), ImmutableList.of(result));
    // only one call is expected: the insert index entry. No write is being made with non-numeric values.
    Mockito.verify(mockClient, times(1)).execute(Matchers.<Action<JestResult>>any());
}
Also used : Action(io.searchbox.action.Action) IOException(java.io.IOException) JestResult(io.searchbox.client.JestResult) Test(org.junit.Test)

Example 3 with JestResult

use of io.searchbox.client.JestResult in project gerrit by GerritCodeReview.

the class AbstractElasticIndex method deleteAll.

@Override
public void deleteAll() throws IOException {
    // Delete the index, if it exists.
    JestResult result = client.execute(new IndicesExists.Builder(indexName).build());
    if (result.isSucceeded()) {
        result = client.execute(new DeleteIndex.Builder(indexName).build());
        if (!result.isSucceeded()) {
            throw new IOException(String.format("Failed to delete index %s: %s", indexName, result.getErrorMessage()));
        }
    }
    // Recreate the index.
    result = client.execute(new CreateIndex.Builder(indexName).settings(getMappings()).build());
    if (!result.isSucceeded()) {
        String error = String.format("Failed to create index %s: %s", indexName, result.getErrorMessage());
        throw new IOException(error);
    }
}
Also used : DeleteIndex(io.searchbox.indices.DeleteIndex) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) GsonBuilder(com.google.gson.GsonBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) IOException(java.io.IOException) JestResult(io.searchbox.client.JestResult)

Example 4 with JestResult

use of io.searchbox.client.JestResult in project gerrit by GerritCodeReview.

the class AbstractElasticIndex method delete.

@Override
public void delete(K c) throws IOException {
    Bulk bulk = addActions(new Bulk.Builder(), c).refresh(true).build();
    JestResult result = client.execute(bulk);
    if (!result.isSucceeded()) {
        throw new IOException(String.format("Failed to delete change %s in index %s: %s", c, indexName, result.getErrorMessage()));
    }
}
Also used : IOException(java.io.IOException) Bulk(io.searchbox.core.Bulk) JestResult(io.searchbox.client.JestResult)

Example 5 with JestResult

use of io.searchbox.client.JestResult in project gerrit by GerritCodeReview.

the class ElasticAccountIndex method replace.

@Override
public void replace(AccountState as) throws IOException {
    Bulk bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(ACCOUNTS).addAction(insert(ACCOUNTS, as)).refresh(true).build();
    JestResult result = client.execute(bulk);
    if (!result.isSucceeded()) {
        throw new IOException(String.format("Failed to replace account %s in index %s: %s", as.getAccount().getId(), indexName, result.getErrorMessage()));
    }
}
Also used : Builder(io.searchbox.core.Bulk.Builder) IOException(java.io.IOException) Bulk(io.searchbox.core.Bulk) JestResult(io.searchbox.client.JestResult)

Aggregations

JestResult (io.searchbox.client.JestResult)12 IOException (java.io.IOException)6 Bulk (io.searchbox.core.Bulk)4 Builder (io.searchbox.core.Bulk.Builder)3 CreateIndex (io.searchbox.indices.CreateIndex)3 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 Result (com.googlecode.jmxtrans.model.Result)2 Test (org.junit.Test)2 GsonBuilder (com.google.gson.GsonBuilder)1 OrmException (com.google.gwtorm.server.OrmException)1 Action (io.searchbox.action.Action)1 DocumentResult (io.searchbox.core.DocumentResult)1 Index (io.searchbox.core.Index)1 DeleteIndex (io.searchbox.indices.DeleteIndex)1 IndicesExists (io.searchbox.indices.IndicesExists)1 GetAliases (io.searchbox.indices.aliases.GetAliases)1 PutMapping (io.searchbox.indices.mapping.PutMapping)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1