# How to access java methods inside of painless

**URL:** https://discuss.elastic.co/t/how-to-access-java-methods-inside-of-painless/182242
**Category:** Elasticsearch
**Tags:** painless
**Created:** [May 22, 2019, 2:06pm UTC](https://discuss.elastic.co/t/how-to-access-java-methods-inside-of-painless/182242 "2019-05-22T14:06:08Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Lockhead](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lockhead/32/46631_2.png) [@Lockhead](https://discuss.elastic.co/u/Lockhead)
#### Post date: [May 22, 2019, 2:06pm UTC](https://discuss.elastic.co/t/how-to-access-java-methods-inside-of-painless/182242/1 "2019-05-22T14:06:09Z")

</div>

I would like to do a popcount over a long field inside of a painless script, but I do not understand the documentation here. There is no single example in how to access native java functions inside of painless.

{  
"script": {  
"lang": "painless",  
"source": "return BitCount(params.p1^doc[params.p2].value);"  
}  
}

This does not work...

---

<div class="post-metadata">

### Author: ![matheusribeirogarcia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matheusribeirogarcia/32/44363_2.png) [@matheusribeirogarcia](https://discuss.elastic.co/u/matheusribeirogarcia)
#### Post date: [May 22, 2019, 4:47pm UTC](https://discuss.elastic.co/t/how-to-access-java-methods-inside-of-painless/182242/2 "2019-05-22T16:47:57Z")

</div>

I think there's no way to call java functions inside scripts, if there's an option to that, there's no documentation talking about it.

Besides that, if it fixes your problem, the JS API allows functions inside scripts, so you could use it, depending on what are your goals.

---

<div class="post-metadata">

### Author: ![Lockhead](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lockhead/32/46631_2.png) [@Lockhead](https://discuss.elastic.co/u/Lockhead)
#### Post date: [May 22, 2019, 5:00pm UTC](https://discuss.elastic.co/t/how-to-access-java-methods-inside-of-painless/182242/3 "2019-05-22T17:00:53Z")

</div>

The stuff I need is there: [https://www.elastic.co/guide/en/elasticsearch/painless/7.x/painless-api-reference-shared.html](https://www.elastic.co/guide/en/elasticsearch/painless/7.x/painless-api-reference-shared.html) I simply have no Idea how to access them, it would be awesome if the documentation could cover that...

---

<div class="post-metadata">

### Author: ![matheusribeirogarcia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matheusribeirogarcia/32/44363_2.png) [@matheusribeirogarcia](https://discuss.elastic.co/u/matheusribeirogarcia)
#### Post date: [May 22, 2019, 5:07pm UTC](https://discuss.elastic.co/t/how-to-access-java-methods-inside-of-painless/182242/4 "2019-05-22T17:07:49Z")

</div>

are you trying to do something like this?

`script(new Script(ScriptType.INLINE,ctx._source.field = "+java method,"painless", Collections.emptyMap()));`

---

<div class="post-metadata">

### Author: ![Lockhead](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lockhead/32/46631_2.png) [@Lockhead](https://discuss.elastic.co/u/Lockhead)
#### Post date: [May 22, 2019, 6:53pm UTC](https://discuss.elastic.co/t/how-to-access-java-methods-inside-of-painless/182242/5 "2019-05-22T18:53:06Z")

</div>

No I want to access the BitCount method of java.lang.Long and it simply does not work...

---

<div class="post-metadata">

### Author: ![martinr\_ubi](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@martinr\_ubi](https://discuss.elastic.co/u/martinr_ubi)
#### Post date: [May 22, 2019, 10:56pm UTC](https://discuss.elastic.co/t/how-to-access-java-methods-inside-of-painless/182242/6 "2019-05-22T22:56:09Z")

</div>

> [@Lockhead](#):
>
> BitCount method of java.lang.Long

```auto
return Long.bitCount(31416);

```

(You also had a typo.)  
This also works although I think I never saw an example doing it like that:

```auto
return java.lang.Long.bitCount(31416);

```

Imagine the package is imported:

```auto
import java.lang.*;

```

Apply this to all available packages in the context in which your are.

There are example in the docs but I'm not saying that because I disagree with your documentation improvement suggestion. I'm not a born developer and I'm intimidated by painless myself and it's kinda required to do alarming with the Watcher feature.

See the example on this page for an example of an example 🙂 :

> **[Ingest processor context | Painless Scripting Language \[7.16\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/painless/7.16/painless-ingest-processor-context.html)**

```auto
[...]
int hours = Integer.parseInt(timeSplit[0].trim());
[...]
ZonedDateTime dt = ZonedDateTime.parse(
         dts, DateTimeFormatter.ISO_OFFSET_DATE_TIME);               
ctx.datetime = dt.getLong(ChronoField.INSTANT_SECONDS)*1000L;

```

From a Kibana dev tool console:

```auto
POST /_scripts/painless/_execute
{
  "script": {
    "source": "return Long.bitCount(params.var1);",
    "params": {
      "var1": 666,
      "var2": 66666
    }
  }
}

```

Response:

```auto
{
  "result": "5"
}

```

Not directly related but sites like this one (first link out of googling "java Long.bitCount example") help me sometimes because as I said not a formally trained developer:

> **[Java.lang.Long.bitCount() Method](https://www.tutorialspoint.com/java/lang/long_bitcount.htm)**
>
> Java lang Long bitCount() Method - The java.lang.Long.bitCount() method returns the number of one-bits in the two's complement binary representation of the specified long value i. This function is sometimes referred to as the population count.

---

<div class="post-metadata">

### Author: ![Lockhead](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lockhead/32/46631_2.png) [@Lockhead](https://discuss.elastic.co/u/Lockhead)
#### Post date: [May 23, 2019, 9:06am UTC](https://discuss.elastic.co/t/how-to-access-java-methods-inside-of-painless/182242/7 "2019-05-23T09:06:27Z")

</div>

Awesome thank you.

---

<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: [June 20, 2019, 9:18am UTC](https://discuss.elastic.co/t/how-to-access-java-methods-inside-of-painless/182242/8 "2019-06-20T09:18:29Z")

</div>

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