How to use Elasticsearch Ingest Attachment Processor Plugin in Java

Viewed 1183

I am looking for a way to use the Ingest Attachment Processor Plugin from the Java High-level REST client.

It seems that you need to do two steps, i.e., first you define a pipeline containing the attachment processor (e.g., referring to a field data and using a pipeline id attachment)

PUT _ingest/pipeline/attachment
{
  "description" : "Extract attachment information",
  "processors" : [
    {
      "attachment" : {
        "field" : "data"
      }
    }
 ]
}

then you PUT data referring to the field (here data) and the pipeline (here attachment)

PUT my_index/my_type/my_id?pipeline=attachment
{
  "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
}

Now I want to execute these two steps from the Java High-level REST client. It seems that I can execute the first step (definition of the pipeline) with the Put Pipeline API, but I could not find a Java mechanisms for the second part, i.e., writing the actual data while referring to the pipeline.

1 Answers

In a Java high-level REST client there is a method to index using IndexRequest, during the creation of it, you could set pipeline via Java method.

JavaDoc reference for it - https://artifacts.elastic.co/javadoc/org/elasticsearch/elasticsearch/7.6.2/org/elasticsearch/action/index/IndexRequest.html?is-external=true#setPipeline(java.lang.String)

I would expect to have code like this:

RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost", 9200, "http")));

final IndexRequest indexRequest = new IndexRequest("index-name", "index-type");
indexRequest.setPipeline("pipeline-name");

Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("data", "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=");

indexRequest.source(jsonMap);
final IndexResponse indexResponse = client.index(indexRequest);
Related