use of org.opensearch.client.opensearch.core.bulk.BulkOperation in project opensearch-java by opensearch-project.
the class BulkRequest method serialize.
/**
* Serialize this value to JSON.
*/
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
generator.writeStartArray();
for (BulkOperation item0 : this.operations) {
item0.serialize(generator, mapper);
}
generator.writeEnd();
}
use of org.opensearch.client.opensearch.core.bulk.BulkOperation in project opensearch-java by opensearch-project.
the class CrudIT method testBulk.
public void testBulk() throws IOException {
AppData appData = new AppData();
appData.setIntValue(1337);
appData.setMsg("foo");
int nbItems = randomIntBetween(10, 100);
boolean[] errors = new boolean[nbItems];
List<BulkOperation> opsList = new ArrayList<>();
for (int i = 0; i < nbItems; i++) {
String id = String.valueOf(i);
boolean erroneous = randomBoolean();
errors[i] = erroneous;
BulkOperation.Kind opType = randomFrom(BulkOperation.Kind.Delete, BulkOperation.Kind.Index, BulkOperation.Kind.Create);
if (opType.equals(BulkOperation.Kind.Delete)) {
if (!erroneous) {
assertEquals(Result.Created, highLevelClient().index(b -> b.index("index").id(id).document(appData)).result());
}
BulkOperation op = new BulkOperation.Builder().delete(d -> d.index("index").id(id)).build();
opsList.add(op);
} else {
appData.setIntValue(i);
appData.setMsg("id");
if (opType.equals(BulkOperation.Kind.Index)) {
BulkOperation op = new BulkOperation.Builder().index(b -> b.index("index").id(id).document(appData).ifSeqNo(erroneous ? 12L : null).ifPrimaryTerm(erroneous ? 12L : null)).build();
opsList.add(op);
} else if (opType.equals(BulkOperation.Kind.Create)) {
BulkOperation op = new BulkOperation.Builder().create(o -> o.index("index").id(id).document(appData)).build();
opsList.add(op);
if (erroneous) {
assertEquals(Result.Created, highLevelClient().index(b -> b.index("index").id(id).document(appData)).result());
}
} else if (opType.equals(BulkOperation.Kind.Update)) {
BulkOperation op = new BulkOperation.Builder().update(o -> o.index("index").id(id).document(Collections.singletonMap("key", "value"))).build();
opsList.add(op);
if (!erroneous) {
assertEquals(Result.Created, highLevelClient().index(b -> b.index("index").id(id).document(appData)).result());
}
}
}
}
BulkRequest bulkRequest = new BulkRequest.Builder().operations(opsList).build();
BulkResponse bulkResponse = highLevelClient().bulk(bulkRequest);
assertTrue(bulkResponse.took() > 0);
assertEquals(nbItems, bulkResponse.items().size());
validateBulkResponses(nbItems, errors, bulkResponse, bulkRequest);
}
use of org.opensearch.client.opensearch.core.bulk.BulkOperation in project opensearch-java by opensearch-project.
the class CrudIT method validateBulkResponses.
private void validateBulkResponses(int nbItems, boolean[] errors, BulkResponse bulkResponse, BulkRequest bulkRequest) {
for (int i = 0; i < nbItems; i++) {
BulkResponseItem bulkResponseItem = bulkResponse.items().get(i);
assertEquals("index", bulkResponseItem.index());
assertEquals("_doc", bulkResponseItem.type());
assertEquals(String.valueOf(i), bulkResponseItem.id());
BulkOperation bulkOperation = bulkRequest.operations().get(i);
if (bulkOperation.isIndex() || bulkOperation.isCreate()) {
assertEquals(errors[i] ? 409 : 201, bulkResponseItem.status());
} else if (bulkOperation.isUpdate()) {
assertEquals(errors[i] ? Result.NotFound.jsonValue() : Result.Updated.jsonValue(), bulkResponseItem.result());
assertEquals(errors[i] ? 404 : 200, bulkResponseItem.status());
} else if (bulkOperation.isDelete()) {
assertEquals(errors[i] ? Result.NotFound.jsonValue() : Result.Deleted.jsonValue(), bulkResponseItem.result());
assertEquals(errors[i] ? 404 : 200, bulkResponseItem.status());
}
}
}
Aggregations