How can I write Effective Painless script?

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:

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}

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

Could you try

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

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