# Create bulkRequest using latest java API client(8.1.1) for list of products using builder lambda method

**URL:** <https://discuss.elastic.co/t/create-bulkrequest-using-latest-java-api-client-8-1-1-for-list-of-products-using-builder-lambda-method/301573>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 5, 2022, 2:16am UTC](https://discuss.elastic.co/t/create-bulkrequest-using-latest-java-api-client-8-1-1-for-list-of-products-using-builder-lambda-method/301573 "2022-04-05T02:16:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amitmbm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amitmbm/32/102111_2.png) [@amitmbm](https://discuss.elastic.co/u/amitmbm)\
**Post date:** [April 5, 2022, 2:16am UTC](https://discuss.elastic.co/t/create-bulkrequest-using-latest-java-api-client-8-1-1-for-list-of-products-using-builder-lambda-method/301573/1 "2022-04-05T02:16:13Z")

</div>

I am having a list of Products(for simplicity let's assume it has an id, brand, and name properties), and I want to index this list of products to Elasticsearch using java API client version 8.1.1.

BulkRequest in java api-client, accepts a List, that makes sense and there are three different overloaded methods to pass the list of bulkoperations, like below

Using lambda builder

```auto
 /**
         * Required - Request body.
         * <p>
         * API name: {@code _value_body}
         * <p>
         * Adds a value to <code>operations</code> using a builder lambda.
         */
        public final Builder operations(Function<BulkOperation.Builder, ObjectBuilder<BulkOperation>> fn) {
            return operations(fn.apply(new BulkOperation.Builder()).build());
        }

```

And using List

```auto
 public final Builder operations(List<BulkOperation> list) {
            this.operations = _listAddAll(this.operations, list);
            return this;
        }

```

I was able to bulk index my products using the second simple method that accepts list but not able to do the same using the builder lambda which is preferred as it would be more concise and more readable.

P.S-\> created [this post on Stackoverflow](https://stackoverflow.com/questions/71741109/use-elements-in-a-sub-stream-and-using-each-element-of-the-stream-create-anoth) but no luck so far.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 5, 2022, 7:02am UTC](https://discuss.elastic.co/t/create-bulkrequest-using-latest-java-api-client-8-1-1-for-list-of-products-using-builder-lambda-method/301573/2 "2022-04-05T07:02:02Z")

</div>

Hey,

take a look at [GitHub - spinscale/elasticsearch-rest-client-samples: Elasticsearch REST client samples using Testcontainers](https://github.com/spinscale/elasticsearch-rest-client-samples) - my sample repo using the new Elasticsearch Java Client. There is a snippet using bulk at [elasticsearch-rest-client-samples/ProductServiceImpl.java at main · spinscale/elasticsearch-rest-client-samples · GitHub](https://github.com/spinscale/elasticsearch-rest-client-samples/blob/main/src/main/java/de/spinscale/restclient/ProductServiceImpl.java#L73-L94)

Hope this helps!

--Alex

---

<div class="post-metadata">

**Author:** ![amitmbm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amitmbm/32/102111_2.png) [@amitmbm](https://discuss.elastic.co/u/amitmbm)\
**Post date:** [April 5, 2022, 9:26am UTC](https://discuss.elastic.co/t/create-bulkrequest-using-latest-java-api-client-8-1-1-for-list-of-products-using-builder-lambda-method/301573/3 "2022-04-05T09:26:53Z")

</div>

Thanks @spinscale your code is doing exactly what I need, but I have few questions,

I am using my own class named as ElasticsearchClient, which uses the Elasticsearch provided API client, and there I defined bulkRequest using below method signature, to make sure that call to Elasticsearch is in a single class and not spread across my application.

```auto
public BulkResponse bulkIndex(BulkRequest request) {
         esClient.bulk(request);
        // Do other stuff 
}

```

Now, as you can see this method accepts the BulkRequest object and not the builder, and your method is directly calling the bulk with the builder lambda, I tried creating the bulkRequest using your code, but couldn't do it.

1. Can we get rid of foreach and use the stream on products?

---

<div class="post-metadata">

**Author:** ![amitmbm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amitmbm/32/102111_2.png) [@amitmbm](https://discuss.elastic.co/u/amitmbm)\
**Post date:** [April 7, 2022, 5:18am UTC](https://discuss.elastic.co/t/create-bulkrequest-using-latest-java-api-client-8-1-1-for-list-of-products-using-builder-lambda-method/301573/4 "2022-04-07T05:18:30Z")

</div>

@spinscale How are you? Did you get a chance to look at my previous reply.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 7, 2022, 7:11am UTC](https://discuss.elastic.co/t/create-bulkrequest-using-latest-java-api-client-8-1-1-for-list-of-products-using-builder-lambda-method/301573/5 "2022-04-07T07:11:40Z")

</div>

Hey,

I'm sorry, but I cannot make too much out of that question. That does not look like a client implementation detail to me, but rather like implementing a proper abstraction in your java code.

Please try to rephrase your question, and I hope understand then 🙂

Thanks!

--Alex

---

<div class="post-metadata">

**Author:** ![amitmbm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amitmbm/32/102111_2.png) [@amitmbm](https://discuss.elastic.co/u/amitmbm)\
**Post date:** [April 7, 2022, 10:49am UTC](https://discuss.elastic.co/t/create-bulkrequest-using-latest-java-api-client-8-1-1-for-list-of-products-using-builder-lambda-method/301573/6 "2022-04-07T10:49:49Z")

</div>

Thanks @spinscale , I would explain it in detail shortly..

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 5, 2022, 10:50am UTC](https://discuss.elastic.co/t/create-bulkrequest-using-latest-java-api-client-8-1-1-for-list-of-products-using-builder-lambda-method/301573/7 "2022-05-05T10:50:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
