Search in sources :

Example 1 with BulkResponse

use of org.opensearch.client.opensearch.core.BulkResponse 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)

Example 2 with BulkResponse

use of org.opensearch.client.opensearch.core.BulkResponse in project opensearch-java by opensearch-project.

the class RequestTest method testBulkRequest.

@Test
public void testBulkRequest() throws IOException {
    AppData appData = new AppData();
    appData.setIntValue(42);
    appData.setMsg("Some message");
    BulkResponse bulk = highLevelClient().bulk(_0 -> _0.operations(_1 -> _1.create(_2 -> _2.index("foo").id("abc").document(appData))).operations(_1 -> _1.create(_2 -> _2.index("foo").id("def").document(appData))));
    assertFalse(bulk.errors());
    assertEquals(2, bulk.items().size());
    assertEquals(OperationType.Create, bulk.items().get(0).operationType());
    assertEquals("foo", bulk.items().get(0).index());
    assertEquals(1L, bulk.items().get(0).version().longValue());
    assertEquals("foo", bulk.items().get(1).index());
    assertEquals(1L, bulk.items().get(1).version().longValue());
    assertEquals(42, highLevelClient().get(b -> b.index("foo").id("abc"), AppData.class).source().getIntValue());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) GetMappingResponse(org.opensearch.client.opensearch.indices.GetMappingResponse) HistogramAggregate(org.opensearch.client.opensearch._types.aggregations.HistogramAggregate) OperationType(org.opensearch.client.opensearch.core.bulk.OperationType) Property(org.opensearch.client.opensearch._types.mapping.Property) IndexResponse(org.opensearch.client.opensearch.core.IndexResponse) Map(java.util.Map) Refresh(org.opensearch.client.opensearch._types.Refresh) GetIndexResponse(org.opensearch.client.opensearch.indices.GetIndexResponse) OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) RequestItem(org.opensearch.client.opensearch.core.msearch.RequestItem) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) IndexState(org.opensearch.client.opensearch.indices.IndexState) Test(org.junit.Test) IOException(java.io.IOException) BulkResponse(org.opensearch.client.opensearch.core.BulkResponse) SearchResponse(org.opensearch.client.opensearch.core.SearchResponse) ClearScrollResponse(org.opensearch.client.opensearch.core.ClearScrollResponse) MsearchResponse(org.opensearch.client.opensearch.core.MsearchResponse) GetIndicesSettingsResponse(org.opensearch.client.opensearch.indices.GetIndicesSettingsResponse) ModelTestCase(org.opensearch.client.opensearch.model.ModelTestCase) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) NodesResponse(org.opensearch.client.opensearch.cat.NodesResponse) GetResponse(org.opensearch.client.opensearch.core.GetResponse) Collections(java.util.Collections) BulkResponse(org.opensearch.client.opensearch.core.BulkResponse) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)2 Collections (java.util.Collections)2 OpenSearchException (org.opensearch.client.opensearch._types.OpenSearchException)2 Refresh (org.opensearch.client.opensearch._types.Refresh)2 BulkResponse (org.opensearch.client.opensearch.core.BulkResponse)2 GetResponse (org.opensearch.client.opensearch.core.GetResponse)2 IndexResponse (org.opensearch.client.opensearch.core.IndexResponse)2 CreateIndexResponse (org.opensearch.client.opensearch.indices.CreateIndexResponse)2 BooleanResponse (org.opensearch.client.transport.endpoints.BooleanResponse)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 DateTime (org.joda.time.DateTime)1 DateTimeZone (org.joda.time.DateTimeZone)1 DateTimeFormat (org.joda.time.format.DateTimeFormat)1 Test (org.junit.Test)1 OpenSearchAsyncClient (org.opensearch.client.opensearch.OpenSearchAsyncClient)1