# Kibana5.4 scripted field access to array by index

**URL:** <https://discuss.elastic.co/t/kibana5-4-scripted-field-access-to-array-by-index/89445>\
**Category:** Kibana\
**Created:** [June 14, 2017, 7:06pm UTC](https://discuss.elastic.co/t/kibana5-4-scripted-field-access-to-array-by-index/89445 "2017-06-14T19:06:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpuserh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rpuserh/32/63367_2.png) [@rpuserh](https://discuss.elastic.co/u/rpuserh)\
**Post date:** [June 14, 2017, 7:06pm UTC](https://discuss.elastic.co/t/kibana5-4-scripted-field-access-to-array-by-index/89445/1 "2017-06-14T19:06:45Z")

</div>

Hi,  
Im running kibana5.4 and ES 5.4  
I have data like this sensor\_data : 3, 5, 4 and I need to do aggregation on each number.  
Mapping for this field is ("type": "long")  
When I'm trying to filter sensor\_data:"3, 5, 4" Kibana is giving an error

It will be better solution for me to do scripted fields by selecting index for array but script like this is giving error  
doc["sensor\_data"].value[1]

And script like this taking first array value  
doc["sensor\_data"].value (this will return me 3)

Please help on this.

---

<div class="post-metadata">

**Author:** ![spalger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spalger/32/14092_2.png) [@spalger](https://discuss.elastic.co/u/spalger)\
**Post date:** [June 14, 2017, 7:39pm UTC](https://discuss.elastic.co/t/kibana5-4-scripted-field-access-to-array-by-index/89445/2 "2017-06-14T19:39:47Z")

</div>

You would probably have a better experience if you pre-processed this sensor data to give names to each value, but you can access these values with painless with a script like this:

```auto
doc['sensor_data'][0] // for 3
doc['sensor_data'][1] // for 4
doc['sensor_data'][2] // for 5

```

---

<div class="post-metadata">

**Author:** ![rpuserh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rpuserh/32/63367_2.png) [@rpuserh](https://discuss.elastic.co/u/rpuserh)\
**Post date:** [June 14, 2017, 8:01pm UTC](https://discuss.elastic.co/t/kibana5-4-scripted-field-access-to-array-by-index/89445/3 "2017-06-14T20:01:04Z")

</div>

Thank you for response @spalger  
I tried that on this data : 0, 1, 0  
doc['sensor\_data'][0] // value 0  
doc['sensor\_data'][1] // value 0  
doc['sensor\_data'][2] // value 1

and for this  
1, 0, 0  
doc['sensor\_data'][0] // value 0  
doc['sensor\_data'][1] // value 0  
doc['sensor\_data'][2] // value 1  
And the same for  
0, 0, 1  
doc['sensor\_data'][0] // value 0  
doc['sensor\_data'][1] // value 0  
doc['sensor\_data'][2] // value 1  
Why this work like this ?

---

<div class="post-metadata">

**Author:** ![rpuserh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rpuserh/32/63367_2.png) [@rpuserh](https://discuss.elastic.co/u/rpuserh)\
**Post date:** [June 14, 2017, 8:21pm UTC](https://discuss.elastic.co/t/kibana5-4-scripted-field-access-to-array-by-index/89445/4 "2017-06-14T20:21:32Z")

</div>

Oh I tried to convert it to string  
but it sorting data when you are calling doc['sensor\_data']

Is there any way to do not sort ?

Thanks In advance.

---

<div class="post-metadata">

**Author:** ![spalger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spalger/32/14092_2.png) [@spalger](https://discuss.elastic.co/u/spalger)\
**Post date:** [June 14, 2017, 8:30pm UTC](https://discuss.elastic.co/t/kibana5-4-scripted-field-access-to-array-by-index/89445/5 "2017-06-14T20:30:46Z")

</div>

Interesting, perhaps it's sorting the values before injecting them... I don't have access to a testing environment at this moment, but can you try accessing `doc._source`? [This doc](https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-fields.html) seems to imply that it should be available, which should have the values as they were passed when you originally indexed the doc.

You would probably be better off pre-processing the data in something like logstash before indexing it into Elasticsearch. If you don't have that option then perhaps you can index the data as a string and then split the string in painless, converting it to an ordered list once you need it.

---

<div class="post-metadata">

**Author:** ![rpuserh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rpuserh/32/63367_2.png) [@rpuserh](https://discuss.elastic.co/u/rpuserh)\
**Post date:** [June 14, 2017, 9:20pm UTC](https://discuss.elastic.co/t/kibana5-4-scripted-field-access-to-array-by-index/89445/6 "2017-06-14T21:20:20Z")

</div>

@spalger I have no option to preprocess the data we already have a lot of data in ES like that  
I tried to do this  
doc.\_source.sensor\_data  
\_source.sensor\_data  
It giving me an error (Discover: compile error, other one runtime error)

I will be very thankful if you help me to solve this.

---

<div class="post-metadata">

**Author:** ![rpuserh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rpuserh/32/63367_2.png) [@rpuserh](https://discuss.elastic.co/u/rpuserh)\
**Post date:** [June 14, 2017, 9:38pm UTC](https://discuss.elastic.co/t/kibana5-4-scripted-field-access-to-array-by-index/89445/7 "2017-06-14T21:38:01Z")

</div>

Yep Finally done that  
params['\_source']['sensordata'][0]  
params['\_source']['sensordata'][1]  
params['\_source']['sensordata'][2]

Now its working  
Thank you @spalger 😄

---

<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:** [July 12, 2017, 9:38pm UTC](https://discuss.elastic.co/t/kibana5-4-scripted-field-access-to-array-by-index/89445/8 "2017-07-12T21:38:02Z")

</div>

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