Elasticsearch update index NoSuchElementException error

Hi,

I am updating user document with mvel.

It works fine for few minutes and after some time es starts giving below issue

"ElasticsearchIllegalArgumentException[failed to execute script]; nested: NoSuchElementException;"

If I restart es, same query runs successfully.

complete error message is live below

[update] => Array

                    (

                        [_index] => jal

                        [_type] => user

                        [_id] => 559e226b1d4213241fb5bba3

                        [status] => 400

                        [error] => ElasticsearchIllegalArgumentException[failed to execute script]; nested: NoSuchElementException; 

                    )

Looks like your script is trying to iterate past the end of a List/Array (https://docs.oracle.com/javase/7/docs/api/java/util/NoSuchElementException.html)

By the way, its usual much easier to help with these kinds of questions if you post the actual script you are running and an example document (preferrably one that causes the script to fail and one that succeeds). If the script or documents are very long consider posting a link to a gist.

"update":{"id":"hr324hdie93hdjdo391","type":"user","index":"jal","routing":2,"retry_on_conflict":5}}
{"lang":"mvel","script":"if(ctx.source.containsKey(\"category\")){i=0; io=0; foreach (item : ctx.source.category){i++; if (item[ 'key' ] == v_123_purchased){ io=1}} if(io==0){ctx.source.category.add(p123_purchased);}}else{ctx.source.category=c123_purchased;}if(ctx.source.containsKey(\"category\")){i=0; io=0; foreach (item : ctx.source.category){i++; if (item[ 'key' ] == v_233_purchased){ io=1}} if(io==0){ctx.source.category.add(p233_purchased);}}else{ctx.source.category=c233_purchased;}foreach (item : ctx.source.category) { if (item[ 'key' ] == key123_purchased) { if(item[ 'name' ]==null){item[ 'name' ] =name_123_purchased;} } }foreach (item : ctx.source.category) { if (item[ 'key' ] == key123_purchased) { if(item[ 'action' ]==null){item[ 'action' ] =action_123_purchased;} } }foreach (item : ctx.source.category) { if (item[ 'key' ] == key123_purchased) {if(item[ 'price' ]==null){item[ 'price' ]=price_123_purchased;}else{ item[ 'price' ]=item[ 'price' ]+ price_123_purchased;} } }foreach (item : ctx.source.category) { if (item[ 'key' ] == key123_purchased) {if(item[ 'count' ]==null){item[ 'count' ]=count_123_purchased;}else{ item[ 'count' ]=item[ 'count' ]+ count_123_purchased;} } }foreach (item : ctx.source.category) { if (item[ 'key' ] == key123_purchased) {if(item[ 'last_date' ]==null){item[ 'last_date' ]=last_date_123_purchased;}else{ item[ 'last_date' ] = last_date_123_purchased; } }}foreach (item : ctx.source.category) { if (item[ 'key' ] == key123_purchased) {if(item[ 'with_promo' ]==null){item[ 'with_promo' ]=with_promo_123_purchased;}else{ item[ 'with_promo' ]=item[ 'with_promo' ]+ with_promo_123_purchased;} } }foreach (item : ctx.source.category) { if (item[ 'key' ] == key233_purchased) { if(item[ 'name' ]==null){item[ 'name' ] =name_233_purchased;} } }foreach (item : ctx.source.category) { if (item[ 'key' ] == key233_purchased) { if(item[ 'action' ]==null){item[ 'action' ] =action_233_purchased;} } }foreach (item : ctx.source.category) { if (item[ 'key' ] == key233_purchased) {if(item[ 'price' ]==null){item[ 'price' ]=price_233_purchased;}else{ item[ 'price' ]=item[ 'price' ]+ price_233_purchased;} } }foreach (item : ctx.source.category) { if (item[ 'key' ] == key233_purchased) {if(item[ 'count' ]==null){item[ 'count' ]=count_233_purchased;}else{ item[ 'count' ]=item[ 'count' ]+ count_233_purchased;} } }foreach (item : ctx.source.category) { if (item[ 'key' ] == key233_purchased) {if(item[ 'last_date' ]==null){item[ 'last_date' ]=last_date_233_purchased;}else{ item[ 'last_date' ] = last_date_233_purchased; } }}foreach (item : ctx.source.category) { if (item[ 'key' ] == key233_purchased) {if(item[ 'with_promo' ]==null){item[ 'with_promo' ]=with_promo_233_purchased;}else{ item[ 'with_promo' ]=item[ 'with_promo' ]+ with_promo_233_purchased;} } }","params":{"p_123_purchased":{"key":"123_purchased"},"c_123_purchased":[{"key":"123_purchased"}],"v_123_purchased":"123_purchased","p_233_purchased":{"key":"233_purchased"},"c_233_purchased":[{"key":"233_purchased"}],"v_233_purchased":"233_purchased","key_123_purchased":"123_purchased","name_123_purchased":123,"action_123_purchased":"purchased","price_123_purchased":20,"count_123_purchased":1,"last_date_123_purchased":"2015-07-04T07:11:03","with_promo_123_purchased":1,"key_233_purchased":"233_purchased","name_233_purchased":233,"action_233_purchased":"purchased","price_233_purchased":20,"count_233_purchased":1,"last_date_233_purchased":"2015-07-04T07:11:03","with_promo_233_purchased":1}}

Trying To update this type of object

{
               "_userId": "hr324hdie93hdjdo391",
               "_shardId": 2,
               "brand": [
                  {
                     "price": 140,
                     "name": 23,
                     "count": 7,
                     "without_promo": 7,
                     "action": "purchased",
                     "key": "23_purchased",
                     "last_date": "2015-07-04T10:10:25"
                  }
               ],
               "product": [
                  {
                     "price": 140,
                     "with_promo": 7,
                     "name": 28,
                     "count": 7,
                     "action": "purchased",
                     "key": "28_purchased",
                     "last_date": "2015-07-04T10:10:25"
                  },
                  {
                     "price": 40,
                     "with_promo": 2,
                     "name": 29,
                     "count": 2,
                     "action": "purchased",
                     "key": "29_purchased",
                     "last_date": "2015-07-04T06:25:30"
                  }
                 
               ],
               "category": [
                  {
                     "price": 140,
                     "with_promo": 7,
                     "name": 123,
                     "count": 7,
                     "action": "purchased",
                     "key": "123_purchased",
                     "last_date": "2015-07-04T10:10:25"
                  },
                  {
                     "price": 40,
                     "with_promo": 2,
                     "name": 124,
                     "count": 2,
                     "action": "purchased",
                     "key": "124_purchased",
                     "last_date": "2015-07-04T06:25:30"
                  }               ]
            }

Unfortunately I do not know mvel but I would think that there is a bug in your script somewhere which is being triggered by specific documents. I would suggest that you debug the script by removing parts of script until it runs successfully and then slowly add the parts back until you find the error. I would also try to determine the specific documents which are causing the error.

Below I have repasted the script and the params formatted in a more readable way incase anyone else can see something obvious but I would go with the above suggestion for now.

script:

if (ctx.source.containsKey("category")) {
  i = 0;
  io = 0;
  foreach(item: ctx.source.category) {
    i++;
    if (item['key'] == v_123_purchased) {
      io = 1
    }
  }
  if (io == 0) {
    ctx.source.category.add(p123_purchased);
  }
} else {
  ctx.source.category = c123_purchased;
}
if (ctx.source.containsKey("category")) {
  i = 0;
  io = 0;
  foreach(item: ctx.source.category) {
    i++;
    if (item['key'] == v_233_purchased) {
      io = 1
    }
  }
  if (io == 0) {
    ctx.source.category.add(p233_purchased);
  }
} else {
  ctx.source.category = c233_purchased;
}
foreach(item: ctx.source.category) {
  if (item['key'] == key123_purchased) {
    if (item['name'] == null) {
      item['name'] = name_123_purchased;
    }
  }
}
foreach(item: ctx.source.category) {
  if (item['key'] == key123_purchased) {
    if (item['action'] == null) {
      item['action'] = action_123_purchased;
    }
  }
}
foreach(item: ctx.source.category) {
  if (item['key'] == key123_purchased) {
    if (item['price'] == null) {
      item['price'] = price_123_purchased;
    } else {
      item['price'] = item['price'] + price_123_purchased;
    }
  }
}
foreach(item: ctx.source.category) {
  if (item['key'] == key123_purchased) {
    if (item['count'] == null) {
      item['count'] = count_123_purchased;
    } else {
      item['count'] = item['count'] + count_123_purchased;
    }
  }
}
foreach(item: ctx.source.category) {
  if (item['key'] == key123_purchased) {
    if (item['last_date'] == null) {
      item['last_date'] = last_date_123_purchased;
    } else {
      item['last_date'] = last_date_123_purchased;
    }
  }
}
foreach(item: ctx.source.category) {
  if (item['key'] == key123_purchased) {
    if (item['with_promo'] == null) {
      item['with_promo'] = with_promo_123_purchased;
    } else {
      item['with_promo'] = item['with_promo'] + with_promo_123_purchased;
    }
  }
}
foreach(item: ctx.source.category) {
  if (item['key'] == key233_purchased) {
    if (item['name'] == null) {
      item['name'] = name_233_purchased;
    }
  }
}
foreach(item: ctx.source.category) {
  if (item['key'] == key233_purchased) {
    if (item['action'] == null) {
      item['action'] = action_233_purchased;
    }
  }
}
foreach(item: ctx.source.category) {
  if (item['key'] == key233_purchased) {
    if (item['price'] == null) {
      item['price'] = price_233_purchased;
    } else {
      item['price'] = item['price'] + price_233_purchased;
    }
  }
}
foreach(item: ctx.source.category) {
  if (item['key'] == key233_purchased) {
    if (item['count'] == null) {
      item['count'] = count_233_purchased;
    } else {
      item['count'] = item['count'] + count_233_purchased;
    }
  }
}
foreach(item: ctx.source.category) {
  if (item['key'] == key233_purchased) {
    if (item['last_date'] == null) {
      item['last_date'] = last_date_233_purchased;
    } else {
      item['last_date'] = last_date_233_purchased;
    }
  }
}
foreach(item: ctx.source.category) {
  if (item['key'] == key233_purchased) {
    if (item['with_promo'] == null) {
      item['with_promo'] = with_promo_233_purchased;
    } else {
      item['with_promo'] = item['with_promo'] + with_promo_233_purchased;
    }
  }
}

params:

"params": {
  "p_123_purchased": {
    "key": "123_purchased"
  },
  "c_123_purchased": [{
    "key": "123_purchased"
  }],
  "v_123_purchased": "123_purchased",
  "p_233_purchased": {
    "key": "233_purchased"
  },
  "c_233_purchased": [{
    "key": "233_purchased"
  }],
  "v_233_purchased": "233_purchased",
  "key_123_purchased": "123_purchased",
  "name_123_purchased": 123,
  "action_123_purchased": "purchased",
  "price_123_purchased": 20,
  "count_123_purchased": 1,
  "last_date_123_purchased": "2015-07-04T07:11:03",
  "with_promo_123_purchased": 1,
  "key_233_purchased": "233_purchased",
  "name_233_purchased": 233,
  "action_233_purchased": "purchased",
  "price_233_purchased": 20,
  "count_233_purchased": 1,
  "last_date_233_purchased": "2015-07-04T07:11:03",
  "with_promo_233_purchased": 1
}