Kibana5.4 scripted field access to array by index

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.

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:

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

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 ?

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.

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 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.

@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.

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

Now its working
Thank you @spalger :smile:

1 Like

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