# Elasticsearch reindex painless compile error

**URL:** <https://discuss.elastic.co/t/elasticsearch-reindex-painless-compile-error/148772>\
**Category:** Elasticsearch\
**Created:** [September 17, 2018, 7:13am UTC](https://discuss.elastic.co/t/elasticsearch-reindex-painless-compile-error/148772 "2018-09-17T07:13:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nayun\_oh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nayun_oh/32/46442_2.png) [@nayun\_oh](https://discuss.elastic.co/u/nayun_oh)\
**Post date:** [September 17, 2018, 7:13am UTC](https://discuss.elastic.co/t/elasticsearch-reindex-painless-compile-error/148772/1 "2018-09-17T07:13:51Z")

</div>

**I have to reindex dynamically like the code below.**

curl -XPOST '[http://localhost:9200/\_reindex](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](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.**

---

<div class="post-metadata">

**Author:** ![Jack\_Conradson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_conradson/32/47236_2.png) [@Jack\_Conradson](https://discuss.elastic.co/u/Jack_Conradson)\
**Post date:** [September 19, 2018, 7:37pm UTC](https://discuss.elastic.co/t/elasticsearch-reindex-painless-compile-error/148772/2 "2018-09-19T19:37:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 17, 2018, 7:37pm UTC](https://discuss.elastic.co/t/elasticsearch-reindex-painless-compile-error/148772/3 "2018-10-17T19:37:15Z")

</div>

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