use of io.searchbox.client.JestResult in project gerrit by GerritCodeReview.
the class ElasticChangeIndex method replace.
@Override
public void replace(ChangeData cd) throws IOException {
String deleteIndex;
String insertIndex;
try {
if (cd.change().getStatus().isOpen()) {
insertIndex = OPEN_CHANGES;
deleteIndex = CLOSED_CHANGES;
} else {
insertIndex = CLOSED_CHANGES;
deleteIndex = OPEN_CHANGES;
}
} catch (OrmException e) {
throw new IOException(e);
}
Bulk bulk = new Bulk.Builder().defaultIndex(indexName).defaultType("changes").addAction(insert(insertIndex, cd)).addAction(delete(deleteIndex, cd.getId())).refresh(true).build();
JestResult result = client.execute(bulk);
if (!result.isSucceeded()) {
throw new IOException(String.format("Failed to replace change %s in index %s: %s", cd.getId(), indexName, result.getErrorMessage()));
}
}
use of io.searchbox.client.JestResult in project opennms by OpenNMS.
the class BasicAuthTest method canPerformPreemptiveAuth.
@Test
public void canPerformPreemptiveAuth() throws IOException {
stubFor(post(urlEqualTo("/_all/_search")).withBasicAuth("mi", "mimimi").willReturn(aResponse().withBody("{\n" + " \"took\": 1,\n" + " \"timed_out\": false,\n" + " \"_shards\":{\n" + " \"total\" : 1,\n" + " \"successful\" : 1,\n" + " \"skipped\" : 0,\n" + " \"failed\" : 0\n" + " },\n" + " \"hits\":{\n" + " \"total\" : 0,\n" + " \"max_score\": 0,\n" + " \"hits\" : []\n" + " }\n" + "}")));
// Create the REST client factory in a fashion similar to how it's created in the Blueprint
// and specify some basic auth credentials
RestClientFactory restClientFactory = new RestClientFactory(wireMockRule.url(""), "mi", "mimimi");
restClientFactory.setTimeout(3000);
restClientFactory.setSocketTimeout(3000);
restClientFactory.setRetries(0);
// Execute a request with our client, the contents of the search don't really matter
// here, we just need some valid JSON
JestClient client = restClientFactory.getJestClient();
JestResult result = client.execute(new Search.Builder("{\n" + " \"query\" : {\n" + " \"term\" : { \"user\" : \"kimchy\" }\n" + " }\n" + "}").build());
// Verify
assertTrue("The search request using Jest should succeed.", result.isSucceeded());
// Cleanup
client.shutdownClient();
}
use of io.searchbox.client.JestResult in project nutch by apache.
the class ElasticRestIndexWriter method commit.
@Override
public void commit() throws IOException {
if (basicFuture != null) {
// wait for previous to finish
long beforeWait = System.currentTimeMillis();
try {
JestResult result = basicFuture.get();
if (result == null) {
throw new RuntimeException();
}
long msWaited = System.currentTimeMillis() - beforeWait;
LOG.info("Previous took in ms {}, including wait {}", millis, msWaited);
} catch (InterruptedException | ExecutionException e) {
LOG.error("Error waiting for result ", e);
}
basicFuture = null;
}
if (bulkBuilder != null) {
if (bulkDocs > 0) {
// start a flush, note that this is an asynchronous call
basicFuture = new BasicFuture<>(null);
millis = System.currentTimeMillis();
client.executeAsync(bulkBuilder.build(), new JestResultHandler<BulkResult>() {
@Override
public void completed(BulkResult bulkResult) {
basicFuture.completed(bulkResult);
millis = System.currentTimeMillis() - millis;
}
@Override
public void failed(Exception e) {
basicFuture.completed(null);
LOG.error("Failed result: ", e);
}
});
}
bulkBuilder = null;
}
if (createNewBulk) {
// Prepare a new bulk request
bulkBuilder = new Bulk.Builder().defaultIndex(defaultIndex).defaultType(defaultType);
bulkDocs = 0;
bulkLength = 0;
}
}
use of io.searchbox.client.JestResult in project jmxtrans by jmxtrans.
the class ElasticWriter method internalWrite.
@Override
protected void internalWrite(Server server, Query query, ImmutableList<Result> results) throws Exception {
for (Result result : results) {
log.debug("Query result: [{}]", result);
Map<String, Object> resultValues = result.getValues();
for (Entry<String, Object> values : resultValues.entrySet()) {
Object value = values.getValue();
if (isNumeric(value)) {
Map<String, Object> map = new HashMap<>();
map.put("serverAlias", server.getAlias());
map.put("server", server.getHost());
map.put("port", server.getPort());
map.put("objDomain", result.getObjDomain());
map.put("className", result.getClassName());
map.put("typeName", result.getTypeName());
map.put("attributeName", result.getAttributeName());
map.put("key", values.getKey());
map.put("keyAlias", result.getKeyAlias());
map.put("value", Double.parseDouble(value.toString()));
map.put("timestamp", result.getEpoch());
log.debug("Insert into Elastic: Index: [{}] Type: [{}] Map: [{}]", indexName, ELASTIC_TYPE_NAME, map);
Index index = new Index.Builder(map).index(indexName).type(ELASTIC_TYPE_NAME).build();
JestResult addToIndex = jestClient.execute(index);
if (!addToIndex.isSucceeded()) {
throw new ElasticWriterException(String.format("Unable to write entry to elastic: %s", addToIndex.getErrorMessage()));
}
} else {
log.warn("Unable to submit non-numeric value to Elastic: [{}] from result [{}]", value, result);
}
}
}
}
use of io.searchbox.client.JestResult in project jmxtrans by jmxtrans.
the class ElasticWriterTests method sendNonNumericMessageToElastic.
@Test
public void sendNonNumericMessageToElastic() throws Exception {
Result resultWithNonNumericValue = new Result(1, "attributeName", "className", "objDomain", "classNameAlias", "typeName", ImmutableMap.of("key", (Object) "abc"));
writer.doWrite(dummyServer(), dummyQuery(), ImmutableList.of(resultWithNonNumericValue));
// only one call is expected: the index check. No write is being made with non-numeric values.
Mockito.verify(mockClient, times(0)).execute(Matchers.<Action<JestResult>>any());
}
Aggregations