Search in sources :

Example 6 with PutPipelineRequest

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

the class FinalPipelineIT method testFinalPipelineOfOldDestinationIsNotInvoked.

public void testFinalPipelineOfOldDestinationIsNotInvoked() {
    Settings settings = Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "default_pipeline").put(IndexSettings.FINAL_PIPELINE.getKey(), "final_pipeline").build();
    createIndex("index", settings);
    BytesReference defaultPipelineBody = new BytesArray("{\"processors\": [{\"changing_dest\": {}}]}");
    client().admin().cluster().putPipeline(new PutPipelineRequest("default_pipeline", defaultPipelineBody, XContentType.JSON)).actionGet();
    BytesReference finalPipelineBody = new BytesArray("{\"processors\": [{\"final\": {\"exists\":\"no_such_field\"}}]}");
    client().admin().cluster().putPipeline(new PutPipelineRequest("final_pipeline", finalPipelineBody, XContentType.JSON)).actionGet();
    IndexResponse indexResponse = client().prepareIndex("index").setId("1").setSource(Collections.singletonMap("field", "value")).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
    assertEquals(RestStatus.CREATED, indexResponse.status());
    SearchResponse target = client().prepareSearch("target").get();
    assertEquals(1, target.getHits().getTotalHits().value);
    assertFalse(target.getHits().getAt(0).getSourceAsMap().containsKey("final"));
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) BytesArray(org.opensearch.common.bytes.BytesArray) IndexResponse(org.opensearch.action.index.IndexResponse) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) Settings(org.opensearch.common.settings.Settings) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 7 with PutPipelineRequest

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

the class IngestClientIT method test.

public void test() throws Exception {
    BytesReference source = BytesReference.bytes(jsonBuilder().startObject().field("description", "my_pipeline").startArray("processors").startObject().startObject("test").endObject().endObject().endArray().endObject());
    PutPipelineRequest putPipelineRequest = new PutPipelineRequest("_id", source, XContentType.JSON);
    client().admin().cluster().putPipeline(putPipelineRequest).get();
    GetPipelineRequest getPipelineRequest = new GetPipelineRequest("_id");
    GetPipelineResponse getResponse = client().admin().cluster().getPipeline(getPipelineRequest).get();
    assertThat(getResponse.isFound(), is(true));
    assertThat(getResponse.pipelines().size(), equalTo(1));
    assertThat(getResponse.pipelines().get(0).getId(), equalTo("_id"));
    client().prepareIndex("test").setId("1").setPipeline("_id").setSource("field", "value", "fail", false).get();
    Map<String, Object> doc = client().prepareGet("test", "1").get().getSourceAsMap();
    assertThat(doc.get("field"), equalTo("value"));
    assertThat(doc.get("processed"), equalTo(true));
    client().prepareBulk().add(client().prepareIndex("test").setId("2").setSource("field", "value2", "fail", false).setPipeline("_id")).get();
    doc = client().prepareGet("test", "2").get().getSourceAsMap();
    assertThat(doc.get("field"), equalTo("value2"));
    assertThat(doc.get("processed"), equalTo(true));
    DeletePipelineRequest deletePipelineRequest = new DeletePipelineRequest("_id");
    AcknowledgedResponse response = client().admin().cluster().deletePipeline(deletePipelineRequest).get();
    assertThat(response.isAcknowledged(), is(true));
    getResponse = client().admin().cluster().prepareGetPipeline("_id").get();
    assertThat(getResponse.isFound(), is(false));
    assertThat(getResponse.pipelines().size(), equalTo(0));
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) DeletePipelineRequest(org.opensearch.action.ingest.DeletePipelineRequest) GetPipelineRequest(org.opensearch.action.ingest.GetPipelineRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) GetPipelineResponse(org.opensearch.action.ingest.GetPipelineResponse)

Example 8 with PutPipelineRequest

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

the class IngestClientIT method testPipelineProcessorOnFailure.

public void testPipelineProcessorOnFailure() throws Exception {
    {
        XContentBuilder source = jsonBuilder().startObject();
        {
            source.startArray("processors");
            source.startObject();
            {
                source.startObject("pipeline");
                source.field("name", "2");
                source.endObject();
            }
            source.endObject();
            source.endArray();
        }
        {
            source.startArray("on_failure");
            source.startObject();
            {
                source.startObject("onfailure_processor");
                source.endObject();
            }
            source.endObject();
            source.endArray();
        }
        source.endObject();
        PutPipelineRequest putPipelineRequest = new PutPipelineRequest("1", BytesReference.bytes(source), XContentType.JSON);
        client().admin().cluster().putPipeline(putPipelineRequest).get();
    }
    {
        XContentBuilder source = jsonBuilder().startObject();
        {
            source.startArray("processors");
            source.startObject();
            {
                source.startObject("pipeline");
                source.field("name", "3");
                source.endObject();
            }
            source.endObject();
            source.endArray();
        }
        source.endObject();
        PutPipelineRequest putPipelineRequest = new PutPipelineRequest("2", BytesReference.bytes(source), XContentType.JSON);
        client().admin().cluster().putPipeline(putPipelineRequest).get();
    }
    {
        XContentBuilder source = jsonBuilder().startObject();
        {
            source.startArray("processors");
            source.startObject();
            {
                source.startObject("fail");
                source.endObject();
            }
            source.endObject();
            source.endArray();
        }
        source.endObject();
        PutPipelineRequest putPipelineRequest = new PutPipelineRequest("3", BytesReference.bytes(source), XContentType.JSON);
        client().admin().cluster().putPipeline(putPipelineRequest).get();
    }
    client().prepareIndex("test").setId("1").setSource("{}", XContentType.JSON).setPipeline("1").get();
    Map<String, Object> inserted = client().prepareGet("test", "1").get().getSourceAsMap();
    assertThat(inserted.get("readme"), equalTo("pipeline with id [3] is a bad pipeline"));
}
Also used : PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 9 with PutPipelineRequest

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

the class IngestClientIT method testBulkWithUpsert.

public void testBulkWithUpsert() throws Exception {
    createIndex("index");
    BytesReference source = BytesReference.bytes(jsonBuilder().startObject().field("description", "my_pipeline").startArray("processors").startObject().startObject("test").endObject().endObject().endArray().endObject());
    PutPipelineRequest putPipelineRequest = new PutPipelineRequest("_id", source, XContentType.JSON);
    client().admin().cluster().putPipeline(putPipelineRequest).get();
    BulkRequest bulkRequest = new BulkRequest();
    IndexRequest indexRequest = new IndexRequest("index").id("1").setPipeline("_id");
    indexRequest.source(Requests.INDEX_CONTENT_TYPE, "field1", "val1");
    bulkRequest.add(indexRequest);
    UpdateRequest updateRequest = new UpdateRequest("index", "2");
    updateRequest.doc("{}", Requests.INDEX_CONTENT_TYPE);
    updateRequest.upsert("{\"field1\":\"upserted_val\"}", XContentType.JSON).upsertRequest().setPipeline("_id");
    bulkRequest.add(updateRequest);
    BulkResponse response = client().bulk(bulkRequest).actionGet();
    assertThat(response.getItems().length, equalTo(bulkRequest.requests().size()));
    Map<String, Object> inserted = client().prepareGet("index", "1").get().getSourceAsMap();
    assertThat(inserted.get("field1"), equalTo("val1"));
    assertThat(inserted.get("processed"), equalTo(true));
    Map<String, Object> upserted = client().prepareGet("index", "2").get().getSourceAsMap();
    assertThat(upserted.get("field1"), equalTo("upserted_val"));
    assertThat(upserted.get("processed"), equalTo(true));
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) UpdateRequest(org.opensearch.action.update.UpdateRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) BulkResponse(org.opensearch.action.bulk.BulkResponse) IndexRequest(org.opensearch.action.index.IndexRequest)

Example 10 with PutPipelineRequest

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

the class IngestClientIT method testPipelineOriginHeader.

public void testPipelineOriginHeader() throws Exception {
    {
        XContentBuilder source = jsonBuilder().startObject();
        {
            source.startArray("processors");
            source.startObject();
            {
                source.startObject("pipeline");
                source.field("name", "2");
                source.endObject();
            }
            source.endObject();
            source.endArray();
        }
        source.endObject();
        PutPipelineRequest putPipelineRequest = new PutPipelineRequest("1", BytesReference.bytes(source), XContentType.JSON);
        client().admin().cluster().putPipeline(putPipelineRequest).get();
    }
    {
        XContentBuilder source = jsonBuilder().startObject();
        {
            source.startArray("processors");
            source.startObject();
            {
                source.startObject("pipeline");
                source.field("name", "3");
                source.endObject();
            }
            source.endObject();
            source.endArray();
        }
        source.endObject();
        PutPipelineRequest putPipelineRequest = new PutPipelineRequest("2", BytesReference.bytes(source), XContentType.JSON);
        client().admin().cluster().putPipeline(putPipelineRequest).get();
    }
    {
        XContentBuilder source = jsonBuilder().startObject();
        {
            source.startArray("processors");
            source.startObject();
            {
                source.startObject("fail");
                source.endObject();
            }
            source.endObject();
            source.endArray();
        }
        source.endObject();
        PutPipelineRequest putPipelineRequest = new PutPipelineRequest("3", BytesReference.bytes(source), XContentType.JSON);
        client().admin().cluster().putPipeline(putPipelineRequest).get();
    }
    Exception e = expectThrows(Exception.class, () -> {
        IndexRequest indexRequest = new IndexRequest("test");
        indexRequest.source("{}", XContentType.JSON);
        indexRequest.setPipeline("1");
        client().index(indexRequest).get();
    });
    IngestProcessorException ingestException = (IngestProcessorException) ExceptionsHelper.unwrap(e, IngestProcessorException.class);
    assertThat(ingestException.getHeader("processor_type"), equalTo(Collections.singletonList("fail")));
    assertThat(ingestException.getHeader("pipeline_origin"), equalTo(Arrays.asList("3", "2", "1")));
}
Also used : PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) IndexRequest(org.opensearch.action.index.IndexRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchException(org.opensearch.OpenSearchException) OpenSearchParseException(org.opensearch.OpenSearchParseException)

Aggregations

PutPipelineRequest (org.opensearch.action.ingest.PutPipelineRequest)46 BytesArray (org.opensearch.common.bytes.BytesArray)29 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)21 OpenSearchParseException (org.opensearch.OpenSearchParseException)19 IndexRequest (org.opensearch.action.index.IndexRequest)19 ClusterChangedEvent (org.opensearch.cluster.ClusterChangedEvent)19 ClusterName (org.opensearch.cluster.ClusterName)19 ClusterState (org.opensearch.cluster.ClusterState)19 Settings (org.opensearch.common.settings.Settings)19 Mockito.anyString (org.mockito.Mockito.anyString)18 HashMap (java.util.HashMap)16 ResourceNotFoundException (org.opensearch.ResourceNotFoundException)16 DeletePipelineRequest (org.opensearch.action.ingest.DeletePipelineRequest)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 BulkRequest (org.opensearch.action.bulk.BulkRequest)15 XContentType (org.opensearch.common.xcontent.XContentType)15 List (java.util.List)13 BiConsumer (java.util.function.BiConsumer)13 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)13 Arrays (java.util.Arrays)12