use of org.opensearch.action.bulk.BulkRequest in project OpenSearch by opensearch-project.
the class IngestServiceTests method testExecuteBulkPipelineDoesNotExist.
public void testExecuteBulkPipelineDoesNotExist() {
IngestService ingestService = createWithProcessors(Collections.singletonMap("mock", (factories, tag, description, config) -> mockCompoundProcessor()));
PutPipelineRequest putRequest = new PutPipelineRequest("_id", new BytesArray("{\"processors\": [{\"mock\" : {}}]}"), XContentType.JSON);
// Start empty
ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build();
ClusterState previousClusterState = clusterState;
clusterState = IngestService.innerPut(putRequest, clusterState);
ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
BulkRequest bulkRequest = new BulkRequest();
IndexRequest indexRequest1 = new IndexRequest("_index").id("_id1").source(emptyMap()).setPipeline("_none").setFinalPipeline("_none");
bulkRequest.add(indexRequest1);
IndexRequest indexRequest2 = new IndexRequest("_index").id("_id2").source(emptyMap()).setPipeline("_id").setFinalPipeline("_none");
bulkRequest.add(indexRequest2);
IndexRequest indexRequest3 = new IndexRequest("_index").id("_id3").source(emptyMap()).setPipeline("does_not_exist").setFinalPipeline("_none");
bulkRequest.add(indexRequest3);
@SuppressWarnings("unchecked") BiConsumer<Integer, Exception> failureHandler = mock(BiConsumer.class);
@SuppressWarnings("unchecked") final BiConsumer<Thread, Exception> completionHandler = mock(BiConsumer.class);
ingestService.executeBulkRequest(bulkRequest.numberOfActions(), bulkRequest.requests(), failureHandler, completionHandler, indexReq -> {
}, Names.WRITE);
verify(failureHandler, times(1)).accept(argThat((Integer item) -> item == 2), argThat((IllegalArgumentException iae) -> "pipeline with id [does_not_exist] does not exist".equals(iae.getMessage())));
verify(completionHandler, times(1)).accept(Thread.currentThread(), null);
}
use of org.opensearch.action.bulk.BulkRequest in project OpenSearch by opensearch-project.
the class IngestServiceTests method testBulkRequestExecution.
public void testBulkRequestExecution() throws Exception {
BulkRequest bulkRequest = new BulkRequest();
String pipelineId = "_id";
// Test to make sure that ingest respects content types other than the default index content type
XContentType xContentType = randomFrom(Arrays.stream(XContentType.values()).filter(t -> Requests.INDEX_CONTENT_TYPE.equals(t) == false).collect(Collectors.toList()));
logger.info("Using [{}], not randomly determined default [{}]", xContentType, Requests.INDEX_CONTENT_TYPE);
int numRequest = scaledRandomIntBetween(8, 64);
for (int i = 0; i < numRequest; i++) {
IndexRequest indexRequest = new IndexRequest("_index").id("_id").setPipeline(pipelineId).setFinalPipeline("_none");
indexRequest.source(xContentType, "field1", "value1");
bulkRequest.add(indexRequest);
}
final Processor processor = mock(Processor.class);
when(processor.getType()).thenReturn("mock");
when(processor.getTag()).thenReturn("mockTag");
doAnswer(args -> {
@SuppressWarnings("unchecked") BiConsumer<IngestDocument, Exception> handler = (BiConsumer) args.getArguments()[1];
handler.accept(RandomDocumentPicks.randomIngestDocument(random()), null);
return null;
}).when(processor).execute(any(), any());
Map<String, Processor.Factory> map = new HashMap<>(2);
map.put("mock", (factories, tag, description, config) -> processor);
IngestService ingestService = createWithProcessors(map);
PutPipelineRequest putRequest = new PutPipelineRequest("_id", new BytesArray("{\"processors\": [{\"mock\": {}}], \"description\": \"_description\"}"), XContentType.JSON);
ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build();
ClusterState previousClusterState = clusterState;
clusterState = IngestService.innerPut(putRequest, clusterState);
ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
@SuppressWarnings("unchecked") BiConsumer<Integer, Exception> requestItemErrorHandler = mock(BiConsumer.class);
@SuppressWarnings("unchecked") final BiConsumer<Thread, Exception> completionHandler = mock(BiConsumer.class);
ingestService.executeBulkRequest(numRequest, bulkRequest.requests(), requestItemErrorHandler, completionHandler, indexReq -> {
}, Names.WRITE);
verify(requestItemErrorHandler, never()).accept(any(), any());
verify(completionHandler, times(1)).accept(Thread.currentThread(), null);
for (DocWriteRequest<?> docWriteRequest : bulkRequest.requests()) {
IndexRequest indexRequest = TransportBulkAction.getIndexWriteRequest(docWriteRequest);
assertThat(indexRequest, notNullValue());
assertThat(indexRequest.getContentType(), equalTo(xContentType));
}
}
use of org.opensearch.action.bulk.BulkRequest in project OpenSearch by opensearch-project.
the class RestBulkActionTests method testBulkPipelineUpsert.
public void testBulkPipelineUpsert() throws Exception {
SetOnce<Boolean> bulkCalled = new SetOnce<>();
try (NodeClient verifyingClient = new NoOpNodeClient(this.getTestName()) {
@Override
public void bulk(BulkRequest request, ActionListener<BulkResponse> listener) {
bulkCalled.set(true);
assertThat(request.requests(), hasSize(2));
UpdateRequest updateRequest = (UpdateRequest) request.requests().get(1);
assertThat(updateRequest.upsertRequest().getPipeline(), equalTo("timestamps"));
}
}) {
final Map<String, String> params = new HashMap<>();
params.put("pipeline", "timestamps");
new RestBulkAction(settings(Version.CURRENT).build()).handleRequest(new FakeRestRequest.Builder(xContentRegistry()).withPath("my_index/_bulk").withParams(params).withContent(new BytesArray("{\"index\":{\"_id\":\"1\"}}\n" + "{\"field1\":\"val1\"}\n" + "{\"update\":{\"_id\":\"2\"}}\n" + "{\"script\":{\"source\":\"ctx._source.counter++;\"},\"upsert\":{\"field1\":\"upserted_val\"}}\n"), XContentType.JSON).withMethod(RestRequest.Method.POST).build(), mock(RestChannel.class), verifyingClient);
assertThat(bulkCalled.get(), equalTo(true));
}
}
Aggregations