# How to add parameters to a scripted similarity?

**URL:** https://discuss.elastic.co/t/how-to-add-parameters-to-a-scripted-similarity/317731
**Category:** Elasticsearch
**Tags:** painless
**Created:** [October 29, 2022, 2:51pm UTC](https://discuss.elastic.co/t/how-to-add-parameters-to-a-scripted-similarity/317731 "2022-10-29T14:51:59Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![fsanzestevez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fsanzestevez/32/112675_2.png) [@fsanzestevez](https://discuss.elastic.co/u/fsanzestevez)
#### Post date: [October 29, 2022, 2:51pm UTC](https://discuss.elastic.co/t/how-to-add-parameters-to-a-scripted-similarity/317731/1 "2022-10-29T14:51:59Z")

</div>

I'm trying to make a parametrized scripted similarity function but I can't get it to work.  
I have created the script and have it stored.

However, when I try to implement it in the similarity score, I keep getting errors because of the symbol [params].

^ I know my script looks very similar to BM25, but I need to parametrized certain values during the query.

```auto
{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "... f = doc.freq/(doc.freq + params['k1'] * (1 - param ...",
          " ^---- HERE"
        ],
        "script" : "double boost = 2.2 * doc.freq; double tf = doc.freq/(doc.freq + params['k1'] * (1 - params['b'] + params['b'] * field('dl_avgdl'))); double idf = Math.log(1 + ((params['docs_count'] - term.docfreq + 0.5)/(term.docFreq + 0.5))); return query.boost * tf * idf * norm;",
        "lang" : "painless",
        "position" : {
          "offset" : 64,
          "start" : 39,
          "end" : 89
        }
      }
    ],
    "type" : "script_exception",
    "reason" : "compile error",
    "script_stack" : [
      "... f = doc.freq/(doc.freq + params['k1'] * (1 - param ...",
      " ^---- HERE"
    ],
    "script" : "double boost = 2.2 * doc.freq; double tf = doc.freq/(doc.freq + params['k1'] * (1 - params['b'] + params['b'] * field('dl_avgdl'))); double idf = Math.log(1 + ((params['docs_count'] - term.docfreq + 0.5)/(term.docFreq + 0.5))); return query.boost * tf * idf * norm;",
    "lang" : "painless",
    "position" : {
      "offset" : 64,
      "start" : 39,
      "end" : 89
    },
    "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "cannot resolve symbol [params]"
    }
  },
  "status" : 400
}

```

Is this possible to do with Elasticsearch 8.1?

---

<div class="post-metadata">

### Author: ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)
#### Post date: [October 31, 2022, 1:29pm UTC](https://discuss.elastic.co/t/how-to-add-parameters-to-a-scripted-similarity/317731/2 "2022-10-31T13:29:48Z")

</div>

Hi @fsanzestevez, unfortunately `params` is not available for similarity scripts. So the script will need to embed the constants.

See the [similarity context docs](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-similarity-context.html) for all the variables available to similarity scripts.

---

<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: [November 28, 2022, 1:30pm UTC](https://discuss.elastic.co/t/how-to-add-parameters-to-a-scripted-similarity/317731/3 "2022-11-28T13:30:13Z")

</div>

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