# Scripted fields: Only first token of comma separated string is returned

**URL:** https://discuss.elastic.co/t/scripted-fields-only-first-token-of-comma-separated-string-is-returned/116298
**Category:** Kibana
**Created:** [January 19, 2018, 7:51pm UTC](https://discuss.elastic.co/t/scripted-fields-only-first-token-of-comma-separated-string-is-returned/116298 "2018-01-19T19:51:18Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![dr\_dob](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@dr\_dob](https://discuss.elastic.co/u/dr_dob)
#### Post date: [January 19, 2018, 7:51pm UTC](https://discuss.elastic.co/t/scripted-fields-only-first-token-of-comma-separated-string-is-returned/116298/1 "2018-01-19T19:51:18Z")

</div>

Hello

I've seen this issue reported before with no answer. I have a string of tokens separated by commas, such as "dog, cat, bird, etc," but only the first token is returned with this basic script:

return doc['fieldname.keyword'].value

The mapping for the field is the following:

```
      "Industry" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      }

```

So only "dog" is returned from the original field value "dog, cat, mouse, etc"

Any help would be great!  
Thanks

---

<div class="post-metadata">

### Author: ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)
#### Post date: [January 19, 2018, 8:41pm UTC](https://discuss.elastic.co/t/scripted-fields-only-first-token-of-comma-separated-string-is-returned/116298/2 "2018-01-19T20:41:41Z")

</div>

Could you provide a sample of one of the docs you get back from the following query:

```auto
POST <index-pattern>/_search 
{
  "query": {
    "match_all": {}
  },
  "docvalue_fields": ["Industry.keyword"],
  "_source": "Industry"
}

```

---

<div class="post-metadata">

### Author: ![dr\_dob](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@dr\_dob](https://discuss.elastic.co/u/dr_dob)
#### Post date: [January 19, 2018, 8:57pm UTC](https://discuss.elastic.co/t/scripted-fields-only-first-token-of-comma-separated-string-is-returned/116298/3 "2018-01-19T20:57:59Z")

</div>

> [@Bargs](#):
>
> POST \<index-pattern\>/\_search  
> {  
> "query": {  
> "match\_all": {}  
> },  
> "docvalue\_fields": ["Industry.keyword"],  
> "\_source": "Industry"  
> }

Sure thing:

{  
"\_index": "moviesandshows",  
"\_type": "moviesandshows",  
"\_id": "02f1b90d91ac05bfa335facd3fb89d08",  
"\_score": 1,  
"\_source": {  
"Industry": [  
"ActionandAdventure",  
"Drama",  
"Movies",  
"Thrillers"  
]  
},  
"fields": {  
"Industry.keyword": [  
"ActionandAdventure",  
"Drama",  
"Movies",  
"Thrillers"  
]  
}  
}

....Oh i see, its an array. but why? And can I leave it as an array, and convert it to a string?

---

<div class="post-metadata">

### Author: ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)
#### Post date: [January 19, 2018, 9:03pm UTC](https://discuss.elastic.co/t/scripted-fields-only-first-token-of-comma-separated-string-is-returned/116298/4 "2018-01-19T21:03:18Z")

</div>

What do the original documents look like when sending them to ES? Are you using logstash or ingest pipelines to split the strings into arrays perhaps?

---

<div class="post-metadata">

### Author: ![dr\_dob](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@dr\_dob](https://discuss.elastic.co/u/dr_dob)
#### Post date: [January 19, 2018, 9:05pm UTC](https://discuss.elastic.co/t/scripted-fields-only-first-token-of-comma-separated-string-is-returned/116298/5 "2018-01-19T21:05:24Z")

</div>

I'll take a look at that. Thanks for the help, it was really helpful. Can I can convert it from an array to a string in the scripted field? I'll look at the Painless API

---

<div class="post-metadata">

### Author: ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)
#### Post date: [January 19, 2018, 9:34pm UTC](https://discuss.elastic.co/t/scripted-fields-only-first-token-of-comma-separated-string-is-returned/116298/6 "2018-01-19T21:34:14Z")

</div>

Yep, Painless mainly [exposes the core Java API](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-api-reference.html) so you should be able to [String.join](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#join%2Djava.lang.CharSequence%2Djava.lang.Iterable%2D)

---

<div class="post-metadata">

### Author: ![dr\_dob](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@dr\_dob](https://discuss.elastic.co/u/dr_dob)
#### Post date: [January 19, 2018, 9:44pm UTC](https://discuss.elastic.co/t/scripted-fields-only-first-token-of-comma-separated-string-is-returned/116298/7 "2018-01-19T21:44:22Z")

</div>

I tried both

String t = Arrays.deepToString(doc['Industry.keyword'].value);  
return t

and

String t = "";

if (doc["Industry.keyword"].value.length != null && doc["Industry.keyword"].value.length != 0) {

String.join(t, doc["Industry.keyword"].value);

}

return t;

but they both result in "Courier Fetch: 1 of 5 shards failed." Any ideas?

---

<div class="post-metadata">

### Author: ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)
#### Post date: [January 19, 2018, 10:13pm UTC](https://discuss.elastic.co/t/scripted-fields-only-first-token-of-comma-separated-string-is-returned/116298/8 "2018-01-19T22:13:39Z")

</div>

I think the `.value` is causing the Iterable to automatically get casted to a string. Try doing:

`String.join(t, doc["Industry.keyword"]);`

---

<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: [February 16, 2018, 10:13pm UTC](https://discuss.elastic.co/t/scripted-fields-only-first-token-of-comma-separated-string-is-returned/116298/9 "2018-02-16T22:13:43Z")

</div>

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