Add leading zeroes in sort script

Hello, im use elastic 7.10.1 and i need help

I wanna sort my numbers, but i can't use number type, so i decide sort it with leading zeroes

{
  "sort": {
    "_script": {
      "type": "string",
      "order": "asc",
      "script": {
        "lang": "painless",
        "source": "if(doc.containsKey('pid') && params._source.pid instanceof int)\n{ return '000' + doc['pid'].value.toString()}"
      }
    }
  }
}

i want add N leading zeroes, for example:
number = 7, expected: 007
number = 21, expected 021
number = 300, expected 300

decision is
'0' * doc['pid'].size()
but in Java (or/and painless) i can't multiply str with int, any idea?

okay, i solved it:

if(doc.containsKey('pid') && params._source.pid instanceof int) {
  return String.format('%010d', new def[] {doc['pid'].value})
}

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