# Truncating String in Data Table

**URL:** https://discuss.elastic.co/t/truncating-string-in-data-table/154876
**Category:** Kibana
**Created:** [October 31, 2018, 3:51pm UTC](https://discuss.elastic.co/t/truncating-string-in-data-table/154876 "2018-10-31T15:51:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)
#### Post date: [October 31, 2018, 3:51pm UTC](https://discuss.elastic.co/t/truncating-string-in-data-table/154876/1 "2018-10-31T15:51:37Z")

</div>

I have a data table visualization where I am counting the top hits on a list of URLs. All the URLs have `http://www.example.com/index`. I am counting top hits on the part after `/index`. Is it possible to truncate the root portion of the URL within the Kibana visualization without modifying the Elasticsearch stored data?

---

<div class="post-metadata">

### Author: ![thomasneirynck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomasneirynck/32/23313_2.png) [@thomasneirynck](https://discuss.elastic.co/u/thomasneirynck)
#### Post date: [October 31, 2018, 4:09pm UTC](https://discuss.elastic.co/t/truncating-string-in-data-table/154876/2 "2018-10-31T16:09:59Z")

</div>

hi @wwalker,

you'll want to look at scripted fields in Kibana, [https://www.elastic.co/guide/en/kibana/current/scripted-fields.html](https://www.elastic.co/guide/en/kibana/current/scripted-fields.html). This will allow you to truncate the url on the-fly, without changing the underlying storage.

---

<div class="post-metadata">

### Author: ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)
#### Post date: [October 31, 2018, 5:05pm UTC](https://discuss.elastic.co/t/truncating-string-in-data-table/154876/3 "2018-10-31T17:05:37Z")

</div>

Looks exactly like what I would want....except that I don't know any programming language to get it to do what I want, lol. I'll see what I can learn about painless.

---

<div class="post-metadata">

### Author: ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)
#### Post date: [October 31, 2018, 8:23pm UTC](https://discuss.elastic.co/t/truncating-string-in-data-table/154876/4 "2018-10-31T20:23:06Z")

</div>

OK, working with the example documentation, I've gotten close...I think. However, when I run the preview, I get the below error. I think it has to do with the field I am working with having a space in it, but I'm not sure what the syntax is that I am supposed to use. I've tried single, double, and triple quotes around the field path but all result in the same error.

**Painless Script:**  
`"'ctx._source.Full URL'" = /https:\/\/www.example.com\/government\/elections*\/.matcher("'ctx._source.Full URL'").replaceAll('');`

**Resulting error**  
`"shard": 0, "index": "cloudflare-2018.10.31", "node": "pPGsP09BSNeNANYO6A9KEQ", "reason": { "type": "script_exception", "reason": "compile error", "script_stack": [], "script": "\"'ctx._source.Full URL'\" = /https:\\/\\/www.example.com\\/government\\/elections*\\/.matcher(\"'ctx._source.Full URL'\").replaceAll('');", "lang": "painless", "caused_by": { "type": "illegal_argument_exception", "reason": "Left-hand side cannot be assigned a value."`

---

<div class="post-metadata">

### Author: ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)
#### Post date: [October 31, 2018, 9:01pm UTC](https://discuss.elastic.co/t/truncating-string-in-data-table/154876/5 "2018-10-31T21:01:49Z")

</div>

Figured the syntax out...and fixed my jacked up regex.

`ctx._source['Full URL'] = /https:\/\/example\.com\/government\/elections\//.matcher(ctx._source['Full URL']).replaceAll('')`

The field was accepted and created but now I get "1 of 2 shards failed" and no results in the Discover regardless of the time picker range I select. If I delete the scripted field, Discover shows everything again.

Any ideas on what I did wrong?

---

<div class="post-metadata">

### Author: ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)
#### Post date: [October 31, 2018, 9:38pm UTC](https://discuss.elastic.co/t/truncating-string-in-data-table/154876/6 "2018-10-31T21:38:30Z")

</div>

In addition to Discover not showing any data, if I create a visualization and select the newly created field, I get the following error:

```
{
	"error": {
		"root_cause": [
			{
				"type": "script_exception",
				"reason": "runtime error",
				"script_stack": [
					"ctx._source['Full URL'] == /https:\\/\\/example\\.com\\/government\\/elections\\//i.matcher(ctx._source['Full URL']).replaceAll('')",
					" ^---- HERE"
				],
				"script": "ctx._source['Full URL'] == /https:\\/\\/example\\.com\\/government\\/elections\\//i.matcher(ctx._source['Full URL']).replaceAll('')",
				"lang": "painless"
			},
			{
				"type": "script_exception",
				"reason": "runtime error",
				"script_stack": [
					"ctx._source['Full URL'] == /https:\\/\\/example\\.com\\/government\\/elections\\//i.matcher(ctx._source['Full URL']).replaceAll('')",
					" ^---- HERE"
				],
				"script": "ctx._source['Full URL'] == /https:\\/\\/example\\.com\\/government\\/elections\\//i.matcher(ctx._source['Full URL']).replaceAll('')",
				"lang": "painless"
			}
		],
		"type": "search_phase_execution_exception",
		"reason": "all shards failed",
		"phase": "query",
		"grouped": true,
		"failed_shards": [
			{
				"shard": 0,
				"index": "cloudflare-2018.10.30",
				"node": "pPGsP09BSNeNANYO6A9KEQ",
				"reason": {
					"type": "script_exception",
					"reason": "runtime error",
					"script_stack": [
						"ctx._source['Full URL'] == /https:\\/\\/example\\.com\\/government\\/elections\\//i.matcher(ctx._source['Full URL']).replaceAll('')",
						" ^---- HERE"
					],
					"script": "ctx._source['Full URL'] == /https:\\/\\/example\\.com\\/government\\/elections\\//i.matcher(ctx._source['Full URL']).replaceAll('')",
					"lang": "painless",
					"caused_by": {
						"type": "null_pointer_exception",
						"reason": null
					}
				}
			},
			{
				"shard": 0,
				"index": "cloudflare-2018.10.31",
				"node": "pPGsP09BSNeNANYO6A9KEQ",
				"reason": {
					"type": "script_exception",
					"reason": "runtime error",
					"script_stack": [
						"ctx._source['Full URL'] == /https:\\/\\/example\\.com\\/government\\/elections\\//i.matcher(ctx._source['Full URL']).replaceAll('')",
						" ^---- HERE"
					],
					"script": "ctx._source['Full URL'] == /https:\\/\\/example\\.com\\/government\\/elections\\//i.matcher(ctx._source['Full URL']).replaceAll('')",
					"lang": "painless",
					"caused_by": {
						"type": "null_pointer_exception",
						"reason": null
					}
				}
			}
		]
	},
	"status": 500
}
```

---

<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: [November 28, 2018, 9:38pm UTC](https://discuss.elastic.co/t/truncating-string-in-data-table/154876/7 "2018-11-28T21:38:41Z")

</div>

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