# How to fix index\_out\_of\_bounds\_exception exception in aggregation in painless script

**URL:** https://discuss.elastic.co/t/how-to-fix-index-out-of-bounds-exception-exception-in-aggregation-in-painless-script/144787
**Category:** Elasticsearch
**Created:** [August 16, 2018, 9:54pm UTC](https://discuss.elastic.co/t/how-to-fix-index-out-of-bounds-exception-exception-in-aggregation-in-painless-script/144787 "2018-08-16T21:54:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Steven\_Ensslen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/steven_ensslen/32/26984_2.png) [@Steven\_Ensslen](https://discuss.elastic.co/u/Steven_Ensslen)
#### Post date: [August 16, 2018, 9:54pm UTC](https://discuss.elastic.co/t/how-to-fix-index-out-of-bounds-exception-exception-in-aggregation-in-painless-script/144787/1 "2018-08-16T21:54:50Z")

</div>

I have what I believe to be an entirely normal index where 1% of the documents don't have a field present. I'd like to write a painless script that references that field, but whenever I do I get "Courier fetch: X of Y shards failed."

The response reads:  
`"reason": { "type": "script_exception", "reason": "runtime error", "script_stack": ["java.nio.Buffer.checkIndex(Buffer.java:540)", "java.nio.DirectByteBuffer.get(DirectByteBuffer.java:253)", "org.apache.lucene.store.ByteBufferGuard.getByte(ByteBufferGuard.java:118)", "org.apache.lucene.store.ByteBufferIndexInput$SingleBufferImpl.readByte(ByteBufferIndexInput.java:385)", "org.apache.lucene.util.packed.DirectReader$DirectPackedReader1.get(DirectReader.java:86)", "org.apache.lucene.codecs.lucene70.Lucene70DocValuesProducer$19.ordValue(Lucene70DocValuesProducer.java:865)", "org.apache.lucene.index.SingletonSortedSetDocValues.advanceExact(SingletonSortedSetDocValues.java:83)", "org.elasticsearch.index.fielddata.FieldData$10.advanceExact(FieldData.java:377)", "org.elasticsearch.index.fielddata.ScriptDocValues$BinaryScriptDocValues.setNextDocId(ScriptDocValues.java:588)", "org.elasticsearch.index.fielddata.ScriptDocValues$Strings.setNextDocId(ScriptDocValues.java:623)", "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:94)", "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)", "if (doc[fieldname] != null) {", " ^---- HERE" ], "script": "String safeGet(def doc, String fieldname) { if (doc[fieldname] != null) {return doc[fieldname].value;} else {return ' ';} } return safeGet(doc,'portal') + ' ' + safeGet(doc,'event')", "lang": "painless", "caused_by": { "type": "index_out_of_bounds_exception", "reason": null }`

I've also tried `contains_key()` and `.size() > 0` checks, neither of which worked for me.

I am using a JSON input field in a visualization in [Elastic.co](http://Elastic.co) hosted Kibana 6.3.1 .

I am aware that this question has been asked here [many](https://discuss.elastic.co/t/accessing-the-value-of-a-dynamic-key-in-an-object/136012) [times](https://discuss.elastic.co/t/how-to-check-a-doc-field-exist-while-creating-scripted-field-using-painless/74355) [before](https://discuss.elastic.co/t/how-to-check-for-existence-of-field-in-script/3815). I don't believe that any of those questions were answered to the satisfaction of the questioner, and I have tried all of the proposed "solutions", all of which return the same error.

---

<div class="post-metadata">

### Author: ![Steven\_Ensslen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/steven_ensslen/32/26984_2.png) [@Steven\_Ensslen](https://discuss.elastic.co/u/Steven_Ensslen)
#### Post date: [August 16, 2018, 11:17pm UTC](https://discuss.elastic.co/t/how-to-fix-index-out-of-bounds-exception-exception-in-aggregation-in-painless-script/144787/2 "2018-08-16T23:17:57Z")

</div>

On closer examination, this same error also occurs for fields that always exist.

The query Kibana is sending to elasticsearch is:  
{  
"size": 0,  
"\_source": {  
"excludes": []  
},  
"aggs": {  
"2": {  
"date\_histogram": {  
"field": "relevant\_at",  
"interval": "1d",  
"time\_zone": "Pacific/Auckland",  
"min\_doc\_count": 1  
},  
"aggs": {  
"3": {  
"terms": {  
"field": "ticket\_group\_name",  
"size": 5,  
"order": {  
"\_count": "desc"  
},  
"script": "doc['ticket\_group\_name'].value + ' ' + doc['ticket\_type\_name'].value;"  
}  
}  
}  
}  
},  
"stored\_fields": [  
"\*"  
],  
"script\_fields": {},  
"docvalue\_fields": [  
"@timestamp",  
"identity\_extra.from\_re",  
"purchased\_on",  
"relevant\_at",  
"start\_datetime"  
],  
"query": {  
"bool": {  
"must": [  
{  
"range": {  
"relevant\_at": {  
"gte": 1533856413951,  
"lte": 1534461213951,  
"format": "epoch\_millis"  
}  
}  
}  
],  
"filter": [  
{  
"match\_all": {}  
}  
],  
"should": [],  
"must\_not": []  
}  
}  
}

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [September 13, 2018, 11:18pm UTC](https://discuss.elastic.co/t/how-to-fix-index-out-of-bounds-exception-exception-in-aggregation-in-painless-script/144787/3 "2018-09-13T23:18:07Z")

</div>

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