Mary
Mary

Reputation: 1135

In Elasticsearch, why isn't my multi-line script working?

I'm using the Elasticsearch JavaScript API.

The following script works in the sense that it runs without error:

const script = {
  "lang": "painless",
  "source": `
  for (int i = 0; i < ctx._source.metadata.length; ++i) {
  }
  `,
};

But when I try and use three double quotes as in the following example:

https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-examples.html#_accessing_doc_values_from_painless

(because my script is going to be a complex multi-line one), I modify my code to:

const script = {
  "lang": "painless",
  "source": `
  """
  for (int i = 0; i < ctx._source.metadata.length; ++i) {
  }
  """
  `,
};

and then I get the following error:

"reason\":\"unexpected token ['\\"\\n for (int i = 0; i < ctx._source.metadata.length; ++i) {\\n }\\n \\"'] was expecting one of [{, ';'}].\"}},\"status\":500}"}

How do I fix this issue?

Upvotes: 0

Views: 2117

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138327

In JavaScript, multiline strings can be done with `. There is no need for the """. The documentation you reference is not for JavaScript.

... because my script is going to be a complex multi-line one

... But your first example also shows a multiline code and that works?

Upvotes: 1

Related Questions