# How can I write Effective Painless script?

**URL:** <https://discuss.elastic.co/t/how-can-i-write-effective-painless-script/218819>\
**Category:** Kibana\
**Created:** [February 11, 2020, 4:25pm UTC](https://discuss.elastic.co/t/how-can-i-write-effective-painless-script/218819 "2020-02-11T16:25:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yusuf\_Karatoprak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yusuf_karatoprak/32/44539_2.png) [@Yusuf\_Karatoprak](https://discuss.elastic.co/u/Yusuf_Karatoprak)\
**Post date:** [February 11, 2020, 4:25pm UTC](https://discuss.elastic.co/t/how-can-i-write-effective-painless-script/218819/1 "2020-02-11T16:25:16Z")

</div>

Yes; You have really great script lang but one day, I will be cancer because of poor documentation and knowledge of painless so it makes painless ghost language there is no great full example. I got stuck with below code can you help me?  
MyCode:

```auto
doc['UserId.keyword '].value!=doc['SessionId.keyword '].value ? 'Registered':'Anonymous';

```

Error

```
Error with Painless scripted field 'doc['UserId.keyword '].value!=doc['SessionId.keyword '].value ? 'Registered':'Anonymous';'.

```

You can address this error by editing the 'doc['UserId.keyword '].value!=doc['SessionId.keyword '].value ? 'Registered':'Anonymous';' field in Management \> Index Patterns, under the “Scripted fields” tab.

Request to Elasticsearch failed: {"error":{"root\_cause":[{"type":"script\_exception","reason":"runtime error","script\_stack":["org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:81)","org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)","doc['UserId.keyword '].value!=doc['SessionId.keyword '].value ? 'Registered':'Anonymous';"," ^---- HERE"],"script":"doc['UserId.keyword '].value!=doc['SessionId.keyword '].value ? 'Registered':'Anonymous';","lang":"painless"}],"type":"search\_phase\_execution\_exception","reason":"all shards failed","phase":"fetch","grouped":true,"failed\_shards":[{"shard":0,"index":"usereventsreduced-2020","node":"TBSUPCkhQ1aHX069zwT7Tg","reason":{"type":"script\_exception","reason":"runtime error","script\_stack":["org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:81)","org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)","doc['UserId.keyword '].value!=doc['SessionId.keyword '].value ? 'Registered':'Anonymous';"," ^---- HERE"],"script":"doc['UserId.keyword '].value!=doc['SessionId.keyword '].value ? 'Registered':'Anonymous';","lang":"painless","caused\_by":{"type":"illegal\_argument\_exception","reason":"No field found for [UserId.keyword] in mapping with types "}}}]},"status":500}

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [February 11, 2020, 4:30pm UTC](https://discuss.elastic.co/t/how-can-i-write-effective-painless-script/218819/2 "2020-02-11T16:30:05Z")

</div>

It looks like you have a trailing whitespace there in the fieldname `'UserId.keyword '` instead of `'UserId.keyword'`.

Could you try

```auto
doc['UserId.keyword'].value!=doc['SessionId.keyword'].value ? 'Registered':'Anonymous';

```

---

<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 10, 2020, 4:30pm UTC](https://discuss.elastic.co/t/how-can-i-write-effective-painless-script/218819/3 "2020-03-10T16:30:06Z")

</div>

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