Skip to content

Commit 40f11b3

Browse files
authoredJun 11, 2024
Java high-level REST client bulk() is not respecting the bulkRequest.requireAlias(true) method call (opensearch-project#14146)
* Issue 12958 | Updated bulk api for Rest HighLevel Client for respecting requireAlias flag Signed-off-by: Parv Kapadia <parvkapadia@gmail.com> * Issue 12958 | Updated changelog Signed-off-by: Parv Kapadia <parvkapadia@gmail.com> * Updated changelog with correct PR number Signed-off-by: Parv Kapadia <parvkapadia@gmail.com> --------- Signed-off-by: Parv Kapadia <parvkapadia@gmail.com>
1 parent f2ed7d6 commit 40f11b3

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
 

‎CHANGELOG-3.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
4747
- Fix 'org.apache.hc.core5.http.ParseException: Invalid protocol version' under JDK 16+ ([#4827](https://github.com/opensearch-project/OpenSearch/pull/4827))
4848
- Fix compression support for h2c protocol ([#4944](https://github.com/opensearch-project/OpenSearch/pull/4944))
4949
- Don't over-allocate in HeapBufferedAsyncEntityConsumer in order to consume the response ([#9993](https://github.com/opensearch-project/OpenSearch/pull/9993))
50+
- Java high-level REST client bulk() is not respecting the bulkRequest.requireAlias(true) method call ([#14146](https://github.com/opensearch-project/OpenSearch/pull/14146))
5051

5152
### Security
5253

‎client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java

+7
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
154154
parameters.withRefreshPolicy(bulkRequest.getRefreshPolicy());
155155
parameters.withPipeline(bulkRequest.pipeline());
156156
parameters.withRouting(bulkRequest.routing());
157+
if (bulkRequest.requireAlias() != null) {
158+
parameters.withRequireAlias(bulkRequest.requireAlias());
159+
}
157160
// Bulk API only supports newline delimited JSON or Smile. Before executing
158161
// the bulk, we need to check that all requests have the same content-type
159162
// and this content-type is supported by the Bulk API.
@@ -232,6 +235,10 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
232235
metadata.field("_source", updateRequest.fetchSource());
233236
}
234237
}
238+
239+
if (action.isRequireAlias()) {
240+
metadata.field("require_alias", action.isRequireAlias());
241+
}
235242
metadata.endObject();
236243
}
237244
metadata.endObject();

‎client/rest-high-level/src/test/java/org/opensearch/client/CrudIT.java

+57
Original file line numberDiff line numberDiff line change
@@ -1299,4 +1299,61 @@ public void testMultiTermvectors() throws IOException {
12991299
}
13001300
}
13011301
}
1302+
1303+
public void testBulkWithRequireAlias() throws IOException {
1304+
{
1305+
String indexAliasName = "testindex-1";
1306+
1307+
BulkRequest bulkRequest = new BulkRequest(indexAliasName);
1308+
bulkRequest.requireAlias(true);
1309+
bulkRequest.add(new IndexRequest().id("1").source("{ \"name\": \"Biden\" }", XContentType.JSON));
1310+
bulkRequest.add(new IndexRequest().id("2").source("{ \"name\": \"Trump\" }", XContentType.JSON));
1311+
1312+
BulkResponse bulkResponse = execute(bulkRequest, highLevelClient()::bulk, highLevelClient()::bulkAsync, RequestOptions.DEFAULT);
1313+
1314+
assertFalse("Should not auto-create the '" + indexAliasName + "' index.", indexExists(indexAliasName));
1315+
assertTrue("Bulk response must have failures.", bulkResponse.hasFailures());
1316+
}
1317+
{
1318+
String indexAliasName = "testindex-2";
1319+
1320+
BulkRequest bulkRequest = new BulkRequest();
1321+
bulkRequest.requireAlias(true);
1322+
bulkRequest.add(new IndexRequest().index(indexAliasName).id("1").source("{ \"name\": \"Biden\" }", XContentType.JSON));
1323+
bulkRequest.add(new IndexRequest().index(indexAliasName).id("2").source("{ \"name\": \"Trump\" }", XContentType.JSON));
1324+
1325+
BulkResponse bulkResponse = execute(bulkRequest, highLevelClient()::bulk, highLevelClient()::bulkAsync, RequestOptions.DEFAULT);
1326+
1327+
assertFalse("Should not auto-create the '" + indexAliasName + "' index.", indexExists(indexAliasName));
1328+
assertTrue("Bulk response must have failures.", bulkResponse.hasFailures());
1329+
}
1330+
{
1331+
String indexAliasName = "testindex-3";
1332+
1333+
BulkRequest bulkRequest = new BulkRequest(indexAliasName);
1334+
bulkRequest.add(new IndexRequest().id("1").setRequireAlias(true).source("{ \"name\": \"Biden\" }", XContentType.JSON));
1335+
bulkRequest.add(new IndexRequest().id("2").setRequireAlias(true).source("{ \"name\": \"Trump\" }", XContentType.JSON));
1336+
1337+
BulkResponse bulkResponse = execute(bulkRequest, highLevelClient()::bulk, highLevelClient()::bulkAsync, RequestOptions.DEFAULT);
1338+
1339+
assertFalse("Should not auto-create the '" + indexAliasName + "' index.", indexExists(indexAliasName));
1340+
assertTrue("Bulk response must have failures.", bulkResponse.hasFailures());
1341+
}
1342+
{
1343+
String indexAliasName = "testindex-4";
1344+
1345+
BulkRequest bulkRequest = new BulkRequest();
1346+
bulkRequest.add(
1347+
new IndexRequest().index(indexAliasName).id("1").setRequireAlias(true).source("{ \"name\": \"Biden\" }", XContentType.JSON)
1348+
);
1349+
bulkRequest.add(
1350+
new IndexRequest().index(indexAliasName).id("2").setRequireAlias(true).source("{ \"name\": \"Trump\" }", XContentType.JSON)
1351+
);
1352+
1353+
BulkResponse bulkResponse = execute(bulkRequest, highLevelClient()::bulk, highLevelClient()::bulkAsync, RequestOptions.DEFAULT);
1354+
1355+
assertFalse("Should not auto-create the '" + indexAliasName + "' index.", indexExists(indexAliasName));
1356+
assertTrue("Bulk response must have failures.", bulkResponse.hasFailures());
1357+
}
1358+
}
13021359
}

0 commit comments

Comments
 (0)