Skip to content

Commit

Permalink
Issue 12958 | Updated bulk api for Rest HighLevel Client for respecti…
Browse files Browse the repository at this point in the history
…ng requireAlias flag

Signed-off-by: Parv Kapadia <[email protected]>
  • Loading branch information
parv0201 committed Jun 10, 2024
1 parent 04a417a commit 297ee64
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
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 297ee64

Please sign in to comment.