use of io.searchbox.core.Bulk 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.core.Bulk 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()));
}
}
use of io.searchbox.core.Bulk 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.core.Bulk in project opennms by OpenNMS.
the class EventToIndex method sendEvents.
private void sendEvents(final List<BulkableAction<DocumentResult>> actions) {
if (actions != null && actions.size() > 0) {
// Split the actions up by index
for (Map.Entry<String, List<BulkableAction<DocumentResult>>> entry : actions.stream().collect(Collectors.groupingBy(BulkableAction::getIndex)).entrySet()) {
try {
final List<BulkableAction<DocumentResult>> actionList = entry.getValue();
if (entry.getValue().size() == 1) {
// Not bulk
final BulkableAction<DocumentResult> action = actionList.get(0);
executeSingleAction(getJestClient(), action);
} else {
// The type will be identical for all events in the same index
final String type = actionList.get(0).getType();
final Bulk.Builder builder = new Bulk.Builder().defaultIndex(entry.getKey()).defaultType(type);
// Add all actions to the bulk operation
builder.addAction(actionList);
final Bulk bulk = builder.build();
BulkResult result = getJestClient().execute(bulk);
// If the bulk command fails completely...
if (result == null || !result.isSucceeded()) {
if (result == null) {
logEsError("Bulk API action", entry.getKey(), type, null, -1, null);
} else {
logEsError("Bulk API action", entry.getKey(), type, result.getJsonString(), result.getResponseCode(), result.getErrorMessage());
}
// Try and issue the bulk actions individually as a fallback
for (BulkableAction<DocumentResult> action : entry.getValue()) {
executeSingleAction(getJestClient(), action);
}
continue;
}
// Check the return codes of the results
List<BulkResultItem> items = result.getItems();
boolean all404s = true;
for (BulkResultItem item : items) {
if (item.status != 404) {
all404s = false;
break;
}
}
if (all404s) {
if (LOG.isDebugEnabled()) {
LOG.debug("index name " + entry.getKey() + " doesn't exist, creating new index");
}
createIndex(getJestClient(), entry.getKey(), type);
result = getJestClient().execute(bulk);
}
// If the bulk command fails completely...
if (result == null || !result.isSucceeded()) {
if (result == null) {
logEsError("Bulk API action", entry.getKey(), type, null, -1, null);
} else {
logEsError("Bulk API action", entry.getKey(), type, result.getJsonString(), result.getResponseCode(), result.getErrorMessage());
}
// Try and issue the bulk actions individually as a fallback
for (BulkableAction<DocumentResult> action : entry.getValue()) {
executeSingleAction(getJestClient(), action);
}
continue;
}
// Log any unsuccessful completions as errors
for (BulkResultItem item : result.getItems()) {
if (item.status >= 200 && item.status < 300) {
if (LOG.isDebugEnabled()) {
// If debug is enabled, log all completions
logEsDebug(item.operation, entry.getKey(), item.type, "none", item.status, item.error);
}
} else {
logEsError(item.operation, entry.getKey(), item.type, "none", item.status, item.error);
}
}
}
} catch (Throwable ex) {
LOG.error("Unexpected problem sending event to Elasticsearch", ex);
// Shutdown the ES client, it will be recreated as needed
closeJestClient();
}
}
}
}
use of io.searchbox.core.Bulk in project gerrit by GerritCodeReview.
the class ElasticGroupIndex method replace.
@Override
public void replace(AccountGroup group) throws IOException {
Bulk bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(GROUPS).addAction(insert(GROUPS, group)).refresh(true).build();
JestResult result = client.execute(bulk);
if (!result.isSucceeded()) {
throw new IOException(String.format("Failed to replace group %s in index %s: %s", group.getGroupUUID().get(), indexName, result.getErrorMessage()));
}
}
Aggregations