# Scripted field help

**URL:** <https://discuss.elastic.co/t/scripted-field-help/130152>\
**Category:** Kibana\
**Created:** [May 1, 2018, 7:59pm UTC](https://discuss.elastic.co/t/scripted-field-help/130152 "2018-05-01T19:59:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jskuyper](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@jskuyper](https://discuss.elastic.co/u/jskuyper)\
**Post date:** [May 1, 2018, 7:59pm UTC](https://discuss.elastic.co/t/scripted-field-help/130152/1 "2018-05-01T19:59:52Z")

</div>

I am attempting to create a new scripted field assignee.role where there may be thousands of possibilities, but only want to make it defined for a subset of the possibilities, how do I lump everyone else into the undefined group?  
//  
\<  
if(doc['assigneename.keyword'].value == 'mike') {  
return "Generalist";  
} else if(doc['assigneename.keyword'].value == 'bill') {  
return "Generalist";  
} else if(doc['assigneename.keyword'].value == 'josh') {  
return "Generalist";  
} else if(doc['assigneename.keyword'].value == 'sam') {  
return "SME";  
} else if(doc['assigneename.keyword'].value == 'blake') {  
return "SME";  
} else if(doc['assigneename.keyword'].value == 'bob') {  
return "SME";  
} else if(doc['assigneename.keyword'].value == 'jake') {  
return "SME";   
} else if(doc['assigneename.keyword'].value == 'amy') {  
return "SME";   
} else if(doc['assigneename.keyword'].value == 'frank') {  
return "SME";   
} else if(doc['assigneename.keyword'].value == 'brian') {  
return "SME";  
} else if(doc['assigneename.keyword'].value == '') {  
return "undefined";

/\>

---

<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:** [May 1, 2018, 9:16pm UTC](https://discuss.elastic.co/t/scripted-field-help/130152/2 "2018-05-01T21:16:08Z")

</div>

You can probably just add `else { return "my other group" }` to the end

---

<div class="post-metadata">

**Author:** ![jskuyper](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@jskuyper](https://discuss.elastic.co/u/jskuyper)\
**Post date:** [May 2, 2018, 2:02am UTC](https://discuss.elastic.co/t/scripted-field-help/130152/3 "2018-05-02T02:02:06Z")

</div>

so someething like this? I seem to get an error when doing so  
\<  
if(doc['assigneename.keyword'].value == 'jim') {  
return "Generalist";  
} else if(doc['assigneename.keyword'].value == 'bill') {  
return "Generalist";  
} else if(doc['assigneename.keyword'].value == 'josh') {  
return "Generalist";  
} else if(doc['assigneename.keyword'].value == 'sam') {  
return "SME";  
} else if(doc['assigneename.keyword'].value == 'blake') {  
return "SME";  
} else if(doc['assigneename.keyword'].value == 'bob') {  
return "SME";  
} else if(doc['assigneename.keyword'].value == 'jake') {  
return "SME";   
} else if(doc['assigneename.keyword'].value == 'amy') {  
return "SME";   
} else if(doc['assigneename.keyword'].value == 'frank') {  
return "SME";   
} else if(doc['assigneename.keyword'].value == 'brian') {  
return "SME";  
} else {  
return "undefined";  
}  
/\>

---

<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:** [May 2, 2018, 4:46am UTC](https://discuss.elastic.co/t/scripted-field-help/130152/4 "2018-05-02T04:46:49Z")

</div>

What error are you receiving?

---

<div class="post-metadata">

**Author:** ![jskuyper](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@jskuyper](https://discuss.elastic.co/u/jskuyper)\
**Post date:** [May 2, 2018, 1:29pm UTC](https://discuss.elastic.co/t/scripted-field-help/130152/5 "2018-05-02T13:29:16Z")

</div>

```auto
Error: Request to Elasticsearch failed: {"error":{"root_cause":[{"type":"script_exception","reason":"compile error","script_stack":["... {\n return \"undefined\";"," ^---- HERE"],"script":"if(doc['assigneename.keyword'].value == 'jim') {\n return \"Generalist\";\n } else if(doc['assigneename.keyword'].value == 'helen') {\n return \"Generalist\";\n } else if(doc['assigneename.keyword'].value == 'mark') {\n return \"Generalist\";\n } else if(doc['assigneename.keyword'].value == 'frank') {\n return \"SME\";\n } else if(doc['assigneename.keyword'].value == 'jake') {\n return \"SME\";\n } else if(doc['assigneename.keyword'].value == 'bill') {\n return \"SME\";\n } else if(doc['assigneename.keyword'].value == 'josh') {\n return \"SME\";\t\n } else if(doc['assigneename.keyword'].value == 'bob') {\n return \"SME\";\t\n } else if(doc['assigneename.keyword'].value == 'steve') {\n return \"SME\";\t\n } else if(doc['assigneename.keyword'].value == 'sam') {\n return \"SME\";\n } else if(doc['assigneename.keyword'].value == '') {\n return \"undefined\";","lang":"painless"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"doc-itsm-im","node":"yguTWglBReCEOSRjIJx00w","reason":{"type":"script_exception","reason":"compile error","script_stack":["... {\n return \"undefined\";"," ^---- HERE"],"script":"if(doc['assigneename.keyword'].value == 'jim') {\n return \"Generalist\";\n } else if(doc['assigneename.keyword'].value == 'helen') {\n return \"Generalist\";\n } else if(doc['assigneename.keyword'].value == 'mark') {\n return \"Generalist\";\n } else if(doc['assigneename.keyword'].value == 'frank') {\n return \"SME\";\n } else if(doc['assigneename.keyword'].value == 'jake') {\n return \"SME\";\n } else if(doc['assigneename.keyword'].value == 'bill') {\n return \"SME\";\n } else if(doc['assigneename.keyword'].value == 'josh') {\n return \"SME\";\t\n } else if(doc['assigneename.keyword'].value == 'bob') {\n return \"SME\";\t\n } else if(doc['assigneename.keyword'].value == 'steve') {\n return \"SME\";\t\n } else if(doc['assigneename.keyword'].value == 'sam') {\n return \"SME\";\n } else if(doc['assigneename.keyword'].value == '') {\n return \"undefined\";","lang":"painless","caused_by":{"type":"illegal_argument_exception","reason":"unexpected token ['<EOF>'] was expecting one of ['}']."}}}]},"status":500}
    at kibana/bundles/kibana.bundle.js?v=15555:237:1333
    at Function.Promise.try (kibana/bundles/commons.bundle.js?v=15555:91:24383)
    at kibana/bundles/commons.bundle.js?v=15555:91:23753
    at Array.map (<anonymous>)
    at Function.Promise.map (kibana/bundles/commons.bundle.js?v=15555:91:23708)
    at callResponseHandlers (kibana/bundles/kibana.bundle.js?v=15555:237:949)
    at kibana/bundles/kibana.bundle.js?v=15555:236:20482
    at processQueue (kibana/bundles/commons.bundle.js?v=15555:38:23621)
    at kibana/bundles/commons.bundle.js?v=15555:38:23888
    at Scope.$eval (kibana/bundles/commons.bundle.js?v=15555:39:4619)
    at Scope.$digest (kibana/bundles/commons.bundle.js?v=15555:39:2359)
    at Scope.$apply (kibana/bundles/commons.bundle.js?v=15555:39:5037)
    at done (kibana/bundles/commons.bundle.js?v=15555:37:25027)
    at completeRequest (kibana/bundles/commons.bundle.js?v=15555:37:28702)
    at XMLHttpRequest.xhr.onload (kibana/bundles/commons.bundle.js?v=15555:37:29634)

```

---

<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:** [May 2, 2018, 6:45pm UTC](https://discuss.elastic.co/t/scripted-field-help/130152/6 "2018-05-02T18:45:03Z")

</div>

Looks like elasticsearch isn't getting the closing `}` curly bracket.

---

<div class="post-metadata">

**Author:** ![jskuyper](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@jskuyper](https://discuss.elastic.co/u/jskuyper)\
**Post date:** [May 2, 2018, 7:41pm UTC](https://discuss.elastic.co/t/scripted-field-help/130152/7 "2018-05-02T19:41:34Z")

</div>

Seems like I have all the right brackets

---

<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:** [May 2, 2018, 7:54pm UTC](https://discuss.elastic.co/t/scripted-field-help/130152/8 "2018-05-02T19:54:25Z")

</div>

Can you send a screenshot of the script in Kibana? The source Elasticsearch returns in the error message definitely doesn't have one after `return "undefined";`

---

<div class="post-metadata">

**Author:** ![jskuyper](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@jskuyper](https://discuss.elastic.co/u/jskuyper)\
**Post date:** [May 3, 2018, 7:33pm UTC](https://discuss.elastic.co/t/scripted-field-help/130152/9 "2018-05-03T19:33:39Z")

</div>

![image](https://us1.discourse-cdn.com/elastic/original/3X/5/5/55ff835b73aaee9b1c3e72bc48ce7d38dd8f7c7d.png)

---

<div class="post-metadata">

**Author:** ![jskuyper](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@jskuyper](https://discuss.elastic.co/u/jskuyper)\
**Post date:** [May 3, 2018, 8:09pm UTC](https://discuss.elastic.co/t/scripted-field-help/130152/10 "2018-05-03T20:09:00Z")

</div>

Seems to work now... seems like a bad indentation caused it. Thanks!

---

<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:** [May 31, 2018, 8:09pm UTC](https://discuss.elastic.co/t/scripted-field-help/130152/11 "2018-05-31T20:09:00Z")

</div>

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