Elasticsearch reindex painless compile error

I have to reindex dynamically like the code below.

curl -XPOST 'http://localhost:9200/_reindex' -H 'Content-Type: application/json'
-d '
{
"source": {
"index": "apache-*",
"query": {
"bool": {
"must": [
{
"range": {
"new_time": {
"gte": "now-30d"
}
}
},
{
"range": {
"new_time": {
"lt": "now"
}
}
},
{
"range": {
"duration": {
"gte": "10000"
}
}
}
]
}
}
},
"dest": {
"index": "duration"
},
"script": {
"lang": "painless",
"inline": "ctx._index = 'apache-' + (ctx._index.substring('apache-'.length(), ctx._index.length())) + '-1'"
}
}'

I refer to this code on your web site(https://www.elastic.co/guide/en/elasticsearch/reference/5.4/docs-reindex.html)

POST _reindex
{
"source": {
"index": "metricbeat-*"
},
"dest": {
"index": "metricbeat"
},
"script": {
"lang": "painless",
"inline": "ctx._index = 'metricbeat-' + (ctx._index.substring('metricbeat-'.length(), ctx._index.length())) + '-1'"
}
}

but It keeps giving me this kind of error.
{"error":{"root_cause":[{"type":"script_exception","reason":"compile error","script_stack":["... ._index.substring(apache-.length(), ctx._index.len ..."," ^---- HERE"],"script":"ctx._index = apache- + (ctx._index.substring(apache-.length(), ctx._index.length())) + -1","lang":"painless"}],"type":"script_exception","reason":"compile error","script_stack":["... ._index.substring(apache-.length(), ctx._index.len ..."," ^---- HERE"],"script":"ctx._index = apache- + (ctx._index.substring(apache-.length(), ctx._index.length())) + -1","lang":"painless","caused_by":{"type":"illegal_argument_exception","reason":"invalid sequence of tokens near ['.'].","caused_by":{"type":"no_viable_alt_exception","reason":null}}

I don't know what is wrong with my code. I hope you could help me. Thank you in advance.

Unfortunately, syntax errors in Painless can be difficult to spot due the error messaging which is a noted item that needs improvement. However, if you look closely it looks like the single quotes are being removed from the Painless script which explains the error. This is likely due to the JSON being encapsulated in single quotes for the -d argument of the curl command. If you escape your single quotes using backslash I believe this will fix this issue.

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