# Replace all method is not working

**URL:** https://discuss.elastic.co/t/replace-all-method-is-not-working/188001
**Category:** Elasticsearch
**Created:** [June 28, 2019, 9:22am UTC](https://discuss.elastic.co/t/replace-all-method-is-not-working/188001 "2019-06-28T09:22:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![saranjeet](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@saranjeet](https://discuss.elastic.co/u/saranjeet)
#### Post date: [June 28, 2019, 9:22am UTC](https://discuss.elastic.co/t/replace-all-method-is-not-working/188001/1 "2019-06-28T09:22:12Z")

</div>

"sort": [  
{  
"\_script": {  
"script": {  
"source": "def varRating = 0; if(doc['symbol'].value == 'fac') varRating=1;else if(doc['name'].value == 'fac') varRating=2;else if(doc['name'].value.indexOf('fac') == 0) varRating=20;else if((doc['name'].value.replaceAll(' ', '').indexOf('fac') == 0) )varRating=20; varRating;",  
"lang": "painless"  
},  
"type": "number",  
"order": "asc"  
}  
},  
{  
"avdolv": {  
"order": "desc"  
}  
},  
{  
"symbol": {  
"order": "asc"  
}  
},  
{  
"name": {  
"order": "asc"  
}  
}  
]  
throwing error  
class\_cast\_exception: Cannot cast java.lang.String to java.util.function.Function  
can any body tell me how to fix this query

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [July 1, 2019, 9:53am UTC](https://discuss.elastic.co/t/replace-all-method-is-not-working/188001/2 "2019-07-01T09:53:18Z")

</div>

Hey,

please format your messages properly. You can use markdown in this forum and you should to make your snippets way more readable.

Please take a look at the [painless documentation](https://www.elastic.co/guide/en/elasticsearch/painless/7.2/painless-walkthrough.html) as there is a short mention about the `replaceAll` method and why it takes function as the second argument.

--Alex

---

<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: [July 29, 2019, 9:53am UTC](https://discuss.elastic.co/t/replace-all-method-is-not-working/188001/3 "2019-07-29T09:53:31Z")

</div>

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