SSG
SSG

Reputation: 1505

Elasticsearch inline script : can we use ctx._source field in "params"

I am trying to update huge number of documents with following update_by_query. Payload is something like this:

Consider a case:

value of currentValues.status field changed from "New" to "Fixed"
value of currentValues.priority field changed from "High" to "Low"

So basically
1] I want to record the old values for parameters exa: "status","priority" and assign it to "oldValues" before they as updated.
2] After they are updated I want to assign them to "currentValues"

{
  "oldValues": {
    "status": "New",
    "priority": "High",
    .
    .
    .
    many other fields
  },"currentValues": {
    "status": "Fixed",
    "priority": "Low",
    .
    .
    .
    many other fields
  }
}

I was trying to do so with following script.

POST http://localhost:9200/index/type/_update_by_query

{
  "script": {
    "inline": "ctx._source.data.oldValues.status=params.previousStatus;",
    "params": {
      "previousStatus": {"status": ctx._source.currentValues.status }, // **Unrecognized token 'ctx': was expecting ('true', 'false' or 'null')**
    }
   }
}

So I am trying to store the old value of status in some constant i.e ("previousStatus") in "params" section and use it in "script" section.
But looks like we can store only contant values in "params" section.
Can we assign value of "ctx._source" field to some constant (exa : "previousStatus" in above script )in "params" section ??
If not ,is there a way where in I can store the old values in some variable and use it "script".

Thanks in advance !

Sandeep

Upvotes: 3

Views: 7482

Answers (1)

mkalsi
mkalsi

Reputation: 363

You can use the below script to store the previous values in the old state, and update the current state values to new one.

{
  "script": {
    "inline": 
    "ctx._source.data.oldValues.status=ctx._source.data.currentValues.status;     
     ctx._source.data.oldValues.priority=ctx._source.data.currentValues.priority;
     ctx._source.data.currentValues.status=params.newState.status;
    ctx._source.data.currentValues.priority=params.newState.priority;",
    "params": {
      "newState": {"status": "Fixed","priority": "Low" }
    }
   }
}

Upvotes: 2

Related Questions