# Scripted Fields creation issue In Kibana

**URL:** <https://discuss.elastic.co/t/scripted-fields-creation-issue-in-kibana/161540>\
**Category:** Kibana\
**Created:** [December 19, 2018, 2:37pm UTC](https://discuss.elastic.co/t/scripted-fields-creation-issue-in-kibana/161540 "2018-12-19T14:37:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sanj](https://avatars.discourse-cdn.com/v4/letter/s/e274bd/32.png) [@Sanj](https://discuss.elastic.co/u/Sanj)\
**Post date:** [December 19, 2018, 2:37pm UTC](https://discuss.elastic.co/t/scripted-fields-creation-issue-in-kibana/161540/1 "2018-12-19T14:37:15Z")

</div>

Hi Team,  
I'm trying to create a script field based in certain conditions. I'm a newbie to creating scripted fields so I'm facing troubles in creating it.

As of now my script looks like this.

```
		GET terracota_sample/_search
		{
			"query": {
				"match_all": {}
			},
			"script_fields": {
				"status": {
					"script": {
						"lang": "painless",
						"source": """
						if(doc['agentId.keyword'].value =~ /PerfWAG1/){
		  def perfWAG1Name = doc['name.keyword'];
		  def perfWAG1Size = doc['Size.keyword'].value;
		}
		else if(doc['agentId.keyword'].value =~ /PerfWAG2/){
		  def perfWAG2Name = doc['name.keyword'];
		  def perfWAG2Size = doc['Size.keyword'].value;
		}
		int status = 0;
		if (perfWAG1Name == perfWAG2Name) {
		  if (perfWAG1Size == perfWAG2Size){
			status = 1;
		  }else{
			status = -1;
		  }
		}
		return status;
						"""
					}
				}
			}
		}

```

When i test this in DevTools console, I'm getting this following error

```
		{
		  "error": {
			"root_cause": [
			  {
				"type": "script_exception",
				"reason": "compile error",
				"script_stack": [
				  "... tId.keyword'].value == ~ /PerfWAG1/){\r\n def perfW ...",
				  " ^---- HERE"
				],
				"script": " if(doc['agentId.keyword'].value == ~ /PerfWAG1/){\r\n def perfWAG1Name = doc['name.keyword'];\r\n def perfWAG1Size = doc['Size.keyword'].value;\r\n}\r\nelse if(doc['agentId.keyword'].value == ~ /PerfWAG2/){\r\n def perfWAG2Name = doc['name.keyword'];\r\n def perfWAG2Size = doc['Size.keyword'].value;\r\n}\r\nint status = 0;\r\nif (perfWAG1Name == perfWAG2Name) {\r\n if (perfWAG1Size == perfWAG2Size){\r\n status = 1;\r\n }else{\r\n status = -1;\r\n }\r\n}\r\nreturn status;",
				"lang": "painless"
			  }
			],
			"type": "search_phase_execution_exception",
			"reason": "all shards failed",
			"phase": "query",
			"grouped": true,
			"failed_shards": [
			  {
				"shard": 0,
				"index": "elkrft_ind_test",
				"node": "515B4tODSJqBR6WetvxydA",
				"reason": {
				  "type": "script_exception",
				  "reason": "compile error",
				  "script_stack": [
					"... tId.keyword'].value == ~ /PerfWAG1/){\r\n def perfW ...",
					" ^---- HERE"
				  ],
				  "script": " if(doc['agentId.keyword'].value == ~ /PerfWAG1/){\r\n def perfWAG1Name = doc['name.keyword'];\r\n def perfWAG1Size = doc['Size.keyword'].value;\r\n}\r\nelse if(doc['agentId.keyword'].value == ~ /PerfWAG2/){\r\n def perfWAG2Name = doc['name.keyword'];\r\n def perfWAG2Size = doc['Size.keyword'].value;\r\n}\r\nint status = 0;\r\nif (perfWAG1Name == perfWAG2Name) {\r\n if (perfWAG1Size == perfWAG2Size){\r\n status = 1;\r\n }else{\r\n status = -1;\r\n }\r\n}\r\nreturn status;",
				  "lang": "painless",
				  "caused_by": {
					"type": "illegal_state_exception",
					"reason": "Regexes are disabled. Set [script.painless.regex.enabled] to [true] in elasticsearch.yaml to allow them. Be careful though, regexes break out of Painless's protection against deep recursion and long loops."
				  }
				}
			  }
			]
		  },
		  "status": 500
		}

```

I wanna check if the field 'agentId' has "PerfWAG1" in it. Am I doing it wrong? Kindly Help me out with solving this issue.

Thank you.

---

<div class="post-metadata">

**Author:** ![rashmi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashmi/32/16391_2.png) [@rashmi](https://discuss.elastic.co/u/rashmi)\
**Post date:** [December 19, 2018, 6:48pm UTC](https://discuss.elastic.co/t/scripted-fields-creation-issue-in-kibana/161540/2 "2018-12-19T18:48:15Z")

</div>

@Jack_Conradson can you please take a look at this ? Thanks much - Rashmi

---

<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:** [January 16, 2019, 6:48pm UTC](https://discuss.elastic.co/t/scripted-fields-creation-issue-in-kibana/161540/3 "2019-01-16T18:48:22Z")

</div>

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