Passing script_fields to Perl elasticsearch client

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

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).

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