Rest High Level Client Partial Nested Update Script

I wanna send Script to ElasticSearch.

I want to do a partial update inside a nested array.

But It didn't work well...

Below is the script code I created. At least this works well in kibana.

{ 
"script": {
"source" : "def target = ctx._source.daily_schedules.findAll(it -> { for(i in params.json) { if(i.date == it.date) { for(j in i.entrySet()) { it[j.getKey()] = j.getValue() } return true;} } return false; }) ",
"params": {
  "json" : [{
    "date" : "2019-10-08",
    "isBusinessDay" : false,
    "isSaleDay" : false,
    "hourly_schedules": [
    {
      "dateTime" : "2019-10-08T00:30:00+09:00",
      "stock" : 2,
      "bookingCount" : 0,
      "remainStock" : 2
    },
    {
      "dateTime" : "2019-10-08T01:00:00+09:00",
      "stock" : 2,
      "bookingCount" : 0,
      "remainStock" : 2
    }
    ]
  },
  {
    "date" : "2019-10-09",
    "isBusinessDay" : false,
    "isSaleDay" : false,
    "hourly_schedules": [{
      "dateTime" : "2019-10-09T00:00:00+09:00",
      "stock": 998,
      "bookingCount" : 998,
      "remainStock" : 0
    },
    {
      "dateTime" : "2019-10-09T00:30:00+09:00",
      "stock" : 2,
      "bookingCount" : 0,
      "remainStock" : 2
    },
    {
      "dateTime" : "2019-10-09T01:00:00+09:00",
      "stock" : 2,
      "bookingCount" : 0,
      "remainStock" : 2
    }
    ]
  }
  ]
  }
 }
}

The result of the operation is as follows.

"daily_schedules": [
  {
    "date": "2019-10-08",
    "isBusinessDay": false,
    "isSaleDay": false,
    "hourly_schedules": [
      {
        "dateTime": "2019-10-08T00:30:00+09:00",
        "bookingCount": 0,
        "remainStock": 2,
        "stock": 2
      },
      {
        "dateTime": "2019-10-08T01:00:00+09:00",
        "bookingCount": 0,
        "remainStock": 2,
        "stock": 2
      }
    ]
  },
  {
    "date": "2019-10-09",
    "isBusinessDay": false,
    "isSaleDay": false,
    "hourly_schedules": [
      {
        "dateTime": "2019-10-09T00:00:00+09:00",
        "bookingCount": 998,
        "remainStock": 0,
        "stock": 998
      },
      {
        "dateTime": "2019-10-09T00:30:00+09:00",
        "bookingCount": 0,
        "remainStock": 2,
        "stock": 2
      },
      {
        "dateTime": "2019-10-09T01:00:00+09:00",
        "bookingCount": 0,
        "remainStock": 2,
        "stock": 2
      }
    ]
  }

I'm using REST HIGH LEVEL CLIENT on Spring Framework.

However, the above script does not work well here.

data class AvailableDailySchedule(
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
    val date: LocalDate,
    @get:JsonProperty("isBusinessDay") val isBusinessDay: Boolean,
    @get:JsonProperty("isSaleDay") val isSaleDay: Boolean,
    val hourly_schedules : List<AvailableHourlySchedule>
)

This is Class

    availableDailyScheduleList: List<AvailableDailySchedule>
    val parameters = mutableMapOf<String, Any>()
    parameters["json"] = availableDailyScheduleList

    val inline = Script(
        ScriptType.INLINE, "painless",
        "def target = ctx._source.daily_schedules.findAll(it -> { for(i in params.json) { if(i.date == it.date) { for(j in i.entrySet()) { it[j.getKey()] = j.getValue() } return true;} } return false; })",
        parameters
    )
    request.script(inline)

It is RestHighLevelClient Code.
The above language is written in Kotlin.
This code unfortunately doesn't work with the following error:

cannot write xcontent for unknown value of type class AvailableDailySchedule

So I tried changing my List to String using Jackson's Object Mapper as well, but still to no avail.

like this

    val parameters = mutableMapOf<String, Any>()
    parameters["json"] = ObjectMapper.writeValueAsString(availableDailyScheduleList)

However, this method still generates the following error:

cannot iterate over java.lang.string

Not all methods work.

Please Help me.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.