# Check a field contains string using script

**URL:** <https://discuss.elastic.co/t/check-a-field-contains-string-using-script/314424>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [September 14, 2022, 2:34pm UTC](https://discuss.elastic.co/t/check-a-field-contains-string-using-script/314424 "2022-09-14T14:34:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahulkothanath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahulkothanath/32/99122_2.png) [@rahulkothanath](https://discuss.elastic.co/u/rahulkothanath)\
**Post date:** [September 14, 2022, 2:34pm UTC](https://discuss.elastic.co/t/check-a-field-contains-string-using-script/314424/1 "2022-09-14T14:34:11Z")

</div>

I would like to run a query to check if a field is substring of a query string parameter. if yes , I want to return 1.  
I am new to scripting and below code is not working.

> Blockquote  
> GET testspan/\_search  
> {  
> "size": 0,  
> "query": {  
> "match\_all": {}  
> },  
> "aggs": {  
> "CONTAINS": {  
> "filter": {  
> "bool": {  
> "must": [  
> {  
> "term": {  
> "A": "10"  
> }  
> }  
> ]  
> }  
> },  
> "aggs": {  
> "script\_fields": {  
> "example": {  
> "script": {  
> "lang": "painless",  
> "source": """  
> if("CANADA POST VISA".contains(doc['SUSP\_VAL'].value)){  
> return "1"  
> }  
> """  
> }  
> }  
> }  
> }  
> }  
> }  
> }

thanks for the help

---

<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 12, 2022, 2:34pm UTC](https://discuss.elastic.co/t/check-a-field-contains-string-using-script/314424/2 "2022-10-12T14:34:46Z")

</div>

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