OpenSearch/docs/java-api
David Pilato e32c7f1d72 Explain how to use bulk processor in a test context
When using a bulk processor in test, you might write something like:

```java
BulkProcessor bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() {
    @Override public void beforeBulk(long executionId, BulkRequest request) {}
    @Override public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {}
    @Override public void afterBulk(long executionId, BulkRequest request, Throwable failure) {}
})
        .setBulkActions(10000)
        .setFlushInterval(TimeValue.timeValueSeconds(10))
        .build();

for (int i = 0; i < 10000; i++) {
    bulkProcessor.add(new IndexRequest("foo", "bar", "doc_" + i)
            .source(jsonBuilder().startObject().field("foo", "bar").endObject()
    ));
}

bulkProcessor.flush();
client.admin().indices().prepareRefresh("foo").get();
SearchResponse response = client.prepareSearch("foo").get();
// response does not contain any hit
```

The problem is that by default bulkProcessor defines the number of concurrent requests to 1 which is using behind the scene an Async BulkRequestHandler.
When you call `flush()` in a test, you expect it to flush all the content of the bulk so you can search for your docs.
But because of the async handling, there is a great chance that none of the documents has been indexed yet when you call the `refresh` method.

We should advice in our Java guide to explicitly set concurrent requests to `0` so users will use behind the scene the Sync BulkRequestHandler.

```java
BulkProcessor bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() {
    @Override public void beforeBulk(long executionId, BulkRequest request) {}
    @Override public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {}
    @Override public void afterBulk(long executionId, BulkRequest request, Throwable failure) {}
})
        .setBulkActions(5000)
        .setFlushInterval(TimeValue.timeValueSeconds(10))
        .setConcurrentRequests(0)
        .build();
```

Closes #22158.
2016-12-16 16:45:56 +01:00
..
admin Update Java API doc for cluster health 2016-06-28 10:13:08 +02:00
aggregations Update script metric aggregation for 5.0 2016-12-14 11:44:10 +01:00
docs Explain how to use bulk processor in a test context 2016-12-16 16:45:56 +01:00
query-dsl Update script query doc for 5.1 2016-12-14 11:36:55 +01:00
aggs.asciidoc removing duplicated parenthese open (#17975) 2016-04-26 20:13:08 +02:00
client.asciidoc minor documentation improvements (#19500) 2016-07-21 14:26:56 +02:00
docs.asciidoc Remove delete-by-query core docs 2015-08-01 05:14:46 -04:00
index.asciidoc Add documentation for Logger with Transport Client 2016-11-11 21:23:23 +01:00
indexed-scripts.asciidoc cutover some docs to painless 2016-06-27 09:55:16 -04:00
query-dsl.asciidoc [doc] Reorganize and clean Java documentation 2015-07-01 22:19:11 +02:00
search.asciidoc Update search template doc for 5.1 2016-12-14 11:36:32 +01:00