elastic / elasticsearch

Free and Open, Distributed, RESTful Search Engine
https://www.elastic.co/products/elasticsearch
Other
69.41k stars 24.56k forks source link

Support \n and \t in painless scripts #103228

Open EnderShadow opened 9 months ago

EnderShadow commented 9 months ago

Description

Painless only supports escaping backslashes, double quotes, and single quotes. Newlines and tabs should also be supported to allow for more readable painless scripts which use those escapes when transforming and formatting text.

elasticsearchmachine commented 8 months ago

Pinging @elastic/es-core-infra (Team:Core/Infra)

rjernst commented 8 months ago

There's nothing about newlines that requires painless to restrict them. The escapes you mentioned are specifically to allow strings to be encoded within the JSON string a painless script is defined in. But JSON does not allow newline characters within strings. This isn't something painless can really workaround, any actual newline character whether prefixed by a \ or not, will break parsing.

stu-elastic commented 8 months ago

@EnderShadow Can you provide an example for this use-case?

EnderShadow commented 7 months ago

Yes, I have a watcher which checks the status of ECE to send alerts to my team. To make it easier for us to read, I use newlines to separate sections of the message. Currently I have actual newlines in the string.

String message = "";
if(allocator_messages.size() > 0) {
  message = "The following allocators are unhealthy: " + allocator_messages.join('
');
}

if(instance_messages.size() > 0) {
  if(message != "") {
    message == "

";
  }
  message += "The following nodes are unhealthy:
" + instance_messages.join('
');
}

It would be much clearer and nicer to write it as

String message = "";
if(allocator_messages.size() > 0) {
  message = "The following allocators are unhealthy:\n" + allocator_messages.join('\n');
}

if(instance_messages.size() > 0) {
  if(message != "") {
    message == "\n\n";
  }
  message += "The following nodes are unhealthy:\n" + instance_messages.join('\n');
}