# Kibana Scripted Field, Return value from string out of an array

**URL:** <https://discuss.elastic.co/t/kibana-scripted-field-return-value-from-string-out-of-an-array/168787>\
**Category:** Kibana\
**Created:** [February 18, 2019, 8:57am UTC](https://discuss.elastic.co/t/kibana-scripted-field-return-value-from-string-out-of-an-array/168787 "2019-02-18T08:57:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo\_Faria](https://avatars.discourse-cdn.com/v4/letter/h/87869e/32.png) [@Hugo\_Faria](https://discuss.elastic.co/u/Hugo_Faria)\
**Post date:** [February 18, 2019, 8:57am UTC](https://discuss.elastic.co/t/kibana-scripted-field-return-value-from-string-out-of-an-array/168787/1 "2019-02-18T08:57:31Z")

</div>

Hello,  
looking for some help to "extract" the first value in an array inside a document.

I have this field called " orchestratorOutput.categoricalListData.ACPredictions " that is logging an array of values (ex.: OTHER, PYMTCONF2, COMPCRI, PYMTCONF, SPAM, OOOAA, R4C\_GENERAL, R4C\_LIFT, R4C\_ABROAD, R4C\_DEATH, R4C\_OUTPORT, INTTEST, R4C\_TOGETHER, R4C\_TELECLUB, R4C\_RETIREMENT, SPONS, R4C\_POST2PRE, R4C\_EMPLLEAVES, R4C\_DATASUB, R4C\_COMPANYCLOSED)

now, I need to return in an new field, just the first value (before the ",")...  
I tried it in several ways, but i'm not able to return it...

f.ex: if I use just doc['orchestratorOutput.categoricalListData.ACPredictions.keyword'].value it returns always "COMPCRI" that is the first value in alphabetical order...  
any tips how to do it?  
thanks a lot.

ps. I'm just a "business"-guy with very limited tech skills 😛

---

<div class="post-metadata">

**Author:** ![bhavyarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bhavyarm/32/22392_2.png) [@bhavyarm](https://discuss.elastic.co/u/bhavyarm)\
**Post date:** [February 18, 2019, 4:44pm UTC](https://discuss.elastic.co/t/kibana-scripted-field-return-value-from-string-out-of-an-array/168787/2 "2019-02-18T16:44:29Z")

</div>

@Marius_Dragomir can we please help this user?

Thanks,  
Bhavya

---

<div class="post-metadata">

**Author:** ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)\
**Post date:** [February 18, 2019, 4:57pm UTC](https://discuss.elastic.co/t/kibana-scripted-field-return-value-from-string-out-of-an-array/168787/3 "2019-02-18T16:57:05Z")

</div>

This is a bit difficult to do with Elasticsearch as all arrays that are indexed in Elasticsearch won't be ordered, they will be like a bag of elements. The simplest way would be to either get the first element at ingest or to save the field as a string(again, at ingest, before it was indexed).  
Otherwise, you would have to enable the `_source` field and then use something like:  
`params._source['orchestratorOutput.categoricalListData.ACPredictions']` in order to access the ordered array.

---

<div class="post-metadata">

**Author:** ![Hugo\_Faria](https://avatars.discourse-cdn.com/v4/letter/h/87869e/32.png) [@Hugo\_Faria](https://discuss.elastic.co/u/Hugo_Faria)\
**Post date:** [February 19, 2019, 6:19am UTC](https://discuss.elastic.co/t/kibana-scripted-field-return-value-from-string-out-of-an-array/168787/4 "2019-02-19T06:19:46Z")

</div>

Hello @Marius_Dragomir, thanks a lot for your support!  
luckily \_source field is already enabled...

I tried with your suggested code and I always get " - " as result.

then I tried just params.\_source.orchestratorOutput.categoricalListData.ACPredictions and it returns the full content.  
what I would need is just the first word before the first " , "

thanks again and have a nice day 🙂  
cheers  
Hugo

---

<div class="post-metadata">

**Author:** ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)\
**Post date:** [February 19, 2019, 10:43am UTC](https://discuss.elastic.co/t/kibana-scripted-field-return-value-from-string-out-of-an-array/168787/5 "2019-02-19T10:43:37Z")

</div>

You can some java functions for this:

```auto
text = params._source.orchestratorOutput.categoricalListData.ACPredictions;
first_comma = text.indexof(',');
if (first_comma != -1) {
return "Empty"
} else {
return text.substring(0, first_comma)
}
```

---

<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:** [March 19, 2019, 10:43am UTC](https://discuss.elastic.co/t/kibana-scripted-field-return-value-from-string-out-of-an-array/168787/6 "2019-03-19T10:43:47Z")

</div>

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