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 (opensearch-project#14146) (opensearch-project#14184)

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

* Issue 12958 | Updated changelog

* Updated changelog with correct PR number

---------

(cherry picked from commit 40f11b3)

Signed-off-by: Parv Kapadia <parvkapadia@gmail.com>
Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
Co-authored-by: parv0201 <parvkapadia@gmail.com>
Signed-off-by: kkewwei <kkewwei@163.com>
  • Loading branch information
2 people authored and kkewwei committed Jul 24, 2024
1 parent 84e2833 commit a2f2d64
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix NPE on restore searchable snapshot ([#13911](https://github.com/opensearch-project/OpenSearch/pull/13911))
- Fix double invocation of postCollection when MultiBucketCollector is present ([#14015](https://github.com/opensearch-project/OpenSearch/pull/14015))
- Fix ReplicaShardBatchAllocator to batch shards without duplicates ([#13710](https://github.com/opensearch-project/OpenSearch/pull/13710))
- 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 a2f2d64

Please sign in to comment.