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);
}
}
}
}
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());
}
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);
}
}
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()));
}
}
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()));
}
}
Aggregations