# How to convert binary field's value to string with Painless?

**URL:** https://discuss.elastic.co/t/how-to-convert-binary-fields-value-to-string-with-painless/222262
**Category:** Elasticsearch
**Tags:** painless
**Created:** [March 5, 2020, 10:14am UTC](https://discuss.elastic.co/t/how-to-convert-binary-fields-value-to-string-with-painless/222262 "2020-03-05T10:14:20Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![michalak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michalak/32/63876_2.png) [@michalak](https://discuss.elastic.co/u/michalak)
#### Post date: [March 5, 2020, 10:14am UTC](https://discuss.elastic.co/t/how-to-convert-binary-fields-value-to-string-with-painless/222262/1 "2020-03-05T10:14:20Z")

</div>

I have a binary field, the values are JSON strings, just stored as bytes. I need to look inside and query for specific substring. The values as you query them are base64 strings, so I thought I'd decode them, make string out of it and check if string contains what I'm looking for. I'm stuck on creating new string. Seems like constructors aren't exposed in the API. I've tried `new String(bytes)` and `new BytesRef(bytes).utf8ToString()` - both failed on `new` keyword.

So far I've got this. Obviously won't compile. Is there any other way to achieve what I need?

```
POST /index/_search
{
    "size": 10,
    "query": {
        "bool" : {
            "filter" : {
                "script" : {
                    "script" : {
                        "source": "String i = doc['key'].value; byte[] o = java.util.Base64.getDecoder().decode(i); String str = new String(o); str.contains(\"id12345\")",
                        "lang": "painless"
                     }
                }
            }
        }
    }
}
```

---

<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: [April 2, 2020, 10:14am UTC](https://discuss.elastic.co/t/how-to-convert-binary-fields-value-to-string-with-painless/222262/2 "2020-04-02T10:14:28Z")

</div>

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