如何使用 Java API 设置 Ingest attachment(elasticsearch) 插件选项?

How can I set Ingest attachment(elasticsearch) plugin options with Java API?

我在 elasticsearch 上使用 Ingest Attachment Processor Plugin。我需要使用 Java API 设置附件选项(indexed_charspropertiesignore_missing 等)。我该怎么做?

我正在创建索引并设置管道,如下所示:

String id = ...
Map<String, Object> row = ...
client.prepareIndex(indexName, "my_type", id)
                    .setSource(row)
                    .setPipeline("my_pipeline")
                    .execute();

我找到了答案,如果你有嵌套文档,你必须使用 foreach 否则构建 json 就像 documentation

文档:

解法:

try (XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()) {
    BytesReference pipelineSource = jsonBuilder.startObject()
            .field("description", "Extract attachment information")
            .startArray("processors")
              .startObject()
                .startObject("foreach")
                  .field("field", "my_field")
                  .startObject("processor")
                    .startObject("attachment")
                      .field("field", "_ingest._value.my_base64_field")
                      .field("target_field", "_ingest._value.my_base64_field")
                      .field("ignore_missing", true)
                      .field("indexed_chars", -1)
                    .endObject()
                  .endObject()
                .endObject()
              .endObject()
            .endArray()
            .endObject().bytes();
    client.admin().cluster().preparePutPipeline("my_pipeline",
            pipelineSource, XContentType.JSON).get();
}

你可以在json下面手动输入

结果:

http://localhost:9200/_ingest/pipeline/my_pipeline

{
  "my_pipeline": {
    "description": "Extract attachment information",
    "processors": [
      {
        "foreach": {
          "field": "my_field",
          "processor": {
            "attachment": {
              "field": "_ingest._value.my_base64_field",
              "target_field": "_ingest._value.my_base64_field",
              "ignore_missing": true,
              "indexed_chars": -1
            }
          }
        }
      }
    ]
  }
}