首页 > 解决方案 > 如何使用 IngestPlugin 在 Elasticsearch 中基于 IngestDocument 填充 indexableField

问题描述

我正在编写 IngestPlugin,它需要根据 IngestDocument 中的字段填充时间戳(多字段)。我已经定义了一个索引如下

 {
  "mappings": {
    "properties": {
      "field1": {
        "type": "text",
        "fields": {
          "timestamp": {
            "type": "date",
            "format": "yyyy-MM-dd"
          }
        }
      },
      "field2": {
        "type": "text",
        "fields": {
          "timestamp": {
            "type": "date",
            "format": "yyyy-MM-dd"
          }
        }
      },
      "field3": {
        "type": "text"
      }
    }
  },
  "settings": {
    "default_pipeline": "my_pipeline"
  }
}

我尝试使用这样的处理器为“title.timestamp”设置值

@Override
public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
    ingestDocument.setFieldValue("field1.timestamp", "2020-07-27");
    
    return ingestDocument;
}

插入文档时出现以下错误

{

  "error" : {

    "root_cause" : [

      {

        "type" : "illegal_argument_exception",

        "reason" : "cannot set [timestamp] with parent object of type [java.lang.String] as part of path [field1.timestamp]"

      }

    ],

    "type" : "illegal_argument_exception",

    "reason" : "cannot set [timestamp] with parent object of type [java.lang.String] as part of path [field1.timestamp]"

  },

  "status" : 400

}

有没有办法使用 Elasticsearch 插件在 indesting 文档上填充多字段(field1.timestamp)值?

标签: javaelasticsearchplugins

解决方案


您不能直接填充多字段的子字段。

由于field1是 type text,你只能设置"field1": "2020-07-27",你会得到:

  • in的分析text值和"2020-07-27"field1
  • date"2020-07-27"_field1.timestamp

所以你需要简单地这样做:

ingestDocument.setFieldValue("field1", "2020-07-27");
                                    ^
                                    |
                          remove .timestamp here

推荐阅读