[elasticsearch-js] putScript example

Looking at the documentation for the putScript method, the main params are id, lang, and body:

Having reviewed the POST _scripts/{id} request examples, I expected the body param to be expecting the same structure ({ "script": { "lang": "painless", "source": "Math.log(_score * 2) + params.my_modifier"}}).

After a LOT of trial and error, I have a working example of what is expected (the documentation and the way the elasticsearch-js source code works, it was very difficult to determine what is needed). The documentation for the putScript example should definitely be improved!

const elasticsearch = require('elasticsearch');
const client = new elasticsearch.Client({
  host: 'localhost:9200',
  log: 'trace',
  apiVersion: '5.6'

  id: 'calculate-score',
  lang: "painless",
  body: {
    script: "Math.log(_score * 2) + params.my_modifier"

Looking at the debug output, the example makes more sense because the actual endpoint being called by the client is (I haven't come across docs about this endpoint, only the POST version, linked above):

Elasticsearch INFO: 2018-08-09T14:33:10Z
  Adding connection to http://localhost:9200/

Elasticsearch DEBUG: 2018-08-09T14:33:10Z
  starting request {
    "method": "PUT",
    "path": "/_scripts/painless/calculate-score",
    "body": {
      "script": "Math.log(_score * 2) + params.my_modifier"
    "query": {}

Elasticsearch TRACE: 2018-08-09T14:33:10Z
  -> PUT http://localhost:9200/_scripts/painless/calculate-score
    "script": "Math.log(_score * 2) + params.my_modifier"
  <- 200
    "acknowledged": true

Elasticsearch DEBUG: 2018-08-09T14:33:10Z
  Request complete

I hope this helps somebody!

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