# Script having issue with long data type

**URL:** <https://discuss.elastic.co/t/script-having-issue-with-long-data-type/68795>\
**Category:** Elasticsearch\
**Created:** [December 13, 2016, 12:07am UTC](https://discuss.elastic.co/t/script-having-issue-with-long-data-type/68795 "2016-12-13T00:07:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sree2k](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@Sree2k](https://discuss.elastic.co/u/Sree2k)\
**Post date:** [December 13, 2016, 12:07am UTC](https://discuss.elastic.co/t/script-having-issue-with-long-data-type/68795/1 "2016-12-13T00:07:21Z")

</div>

Hello,

I'm kind of new to elasticsearch. I had a question regarding using transport client ( v5.0.1 and my elasticsearch cluster is also at 5.0.1). I'm seeing this error when I try to call update operation.

My document is of following format:

{  
x: [\_array\_of\_longs]  
}

## I tried to use script to have an append only if it exists in the array kind of operation. I don't know if elasticsearch supports Set data structure. Whenever i try with small integer like 100, 200 it works. But when I enter longs, l get this error. Any idea, what I'm doing wrong here ?

Caused by: java.lang.IllegalArgumentException: Invalid int constant [9000000000001].  
at org.elasticsearch.painless.Executable$Script.compile(if(ctx.\_source.x.contains(9000000000001)) { ctx.op = 'none'} else { ctx.\_source.x.add(9000000000001)} @ :33) ~[na:na]  
at org.elasticsearch.painless.node.ENumeric.analyze(ENumeric.java:107) ~[na:na]  
at org.elasticsearch.painless.node.PSubDefCall.analyze(PSubDefCall.java:67) ~[na:na]  
at org.elasticsearch.painless.node.PCallInvoke.analyze(PCallInvoke.java:92) ~[na:na]  
at org.elasticsearch.painless.node.SIfElse.analyze(SIfElse.java:66) ~[na:na]  
at org.elasticsearch.painless.node.SSource.analyze(SSource.java:190) ~[na:na]  
at org.elasticsearch.painless.node.SSource.analyze(SSource.java:162) ~[na:na]  
at org.elasticsearch.painless.Compiler.compile(Compiler.java:104) ~[na:na]  
at org.elasticsearch.painless.PainlessScriptEngineService$2.run(PainlessScriptEngineService.java:171) ~[na:na]  
at org.elasticsearch.painless.PainlessScriptEngineService$2.run(PainlessScriptEngineService.java:168) ~[na:na]  
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0\_31]  
at org.elasticsearch.painless.PainlessScriptEngineService.compile(PainlessScriptEngineService.java:168) ~[na:na]

* * *

I'm using prepareUpdate call from transport client (lang is painless) something like this ...  
client.prepareUpdate(request.getIndex(),  
request.getType(), request.getId())  
.setScript(request.getScript()).setUpsert(request.getDocument()).execute().get();

---

<div class="post-metadata">

**Author:** ![Sree2k](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@Sree2k](https://discuss.elastic.co/u/Sree2k)\
**Post date:** [December 13, 2016, 8:23am UTC](https://discuss.elastic.co/t/script-having-issue-with-long-data-type/68795/2 "2016-12-13T08:23:35Z")

</div>

Have anyone seen this issue before ?

Mapping query shows that type has long defined.

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [December 13, 2016, 10:39am UTC](https://discuss.elastic.co/t/script-having-issue-with-long-data-type/68795/3 "2016-12-13T10:39:48Z")

</div>

I'm using version 2.4.2 and I have same kind of updates. I have to use a script forcing long : here is my script (lang is groovy)

```
    /*
     * Elasticsearch Groovy script (Ref https://www.elastic.co/guide/en/elasticsearch/reference/2.1/modules-scripting.html and http://groovy-lang.org/syntax.html)
     *
     * Use this script when you need to add or delete a single value (int or long) in a list field.
     *
     * Mandatory params:
     * - "paramFieldname": name of the list field to modify
     * - "paramValue": value to add or delete from the field "paramFieldname"
     * - "paramAction", add ? "add" : "del"
     */
    long longparamValue = paramValue as long
    long[] longarray = ctx._source[paramFieldname] as long[]
    
    if(paramAction == "add") {
      if(longarray.contains(longparamValue) == false) {
        ctx._source[paramFieldname].add(longparamValue);
      }
    } else if(paramAction == "del") {
      if(longarray.contains(longparamValue) == true) {
        ctx._source[paramFieldname].removeAll(longparamValue)
      }
    }
```

---

<div class="post-metadata">

**Author:** ![Sree2k](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@Sree2k](https://discuss.elastic.co/u/Sree2k)\
**Post date:** [December 13, 2016, 7:00pm UTC](https://discuss.elastic.co/t/script-having-issue-with-long-data-type/68795/4 "2016-12-13T19:00:04Z")

</div>

Awesome, it works now. thanks!

Must be a problem with transport client and painless script.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [December 13, 2016, 7:24pm UTC](https://discuss.elastic.co/t/script-having-issue-with-long-data-type/68795/5 "2016-12-13T19:24:49Z")

</div>

> [@Sree2k](#):
>
> I'm seeing this error when I try to call update operation.

In Painless you can make a long constant by ending it in `L`. So `9000000000001L`.

You are probably better off using `params` for the script so you don't compile a new script every time you have a new request. That'd look something like

```auto
"script": {
  "inline": "if (ctx.source.x.contains(params.to_add) {ctx.op='none'} else {ctx.source.x.add(params.to_add)}",
  "params": { "to_add": 9000000000001}
}

```

---

<div class="post-metadata">

**Author:** ![Sree2k](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@Sree2k](https://discuss.elastic.co/u/Sree2k)\
**Post date:** [December 13, 2016, 8:16pm UTC](https://discuss.elastic.co/t/script-having-issue-with-long-data-type/68795/6 "2016-12-13T20:16:42Z")

</div>

cool, it works with appending an L. thanks!

---

<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:** [January 10, 2017, 8:17pm UTC](https://discuss.elastic.co/t/script-having-issue-with-long-data-type/68795/7 "2017-01-10T20:17:21Z")

</div>

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