0

I want to append some data to a document in Elasticsearch and set a timestamp using MVEL.

this is what I am currently trying. But it does not work.

{"error":"ElasticSearchIllegalArgumentException[failed to execute script]; nested: PropertyAccessException[[Error: could not access property (Timestamp) in: java.lang.Long]\n[Near : {... Timestamp ....}]\n ^\n[Line: 1, Column: 1]]; ","status":400}

How can I access the Timestamp field (it is successfully created via a mapping)

  {
  "script": "
            valueSet.Timestamp = time();
            if (ctx._source[\"values\"] == null) { ctx._source.values = valueSet} else {ctx._source.values += valueSet}
            ",
  "params": {
    "valueSet": 
    [ 
    {
      "Timestamp": "",
      "value": "100.00"
    } 
   ]
  }
}

UPDATE:

Found out how to access the valueSet

  {
  "script": "
            valueSet[0].value = 'test';
            if (ctx._source[\"values\"] == null) { ctx._source.values = valueSet} else {ctx._source.values += valueSet}
            ",
  "params": {
    "valueSet": 
    [ 
    {
      "Timestamp": "",
      "value": "100.00"
    } 
   ]
  }
}

The above will override the "100.00" to "test".

Jabb
  • 3,414
  • 8
  • 35
  • 58
  • Have you tried accessing another field with that script? Maybe the timestamp field is a speacial case? – MeiSign Nov 01 '13 at 09:50
  • japp! no success. it can't access any field from "valueSet" using the above syntax.. – Jabb Nov 01 '13 at 10:03

1 Answers1

1

I am able to update whole array or whole field very simply just like array. follow these step.

step -1 put a smaple data.

    PUT /1/user/1
    {
        "userId":2,
        "sid":1,
             "1": [
                    "24 hrs left!"
                ]
            ,
            "2": 9
            ,
             "32": "2014-08-01T00:00:00"
            ,
             "evant": [

        {
            "name": "2",
            "count": 9,
            "first_date":"2014-08-01T00:00:00",
            "last_date":"2014-08-01T00:00:00",
            "_date":["2014-08-01T00:00:00",
            "2014-08-01T00:00:00",
            "2014-08-01T00:00:00"]
        },
        {
            "name": "3",
            "count": 9,
            "first_date":"2014-08-01T00:00:00",
            "last_date":"2014-08-01T00:00:00",
            "_date":["2014-08-01T00:00:00",
            "2014-08-01T00:00:00",
            "2014-08-01T00:00:00"]
        }]
}

step-2. update any nested filed value.

POST  /1/user/1/_update
{
  "script": "foreach (item : ctx._source.evant) { if (item['name'] == name) { item['name'] =  item['name']==null?\"none\": 'myname'; } }",
  "params": {"name": "abc"}
}

step-3. add any new field.

POST  /1/user/1/_update
{
  "script": "foreach (item : ctx._source.evant) { if (item['name'] == name) { item['Fname']='zyz'; } }",
  "params": {"name": "abc"}
}
helloflash
  • 2,457
  • 2
  • 15
  • 19
Dharam Bhai
  • 41
  • 1
  • 8