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

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"
                     }
                }
            }
        }
    }
}
1 Like

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