How to populate indexableField based on IngestDocument in Elasticsearch using IngestPlugin

Viewed 82

I am writing IngestPlugin, which needs to populate a timestamp (multi-field) based on fields in IngestDocument. I have defined an index as follows

 {
  "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"
  }
}

I tried to set value for "title.timestamp" using processor like this

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

I got the following error while inserting a document

{

  "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

}

Is there any way to populate multi-field (field1.timestamp) value on indesting document using Elasticsearch plugin?

1 Answers

You cannot populate a sub-field of a multi-field directly.

Since field1 is of type text, you can only set "field1": "2020-07-27" and you'll get:

  • the analyzed text value of "2020-07-27" in field1 and
  • the date value of "2020-07-27" in field1.timestamp

So you need to simply do this instead:

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