如何使用 Java API 设置摄取附件(elasticsearch)插件选项?

nik*_*kli 2 java elasticsearch elasticsearch-plugin

我在elasticsearch上使用摄取附件处理器插件。我需要一套连接选项(indexed_charspropertiesignore_missing等)的Java API。我怎样才能做到这一点?

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

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

nik*_*kli 5

我找到了答案,如果您有嵌套文档,则必须使用foreachelse build json like 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();
}
Run Code Online (Sandbox Code Playgroud)

或者

你可以手动把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
            }
          }
        }
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)