Search in sources :

Example 1 with BulkRequest

use of org.opensearch.client.opensearch.core.BulkRequest 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);
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) ExistsSourceRequest(org.opensearch.client.opensearch.core.ExistsSourceRequest) GetRequest(org.opensearch.client.opensearch.core.GetRequest) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) DeleteIndexResponse(org.opensearch.client.opensearch.indices.DeleteIndexResponse) BulkRequest(org.opensearch.client.opensearch.core.BulkRequest) MgetResponse(org.opensearch.client.opensearch.core.MgetResponse) BulkOperation(org.opensearch.client.opensearch.core.bulk.BulkOperation) ArrayList(java.util.ArrayList) IndexResponse(org.opensearch.client.opensearch.core.IndexResponse) UpdateRequest(org.opensearch.client.opensearch.core.UpdateRequest) Refresh(org.opensearch.client.opensearch._types.Refresh) UpdateResponse(org.opensearch.client.opensearch.core.UpdateResponse) DateTimeFormat(org.joda.time.format.DateTimeFormat) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) Result(org.opensearch.client.opensearch._types.Result) DateTime(org.joda.time.DateTime) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) DeleteResponse(org.opensearch.client.opensearch.core.DeleteResponse) IOException(java.io.IOException) BulkResponse(org.opensearch.client.opensearch.core.BulkResponse) IndexRequest(org.opensearch.client.opensearch.core.IndexRequest) List(java.util.List) GetResponse(org.opensearch.client.opensearch.core.GetResponse) BulkResponseItem(org.opensearch.client.opensearch.core.bulk.BulkResponseItem) Collections(java.util.Collections) BulkOperation(org.opensearch.client.opensearch.core.bulk.BulkOperation) ArrayList(java.util.ArrayList) BulkResponse(org.opensearch.client.opensearch.core.BulkResponse) BulkRequest(org.opensearch.client.opensearch.core.BulkRequest)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 DateTimeZone (org.joda.time.DateTimeZone)1 DateTimeFormat (org.joda.time.format.DateTimeFormat)1 OpenSearchException (org.opensearch.client.opensearch._types.OpenSearchException)1 Refresh (org.opensearch.client.opensearch._types.Refresh)1 Result (org.opensearch.client.opensearch._types.Result)1 BulkRequest (org.opensearch.client.opensearch.core.BulkRequest)1 BulkResponse (org.opensearch.client.opensearch.core.BulkResponse)1 DeleteResponse (org.opensearch.client.opensearch.core.DeleteResponse)1 ExistsSourceRequest (org.opensearch.client.opensearch.core.ExistsSourceRequest)1 GetRequest (org.opensearch.client.opensearch.core.GetRequest)1 GetResponse (org.opensearch.client.opensearch.core.GetResponse)1 IndexRequest (org.opensearch.client.opensearch.core.IndexRequest)1 IndexResponse (org.opensearch.client.opensearch.core.IndexResponse)1 MgetResponse (org.opensearch.client.opensearch.core.MgetResponse)1 UpdateRequest (org.opensearch.client.opensearch.core.UpdateRequest)1