Skip to content

Commit

Permalink
Java high-level REST client bulk() is not respecting the bulkRequest.…
Browse files Browse the repository at this point in the history
…requireAlias(true) method call (#14146)

* Issue 12958 | Updated bulk api for Rest HighLevel Client for respecting requireAlias flag

Signed-off-by: Parv Kapadia <[email protected]>

* Issue 12958 | Updated changelog

Signed-off-by: Parv Kapadia <[email protected]>

* Updated changelog with correct PR number

Signed-off-by: Parv Kapadia <[email protected]>

---------

Signed-off-by: Parv Kapadia <[email protected]>
  • Loading branch information
parv0201 authored Jun 11, 2024
1 parent f2ed7d6 commit 40f11b3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix 'org.apache.hc.core5.http.ParseException: Invalid protocol version' under JDK 16+ ([#4827](https://github.com/opensearch-project/OpenSearch/pull/4827))
- Fix compression support for h2c protocol ([#4944](https://github.com/opensearch-project/OpenSearch/pull/4944))
- Don't over-allocate in HeapBufferedAsyncEntityConsumer in order to consume the response ([#9993](https://github.com/opensearch-project/OpenSearch/pull/9993))
- Java high-level REST client bulk() is not respecting the bulkRequest.requireAlias(true) method call ([#14146](https://github.com/opensearch-project/OpenSearch/pull/14146))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
parameters.withRefreshPolicy(bulkRequest.getRefreshPolicy());
parameters.withPipeline(bulkRequest.pipeline());
parameters.withRouting(bulkRequest.routing());
if (bulkRequest.requireAlias() != null) {
parameters.withRequireAlias(bulkRequest.requireAlias());
}
// Bulk API only supports newline delimited JSON or Smile. Before executing
// the bulk, we need to check that all requests have the same content-type
// and this content-type is supported by the Bulk API.
Expand Down Expand Up @@ -232,6 +235,10 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
metadata.field("_source", updateRequest.fetchSource());
}
}

if (action.isRequireAlias()) {
metadata.field("require_alias", action.isRequireAlias());
}
metadata.endObject();
}
metadata.endObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1299,4 +1299,61 @@ public void testMultiTermvectors() throws IOException {
}
}
}

public void testBulkWithRequireAlias() throws IOException {
{
String indexAliasName = "testindex-1";

BulkRequest bulkRequest = new BulkRequest(indexAliasName);
bulkRequest.requireAlias(true);
bulkRequest.add(new IndexRequest().id("1").source("{ \"name\": \"Biden\" }", XContentType.JSON));
bulkRequest.add(new IndexRequest().id("2").source("{ \"name\": \"Trump\" }", XContentType.JSON));

BulkResponse bulkResponse = execute(bulkRequest, highLevelClient()::bulk, highLevelClient()::bulkAsync, RequestOptions.DEFAULT);

assertFalse("Should not auto-create the '" + indexAliasName + "' index.", indexExists(indexAliasName));
assertTrue("Bulk response must have failures.", bulkResponse.hasFailures());
}
{
String indexAliasName = "testindex-2";

BulkRequest bulkRequest = new BulkRequest();
bulkRequest.requireAlias(true);
bulkRequest.add(new IndexRequest().index(indexAliasName).id("1").source("{ \"name\": \"Biden\" }", XContentType.JSON));
bulkRequest.add(new IndexRequest().index(indexAliasName).id("2").source("{ \"name\": \"Trump\" }", XContentType.JSON));

BulkResponse bulkResponse = execute(bulkRequest, highLevelClient()::bulk, highLevelClient()::bulkAsync, RequestOptions.DEFAULT);

assertFalse("Should not auto-create the '" + indexAliasName + "' index.", indexExists(indexAliasName));
assertTrue("Bulk response must have failures.", bulkResponse.hasFailures());
}
{
String indexAliasName = "testindex-3";

BulkRequest bulkRequest = new BulkRequest(indexAliasName);
bulkRequest.add(new IndexRequest().id("1").setRequireAlias(true).source("{ \"name\": \"Biden\" }", XContentType.JSON));
bulkRequest.add(new IndexRequest().id("2").setRequireAlias(true).source("{ \"name\": \"Trump\" }", XContentType.JSON));

BulkResponse bulkResponse = execute(bulkRequest, highLevelClient()::bulk, highLevelClient()::bulkAsync, RequestOptions.DEFAULT);

assertFalse("Should not auto-create the '" + indexAliasName + "' index.", indexExists(indexAliasName));
assertTrue("Bulk response must have failures.", bulkResponse.hasFailures());
}
{
String indexAliasName = "testindex-4";

BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(
new IndexRequest().index(indexAliasName).id("1").setRequireAlias(true).source("{ \"name\": \"Biden\" }", XContentType.JSON)
);
bulkRequest.add(
new IndexRequest().index(indexAliasName).id("2").setRequireAlias(true).source("{ \"name\": \"Trump\" }", XContentType.JSON)
);

BulkResponse bulkResponse = execute(bulkRequest, highLevelClient()::bulk, highLevelClient()::bulkAsync, RequestOptions.DEFAULT);

assertFalse("Should not auto-create the '" + indexAliasName + "' index.", indexExists(indexAliasName));
assertTrue("Bulk response must have failures.", bulkResponse.hasFailures());
}
}
}

0 comments on commit 40f11b3

Please sign in to comment.