0

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.

1 Answer 1

2

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);"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.