# Bulk / Batch Documents Indexing Examples / Support?

**URL:** <https://forum.opensearch.org/t/bulk-batch-documents-indexing-examples-support/7697>\
**Category:** OpenSearch Client Libraries\
**Tags:** opensearch-java\
**Created:** [November 20, 2021, 2:40am UTC](https://forum.opensearch.org/t/bulk-batch-documents-indexing-examples-support/7697 "2021-11-20T02:40:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![meremortal](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@meremortal](https://forum.opensearch.org/u/meremortal)\
**Post date:** [November 20, 2021, 2:40am UTC](https://forum.opensearch.org/t/bulk-batch-documents-indexing-examples-support/7697/1 "2021-11-20T02:40:03Z")

</div>

I’m working on a project that need to support indexing documents using the opensearch-java client, but I’ve only found very limited examples for indexing documents that look like this…

```auto
    // Index some data
    IndexData indexData = new IndexData("John", "Smith");
    IndexRequest<IndexData> indexRequest = new IndexRequest.Builder<IndexData>().index(index).id("1").value(indexData).build();
    client.index(indexRequest);

```

Given the way this example is written, this approach will send documents to OpenSearch one at a time, with all the overhead / handshaking costs for each request.

What I haven’t been able to find are examples using opensearch-java to send multiple documents in bulk or batches. I’ve tried to figure out how to do it using BulkRequest, OpenSearchClient index()/bulk() methods but I haven’t yet been able to sort it out.

Are there any examples somewhere I am missing that show how to do it using opensearch-java? I even went back to older Elasticsearch examples and the client is just different enough that I’ve not been able to figure out the proper incantations.

For reference, I’m working on a class that has a `OpenSearchClient` member variable called `client` and an abstract method named `sendToIndex` has the following signature…

```auto
protected abstract void sendToIndex(List<Document> documents) throws Exception;

```

The `Document` class is specific to our codebase (not OpenSearch specific) but has a `asMap()` method that can return `Map<String,Object>` type.

For now I’m just gonna make the code work by constructing an `IndexRequest` and calling `client.index(indexRequest)` for each and every `Document` object, but I’m concerned about indexing throughput of sending one document at a time vs 50 or 200 or so.

Help or guidance on the opensearch-java way of sending multiple documents for indexing per request would be greatly appreciated… Thanks!

-Michael

---

<div class="post-metadata">

**Author:** ![asfoorial](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@asfoorial](https://forum.opensearch.org/u/asfoorial)\
**Post date:** [November 20, 2021, 7:48am UTC](https://forum.opensearch.org/t/bulk-batch-documents-indexing-examples-support/7697/2 "2021-11-20T07:48:54Z")

</div>

Adding to this question, does java client support everything provided by the Java highlevel REST client? Including bulk operations, alias, update\_by\_query etc…

---

<div class="post-metadata">

**Author:** ![meremortal](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@meremortal](https://forum.opensearch.org/u/meremortal)\
**Post date:** [November 20, 2021, 12:07pm UTC](https://forum.opensearch.org/t/bulk-batch-documents-indexing-examples-support/7697/3 "2021-11-20T12:07:45Z")

</div>

Well, that’s an interesting question because the OpenSearch website seems to discourage use of the high-level rest client in favor of the java client, which is why we’re trying to use it, but does high level rest client it have better / clear examples for doing bulk indexing?

---

<div class="post-metadata">

**Author:** ![eugenewilliams](https://avatars.discourse-cdn.com/v4/letter/e/e9c0ed/32.png) [@eugenewilliams](https://forum.opensearch.org/u/eugenewilliams)\
**Post date:** [October 16, 2023, 6:11am UTC](https://forum.opensearch.org/t/bulk-batch-documents-indexing-examples-support/7697/4 "2023-10-16T06:11:19Z")

</div>

I stumbled upon this and was disappointed that the question went stale for two years.  
Adding the solution that I found for googlers (and the LLMs indexing information - GPT-5?) that needs training on the answer - credit to: [Bulk: indexing multiple documents | Elasticsearch Java API Client [8.10] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/indexing-bulk.html)

Without this, I estimated that it would take 10 days to get all our documents uploaded to OpenSearch (AWS)

```auto
List<Product> products = fetchProducts();

BulkRequest.Builder br = new BulkRequest.Builder();

for (Product product : products) {
    br.operations(op -> op           
        .index(idx -> idx            
            .index("products")       
            .id(product.getSku())
            .document(product)
        )
    );
}

BulkResponse result = esClient.bulk(br.build());

// Log errors, if any
if (result.errors()) {
    logger.error("Bulk had errors");
    for (BulkResponseItem item: result.items()) {
        if (item.error() != null) {
            logger.error(item.error().reason());
        }
    }
}

```

Instead of 10 days, this took 10 minutes
