use of org.elasticsearch.action.update.UpdateRequestBuilder in project elasticsearch by elastic.
the class BulkWithUpdatesIT method testBulkUpdateLargerVolume.
public void testBulkUpdateLargerVolume() throws Exception {
createIndex("test");
ensureGreen();
int numDocs = scaledRandomIntBetween(100, 2000);
if (numDocs % 2 == 1) {
// this test needs an even num of docs
numDocs++;
}
final Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "ctx._source.counter += 1", Collections.emptyMap());
BulkRequestBuilder builder = client().prepareBulk();
for (int i = 0; i < numDocs; i++) {
builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setFields("counter").setScript(script).setUpsert(jsonBuilder().startObject().field("counter", 1).endObject()));
}
BulkResponse response = builder.get();
assertThat(response.hasFailures(), equalTo(false));
assertThat(response.getItems().length, equalTo(numDocs));
for (int i = 0; i < numDocs; i++) {
assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
assertThat(response.getItems()[i].getVersion(), equalTo(1L));
assertThat(response.getItems()[i].getIndex(), equalTo("test"));
assertThat(response.getItems()[i].getType(), equalTo("type1"));
assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
assertThat(response.getItems()[i].getResponse().getId(), equalTo(Integer.toString(i)));
assertThat(response.getItems()[i].getResponse().getVersion(), equalTo(1L));
assertThat(((UpdateResponse) response.getItems()[i].getResponse()).getGetResult().field("counter").getValue(), equalTo(1));
for (int j = 0; j < 5; j++) {
GetResponse getResponse = client().prepareGet("test", "type1", Integer.toString(i)).execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getVersion(), equalTo(1L));
assertThat(((Number) getResponse.getSource().get("counter")).longValue(), equalTo(1L));
}
}
builder = client().prepareBulk();
for (int i = 0; i < numDocs; i++) {
UpdateRequestBuilder updateBuilder = client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setFields("counter");
if (i % 2 == 0) {
updateBuilder.setScript(script);
} else {
updateBuilder.setDoc(jsonBuilder().startObject().field("counter", 2).endObject());
}
if (i % 3 == 0) {
updateBuilder.setRetryOnConflict(3);
}
builder.add(updateBuilder);
}
response = builder.execute().actionGet();
assertThat(response.hasFailures(), equalTo(false));
assertThat(response.getItems().length, equalTo(numDocs));
for (int i = 0; i < numDocs; i++) {
assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
assertThat(response.getItems()[i].getVersion(), equalTo(2L));
assertThat(response.getItems()[i].getIndex(), equalTo("test"));
assertThat(response.getItems()[i].getType(), equalTo("type1"));
assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
assertThat(response.getItems()[i].getResponse().getId(), equalTo(Integer.toString(i)));
assertThat(response.getItems()[i].getResponse().getVersion(), equalTo(2L));
assertThat(((UpdateResponse) response.getItems()[i].getResponse()).getGetResult().field("counter").getValue(), equalTo(2));
}
builder = client().prepareBulk();
int maxDocs = numDocs / 2 + numDocs;
for (int i = (numDocs / 2); i < maxDocs; i++) {
builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setScript(script));
}
response = builder.execute().actionGet();
assertThat(response.hasFailures(), equalTo(true));
assertThat(response.getItems().length, equalTo(numDocs));
for (int i = 0; i < numDocs; i++) {
int id = i + (numDocs / 2);
if (i >= (numDocs / 2)) {
assertThat(response.getItems()[i].getFailure().getId(), equalTo(Integer.toString(id)));
assertThat(response.getItems()[i].getFailure().getMessage(), containsString("document missing"));
} else {
assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(id)));
assertThat(response.getItems()[i].getVersion(), equalTo(3L));
assertThat(response.getItems()[i].getIndex(), equalTo("test"));
assertThat(response.getItems()[i].getType(), equalTo("type1"));
assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
}
}
builder = client().prepareBulk();
for (int i = 0; i < numDocs; i++) {
builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setScript(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "ctx.op = \"none\"", Collections.emptyMap())));
}
response = builder.execute().actionGet();
assertThat(response.buildFailureMessage(), response.hasFailures(), equalTo(false));
assertThat(response.getItems().length, equalTo(numDocs));
for (int i = 0; i < numDocs; i++) {
assertThat(response.getItems()[i].getItemId(), equalTo(i));
assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
assertThat(response.getItems()[i].getIndex(), equalTo("test"));
assertThat(response.getItems()[i].getType(), equalTo("type1"));
assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
}
builder = client().prepareBulk();
for (int i = 0; i < numDocs; i++) {
builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setScript(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "ctx.op = \"delete\"", Collections.emptyMap())));
}
response = builder.execute().actionGet();
assertThat(response.hasFailures(), equalTo(false));
assertThat(response.getItems().length, equalTo(numDocs));
for (int i = 0; i < numDocs; i++) {
assertThat(response.getItems()[i].getItemId(), equalTo(i));
assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
assertThat(response.getItems()[i].getIndex(), equalTo("test"));
assertThat(response.getItems()[i].getType(), equalTo("type1"));
assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
for (int j = 0; j < 5; j++) {
GetResponse getResponse = client().prepareGet("test", "type1", Integer.toString(i)).get();
assertThat(getResponse.isExists(), equalTo(false));
}
}
}
use of org.elasticsearch.action.update.UpdateRequestBuilder in project titan by thinkaurelius.
the class ElasticSearchIndex method mutate.
@Override
public void mutate(Map<String, Map<String, IndexMutation>> mutations, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
BulkRequestBuilder brb = client.prepareBulk();
int bulkrequests = 0;
try {
for (Map.Entry<String, Map<String, IndexMutation>> stores : mutations.entrySet()) {
String storename = stores.getKey();
for (Map.Entry<String, IndexMutation> entry : stores.getValue().entrySet()) {
String docid = entry.getKey();
IndexMutation mutation = entry.getValue();
assert mutation.isConsolidated();
Preconditions.checkArgument(!(mutation.isNew() && mutation.isDeleted()));
Preconditions.checkArgument(!mutation.isNew() || !mutation.hasDeletions());
Preconditions.checkArgument(!mutation.isDeleted() || !mutation.hasAdditions());
//Deletions first
if (mutation.hasDeletions()) {
if (mutation.isDeleted()) {
log.trace("Deleting entire document {}", docid);
brb.add(new DeleteRequest(indexName, storename, docid));
} else {
String script = getDeletionScript(informations, storename, mutation);
brb.add(client.prepareUpdate(indexName, storename, docid).setScript(script, ScriptService.ScriptType.INLINE));
log.trace("Adding script {}", script);
}
bulkrequests++;
}
if (mutation.hasAdditions()) {
int ttl = mutation.determineTTL();
if (mutation.isNew()) {
//Index
log.trace("Adding entire document {}", docid);
brb.add(new IndexRequest(indexName, storename, docid).source(getNewDocument(mutation.getAdditions(), informations.get(storename), ttl)));
} else {
Preconditions.checkArgument(ttl == 0, "Elasticsearch only supports TTL on new documents [%s]", docid);
boolean needUpsert = !mutation.hasDeletions();
String script = getAdditionScript(informations, storename, mutation);
UpdateRequestBuilder update = client.prepareUpdate(indexName, storename, docid).setScript(script, ScriptService.ScriptType.INLINE);
if (needUpsert) {
XContentBuilder doc = getNewDocument(mutation.getAdditions(), informations.get(storename), ttl);
update.setUpsert(doc);
}
brb.add(update);
log.trace("Adding script {}", script);
}
bulkrequests++;
}
}
}
if (bulkrequests > 0) {
BulkResponse bulkItemResponses = brb.execute().actionGet();
if (bulkItemResponses.hasFailures()) {
boolean actualFailure = false;
for (BulkItemResponse response : bulkItemResponses.getItems()) {
//The document may have been deleted, which is OK
if (response.isFailed() && response.getFailure().getStatus() != RestStatus.NOT_FOUND) {
log.error("Failed to execute ES query {}", response.getFailureMessage());
actualFailure = true;
}
}
if (actualFailure) {
throw new Exception(bulkItemResponses.buildFailureMessage());
}
}
}
} catch (Exception e) {
log.error("Failed to execute ES query {}", brb.request().timeout(), e);
throw convert(e);
}
}
use of org.elasticsearch.action.update.UpdateRequestBuilder in project fess by codelibs.
the class EsAbstractBehavior method delegateBatchRequest.
protected <BUILDER> int[] delegateBatchRequest(final List<? extends Entity> entityList, Function<EsAbstractEntity, BUILDER> call) {
@SuppressWarnings("unchecked") final BulkList<? extends Entity, BUILDER> bulkList = (BulkList<? extends Entity, BUILDER>) entityList;
final RequestOptionCall<BUILDER> builderEntityCall = bulkList.getEntityCall();
final BulkRequestBuilder bulkBuilder = client.prepareBulk();
for (final Entity entity : entityList) {
final EsAbstractEntity esEntity = (EsAbstractEntity) entity;
BUILDER builder = call.apply(esEntity);
if (builder instanceof IndexRequestBuilder) {
if (builderEntityCall != null) {
builderEntityCall.callback(builder);
}
bulkBuilder.add((IndexRequestBuilder) builder);
} else if (builder instanceof UpdateRequestBuilder) {
if (builderEntityCall != null) {
builderEntityCall.callback(builder);
}
bulkBuilder.add((UpdateRequestBuilder) builder);
} else if (builder instanceof DeleteRequestBuilder) {
if (builderEntityCall != null) {
builderEntityCall.callback(builder);
}
bulkBuilder.add((DeleteRequestBuilder) builder);
}
}
final RequestOptionCall<BulkRequestBuilder> builderCall = bulkList.getCall();
if (builderCall != null) {
builderCall.callback(bulkBuilder);
}
final BulkResponse response = bulkBuilder.execute().actionGet(bulkTimeout);
final BulkItemResponse[] itemResponses = response.getItems();
if (itemResponses.length != entityList.size()) {
throw new IllegalStateException("Invalid response size: " + itemResponses.length + " != " + entityList.size());
}
final int[] results = new int[itemResponses.length];
for (int i = 0; i < itemResponses.length; i++) {
final BulkItemResponse itemResponse = itemResponses[i];
final Entity entity = entityList.get(i);
if (entity instanceof EsAbstractEntity) {
((EsAbstractEntity) entity).asDocMeta().id(itemResponse.getId());
}
results[i] = itemResponse.isFailed() ? 0 : 1;
}
return results;
}
use of org.elasticsearch.action.update.UpdateRequestBuilder in project fess by codelibs.
the class SearchService method update.
public boolean update(final String id, final Consumer<UpdateRequestBuilder> builderLambda) {
try {
final UpdateRequestBuilder builder = fessEsClient.prepareUpdate(fessConfig.getIndexDocumentUpdateIndex(), fessConfig.getIndexDocumentType(), id);
builderLambda.accept(builder);
final UpdateResponse response = builder.execute().actionGet(fessConfig.getIndexIndexTimeout());
return response.getResult() == Result.CREATED || response.getResult() == Result.UPDATED;
} catch (final ElasticsearchException e) {
throw new FessEsClientException("Failed to update doc " + id, e);
}
}
Aggregations