Search in sources :

Example 1 with SimulatePipelineRequest

use of org.opensearch.action.ingest.SimulatePipelineRequest in project OpenSearch by opensearch-project.

the class IngestRequestConverters method simulatePipeline.

static Request simulatePipeline(SimulatePipelineRequest simulatePipelineRequest) throws IOException {
    RequestConverters.EndpointBuilder builder = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ingest/pipeline");
    if (simulatePipelineRequest.getId() != null && !simulatePipelineRequest.getId().isEmpty()) {
        builder.addPathPart(simulatePipelineRequest.getId());
    }
    builder.addPathPartAsIs("_simulate");
    String endpoint = builder.build();
    Request request = new Request(HttpPost.METHOD_NAME, endpoint);
    RequestConverters.Params params = new RequestConverters.Params();
    params.putParam("verbose", Boolean.toString(simulatePipelineRequest.isVerbose()));
    request.addParameters(params.asMap());
    request.setEntity(RequestConverters.createEntity(simulatePipelineRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
    return request;
}
Also used : DeletePipelineRequest(org.opensearch.action.ingest.DeletePipelineRequest) SimulatePipelineRequest(org.opensearch.action.ingest.SimulatePipelineRequest) GetPipelineRequest(org.opensearch.action.ingest.GetPipelineRequest) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest)

Example 2 with SimulatePipelineRequest

use of org.opensearch.action.ingest.SimulatePipelineRequest in project OpenSearch by opensearch-project.

the class IngestRequestConvertersTests method testSimulatePipeline.

public void testSimulatePipeline() throws IOException {
    String pipelineId = OpenSearchTestCase.randomBoolean() ? "some_pipeline_id" : null;
    boolean verbose = OpenSearchTestCase.randomBoolean();
    String json = "{" + "  \"pipeline\": {" + "    \"description\": \"_description\"," + "    \"processors\": [" + "      {" + "        \"set\": {" + "          \"field\": \"field2\"," + "          \"value\": \"_value\"" + "        }" + "      }" + "    ]" + "  }," + "  \"docs\": [" + "    {" + "      \"_index\": \"index\"," + "      \"_type\": \"_doc\"," + "      \"_id\": \"id\"," + "      \"_source\": {" + "        \"foo\": \"rab\"" + "      }" + "    }" + "  ]" + "}";
    SimulatePipelineRequest request = new SimulatePipelineRequest(new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON);
    request.setId(pipelineId);
    request.setVerbose(verbose);
    Map<String, String> expectedParams = new HashMap<>();
    expectedParams.put("verbose", Boolean.toString(verbose));
    Request expectedRequest = IngestRequestConverters.simulatePipeline(request);
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    endpoint.add("_ingest/pipeline");
    if (pipelineId != null && !pipelineId.isEmpty())
        endpoint.add(pipelineId);
    endpoint.add("_simulate");
    Assert.assertEquals(endpoint.toString(), expectedRequest.getEndpoint());
    Assert.assertEquals(HttpPost.METHOD_NAME, expectedRequest.getMethod());
    Assert.assertEquals(expectedParams, expectedRequest.getParameters());
    RequestConvertersTests.assertToXContentBody(request, expectedRequest.getEntity());
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) HashMap(java.util.HashMap) DeletePipelineRequest(org.opensearch.action.ingest.DeletePipelineRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) SimulatePipelineRequest(org.opensearch.action.ingest.SimulatePipelineRequest) GetPipelineRequest(org.opensearch.action.ingest.GetPipelineRequest) SimulatePipelineRequest(org.opensearch.action.ingest.SimulatePipelineRequest) StringJoiner(java.util.StringJoiner)

Example 3 with SimulatePipelineRequest

use of org.opensearch.action.ingest.SimulatePipelineRequest in project OpenSearch by opensearch-project.

the class IngestClientIT method testSimulate.

public void testSimulate() throws Exception {
    BytesReference pipelineSource = BytesReference.bytes(jsonBuilder().startObject().field("description", "my_pipeline").startArray("processors").startObject().startObject("test").endObject().endObject().endArray().endObject());
    client().admin().cluster().preparePutPipeline("_id", pipelineSource, XContentType.JSON).get();
    GetPipelineResponse getResponse = client().admin().cluster().prepareGetPipeline("_id").get();
    assertThat(getResponse.isFound(), is(true));
    assertThat(getResponse.pipelines().size(), equalTo(1));
    assertThat(getResponse.pipelines().get(0).getId(), equalTo("_id"));
    BytesReference bytes = BytesReference.bytes(jsonBuilder().startObject().startArray("docs").startObject().field("_index", "index").field("_type", "type").field("_id", "id").startObject("_source").field("foo", "bar").field("fail", false).endObject().endObject().endArray().endObject());
    SimulatePipelineResponse response;
    if (randomBoolean()) {
        response = client().admin().cluster().prepareSimulatePipeline(bytes, XContentType.JSON).setId("_id").get();
    } else {
        SimulatePipelineRequest request = new SimulatePipelineRequest(bytes, XContentType.JSON);
        request.setId("_id");
        response = client().admin().cluster().simulatePipeline(request).get();
    }
    assertThat(response.isVerbose(), equalTo(false));
    assertThat(response.getPipelineId(), equalTo("_id"));
    assertThat(response.getResults().size(), equalTo(1));
    assertThat(response.getResults().get(0), instanceOf(SimulateDocumentBaseResult.class));
    SimulateDocumentBaseResult simulateDocumentBaseResult = (SimulateDocumentBaseResult) response.getResults().get(0);
    Map<String, Object> source = new HashMap<>();
    source.put("foo", "bar");
    source.put("fail", false);
    source.put("processed", true);
    IngestDocument ingestDocument = new IngestDocument("index", "id", null, null, null, source);
    assertThat(simulateDocumentBaseResult.getIngestDocument().getSourceAndMetadata(), equalTo(ingestDocument.getSourceAndMetadata()));
    assertThat(simulateDocumentBaseResult.getFailure(), nullValue());
    // cleanup
    AcknowledgedResponse deletePipelineResponse = client().admin().cluster().prepareDeletePipeline("_id").get();
    assertTrue(deletePipelineResponse.isAcknowledged());
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) SimulatePipelineResponse(org.opensearch.action.ingest.SimulatePipelineResponse) HashMap(java.util.HashMap) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) SimulatePipelineRequest(org.opensearch.action.ingest.SimulatePipelineRequest) GetPipelineResponse(org.opensearch.action.ingest.GetPipelineResponse) SimulateDocumentBaseResult(org.opensearch.action.ingest.SimulateDocumentBaseResult)

Example 4 with SimulatePipelineRequest

use of org.opensearch.action.ingest.SimulatePipelineRequest in project OpenSearch by opensearch-project.

the class RestSimulatePipelineAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
    Tuple<XContentType, BytesReference> sourceTuple = restRequest.contentOrSourceParam();
    SimulatePipelineRequest request = new SimulatePipelineRequest(sourceTuple.v2(), sourceTuple.v1());
    request.setId(restRequest.param("id"));
    request.setVerbose(restRequest.paramAsBoolean("verbose", false));
    return channel -> client.admin().cluster().simulatePipeline(request, new RestToXContentListener<>(channel));
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) POST(org.opensearch.rest.RestRequest.Method.POST) NodeClient(org.opensearch.client.node.NodeClient) BytesReference(org.opensearch.common.bytes.BytesReference) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) Tuple(org.opensearch.common.collect.Tuple) SimulatePipelineRequest(org.opensearch.action.ingest.SimulatePipelineRequest) List(java.util.List) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) XContentType(org.opensearch.common.xcontent.XContentType) BaseRestHandler(org.opensearch.rest.BaseRestHandler) XContentType(org.opensearch.common.xcontent.XContentType) SimulatePipelineRequest(org.opensearch.action.ingest.SimulatePipelineRequest)

Example 5 with SimulatePipelineRequest

use of org.opensearch.action.ingest.SimulatePipelineRequest in project OpenSearch by opensearch-project.

the class IngestClientIT method testSimulatePipeline.

private void testSimulatePipeline(boolean isVerbose, boolean isFailure) throws IOException {
    XContentType xContentType = randomFrom(XContentType.values());
    XContentBuilder builder = XContentBuilder.builder(xContentType.xContent());
    String rankValue = isFailure ? "non-int" : Integer.toString(1234);
    builder.startObject();
    {
        builder.field("pipeline");
        buildRandomXContentPipeline(builder);
        builder.startArray("docs");
        {
            builder.startObject().field("_index", "index").field("_id", "doc_" + 1).startObject("_source").field("foo", "rab_" + 1).field("rank", rankValue).endObject().endObject();
        }
        builder.endArray();
    }
    builder.endObject();
    SimulatePipelineRequest request = new SimulatePipelineRequest(BytesReference.bytes(builder), builder.contentType());
    request.setVerbose(isVerbose);
    SimulatePipelineResponse response = execute(request, highLevelClient().ingest()::simulate, highLevelClient().ingest()::simulateAsync);
    List<SimulateDocumentResult> results = response.getResults();
    assertEquals(1, results.size());
    if (isVerbose) {
        assertThat(results.get(0), instanceOf(SimulateDocumentVerboseResult.class));
        SimulateDocumentVerboseResult verboseResult = (SimulateDocumentVerboseResult) results.get(0);
        assertEquals(2, verboseResult.getProcessorResults().size());
        if (isFailure) {
            assertNotNull(verboseResult.getProcessorResults().get(1).getFailure());
            assertThat(verboseResult.getProcessorResults().get(1).getFailure().getMessage(), containsString("unable to convert [non-int] to integer"));
        } else {
            assertEquals(verboseResult.getProcessorResults().get(0).getIngestDocument().getFieldValue("foo", String.class), "bar");
            assertEquals(Integer.valueOf(1234), verboseResult.getProcessorResults().get(1).getIngestDocument().getFieldValue("rank", Integer.class));
        }
    } else {
        assertThat(results.get(0), instanceOf(SimulateDocumentBaseResult.class));
        SimulateDocumentBaseResult baseResult = (SimulateDocumentBaseResult) results.get(0);
        if (isFailure) {
            assertNotNull(baseResult.getFailure());
            assertThat(baseResult.getFailure().getMessage(), containsString("unable to convert [non-int] to integer"));
        } else {
            assertNotNull(baseResult.getIngestDocument());
            assertEquals(baseResult.getIngestDocument().getFieldValue("foo", String.class), "bar");
            assertEquals(Integer.valueOf(1234), baseResult.getIngestDocument().getFieldValue("rank", Integer.class));
        }
    }
}
Also used : SimulateDocumentResult(org.opensearch.action.ingest.SimulateDocumentResult) XContentType(org.opensearch.common.xcontent.XContentType) SimulatePipelineResponse(org.opensearch.action.ingest.SimulatePipelineResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) SimulatePipelineRequest(org.opensearch.action.ingest.SimulatePipelineRequest) SimulateDocumentVerboseResult(org.opensearch.action.ingest.SimulateDocumentVerboseResult) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) SimulateDocumentBaseResult(org.opensearch.action.ingest.SimulateDocumentBaseResult)

Aggregations

SimulatePipelineRequest (org.opensearch.action.ingest.SimulatePipelineRequest)5 HashMap (java.util.HashMap)2 DeletePipelineRequest (org.opensearch.action.ingest.DeletePipelineRequest)2 GetPipelineRequest (org.opensearch.action.ingest.GetPipelineRequest)2 PutPipelineRequest (org.opensearch.action.ingest.PutPipelineRequest)2 SimulateDocumentBaseResult (org.opensearch.action.ingest.SimulateDocumentBaseResult)2 SimulatePipelineResponse (org.opensearch.action.ingest.SimulatePipelineResponse)2 BytesReference (org.opensearch.common.bytes.BytesReference)2 XContentType (org.opensearch.common.xcontent.XContentType)2 IOException (java.io.IOException)1 Arrays.asList (java.util.Arrays.asList)1 Collections.unmodifiableList (java.util.Collections.unmodifiableList)1 List (java.util.List)1 StringJoiner (java.util.StringJoiner)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 GetPipelineResponse (org.opensearch.action.ingest.GetPipelineResponse)1 SimulateDocumentResult (org.opensearch.action.ingest.SimulateDocumentResult)1 SimulateDocumentVerboseResult (org.opensearch.action.ingest.SimulateDocumentVerboseResult)1 AcknowledgedRequest (org.opensearch.action.support.master.AcknowledgedRequest)1 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)1