# L2norm script in source compiles error because Cannot cast from \[double\] to \[int\]

**URL:** https://discuss.elastic.co/t/l2norm-script-in-source-compiles-error-because-cannot-cast-from-double-to-int/347799
**Category:** Elasticsearch
**Tags:** painless
**Created:** [November 23, 2023, 1:38am UTC](https://discuss.elastic.co/t/l2norm-script-in-source-compiles-error-because-cannot-cast-from-double-to-int/347799 "2023-11-23T01:38:56Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![wensi](https://avatars.discourse-cdn.com/v4/letter/w/e95f7d/32.png) [@wensi](https://discuss.elastic.co/u/wensi)
#### Post date: [November 23, 2023, 1:38am UTC](https://discuss.elastic.co/t/l2norm-script-in-source-compiles-error-because-cannot-cast-from-double-to-int/347799/1 "2023-11-23T01:38:56Z")

</div>

```auto
"source": "double norm=l2norm(params.queryVector, 'vec')^2; return 1/(1+norm);"

```

or

```auto
"source": "1/(1+l2norm(params.queryVector, 'vec')^2)"

```

or

```auto
"source": "1.0/(1.0+l2norm(params.queryVector, 'vec')^2)"

```

all result in

```auto
"caused_by" : {
      "type" : "script_exception",
      "reason" : "compile error",
      "script_stack" : [
        "double norm=l2norm(params.queryVector ...",
        " ^---- HERE"
      ],
      "script" : "double norm=l2norm(params.queryVector, 'vec')^2; return 1/(1+norm);",
      "lang" : "painless",
      "position" : {
        "offset" : 12,
        "start" : 0,
        "end" : 37
      },
      "caused_by" : {
        "type" : "class_cast_exception",
        "reason" : "Cannot cast from [double] to [int]."
      }
    }

```

or

```auto
"caused_by" : {
      "type" : "script_exception",
      "reason" : "compile error",
      "script_stack" : [
        "1.0/(1.0+l2norm(params.queryVe ...",
        " ^---- HERE"
      ],
      "script" : "1.0/(1.0+l2norm(params.queryVector, 'vec')^2)",
      "lang" : "painless",
      "position" : {
        "offset" : 5,
        "start" : 0,
        "end" : 30
      },
      "caused_by" : {
        "type" : "class_cast_exception",
        "reason" : "Cannot cast from [double] to [int]."
      }
    }

```

How to fix `Cannot cast from [double] to [int].`? The following does not have such error by removing `^2`, which is required by script formula:

```auto
"source": "1.0/(1.0+l2norm(params.queryVector, 'vec'))"

```

---

<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: [December 21, 2023, 1:39am UTC](https://discuss.elastic.co/t/l2norm-script-in-source-compiles-error-because-cannot-cast-from-double-to-int/347799/2 "2023-12-21T01:39:55Z")

</div>

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