# \[elasticsearch-js\] putScript example

**URL:** https://discuss.elastic.co/t/elasticsearch-js-putscript-example/143721
**Category:** Elasticsearch
**Created:** [August 9, 2018, 2:38pm UTC](https://discuss.elastic.co/t/elasticsearch-js-putscript-example/143721 "2018-08-09T14:38:30Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![andrhamm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrhamm/32/30607_2.png) [@andrhamm](https://discuss.elastic.co/u/andrhamm)
#### Post date: [August 9, 2018, 2:38pm UTC](https://discuss.elastic.co/t/elasticsearch-js-putscript-example/143721/1 "2018-08-09T14:38:30Z")

</div>

Looking at [the documentation for the `putScript` method](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference-5-6.html#api-putscript-5-6), the main params are `id`, `lang`, and `body`:

 ![28%20AM](https://us1.discourse-cdn.com/elastic/original/3X/8/5/85e809c3ccf597e6b1ad2da52e276c720851be83.png)

Having reviewed the [`POST _scripts/{id}` request examples](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/modules-scripting-using.html#_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'
});

client.putScript({
  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!

---

<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: [September 6, 2018, 2:38pm UTC](https://discuss.elastic.co/t/elasticsearch-js-putscript-example/143721/2 "2018-09-06T14:38:32Z")

</div>

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