# Passing script\_fields to Perl elasticsearch client

**URL:** <https://discuss.elastic.co/t/passing-script-fields-to-perl-elasticsearch-client/242647>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [July 26, 2020, 11:26am UTC](https://discuss.elastic.co/t/passing-script-fields-to-perl-elasticsearch-client/242647 "2020-07-26T11:26:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![igorid70](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igorid70/32/45136_2.png) [@igorid70](https://discuss.elastic.co/u/igorid70)\
**Post date:** [July 26, 2020, 11:26am UTC](https://discuss.elastic.co/t/passing-script-fields-to-perl-elasticsearch-client/242647/1 "2020-07-26T11:26:52Z")

</div>

Hi  
I use Elastic stack 6.8 and connect to extract certain documents based on parameters provided in input files with Elasticsearch official Perl client.  
All works well but I am unable to pass script\_fields parameter .  
I use the standard Perl modules (tried a few, same results) to generate a JSON object representing the "script\_fields" structure, however no matter what it keeps on failing with the same error  
"reason" : "Unknown key for a VALUE\_STRING in [script\_fields].",  
"type" : "parsing\_exception"  
If I remove passing script\_fields at all then the query is processed perfectly.  
I see that the script\_field is generated as follows in the TRACE file:

"script\_fields" : "{"calculated\_system.cpu.total.pct":{"script":{"lang":"painless","source":"if (doc.containsKey(\u0027system.cpu.cores\u0027)){doc[\u0027system.cpu.total.pct\u0027].value \* 100\/doc[\u0027system.cpu.cores\u0027].value"}}}"

which seems to be messed up with quotes and escaped characters. If I remove the most outer quotes, escape character \ before the quotes and have it like this

"script\_fields" : {"calculated\_system.cpu.total.pct":{"script":{"lang":"painless","source":"if (doc.containsKey(\u0027system.cpu.cores\u0027)){doc[\u0027system.cpu.total.pct\u0027].value \* 100/doc[\u0027system.cpu.cores\u0027].value}"}}}

then I am able to perfectly run it via curl from from 'Dev Tools' in Kibana.  
The same happens even if I generate the JSON manually making sure it is not "damaged" by extra quotes and escaping characters by Perl JSON modules.  
The printed Perl JSON object in debugger (before passing to search() function looks also valid

DB\<20\> p $json\_scripted\_fields  
{"calculated\_system.cpu.total.pct":{"script":{"lang":"painless","source":"if (doc.containsKey('system.cpu.cores')){doc['system.cpu.total.pct'].value \* 100/doc['system.cpu.cores'].value}"}}}

Thanks in advance for the assistance

---

<div class="post-metadata">

**Author:** ![igorid70](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igorid70/32/45136_2.png) [@igorid70](https://discuss.elastic.co/u/igorid70)\
**Post date:** [July 27, 2020, 8:27am UTC](https://discuss.elastic.co/t/passing-script-fields-to-perl-elasticsearch-client/242647/2 "2020-07-27T08:27:25Z")

</div>

Just for the record if someone comes across on something similar - after more digging and debugging the Perl ES client I was able to find the problem which was in my code - script\_fields expects to receive reference to a HASH representing the respective structure for all scripted fields (while I tried to send the JSON representing the same structure). I must admit that the official Perl client is awfully documented (contrary to the excellent Elastic documentation).

---

<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:** [August 24, 2020, 8:27am UTC](https://discuss.elastic.co/t/passing-script-fields-to-perl-elasticsearch-client/242647/3 "2020-08-24T08:27:28Z")

</div>

Closed on August 24, 2020, 8:27am UTC

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