mrk
mrk

Reputation: 730

elasticsearch query scripting with variables

I need to update a lot of documents already inserted on elasticsearch 5.4. I think I'm missing some BIG concept here, because I need to do something simply and I can't.

I need to cut the 6 last numbers of "req.body.client.id" and hide the rest (replacing by x). Example: 9494402234028493247 ---> xxxxxxxxxxxxxx493247

I could do it! But the problem is that the code is soo long. I would like to use a VARIABLE and I can't. I don't want to REPEAT the field ctx._source.req.body.client.id 4 times. It would be better if I could do that: clientId = ctx._source.req.body.client.id and then use clientId in the rest of the script line. What is the right way to do that?

POST my_index/_update_by_query
{
    "query": {
        "regexp":{
            "req.body.client.id":"94944022.*"
        }
    },
    "script": {
        "lang": "painless",
        "inline": "ctx._source.req.body.client.id = 'xxxxxxxxxxx' + ctx._source.req.body.client.id.substring(ctx._source.req.body.client.id.length()-6,ctx._source.req.body.client.id.length())"
    }
 }

The question is about VARIABLES and code simplification.

Upvotes: 0

Views: 1501

Answers (1)

mrk
mrk

Reputation: 730

I found the solution. It's a language called PAINLESS. The right way is:

"String clientId = ctx._source.req.body.client.id; 
int cLen = clientId.length(); 
ctx._source.req.body.client.id = "xxxxxxxxxxx" + clientId.substring(cLen-4,cLen);"

Upvotes: 2

Related Questions