# Script not supporting nested if-else or switch case

**URL:** <https://discuss.elastic.co/t/script-not-supporting-nested-if-else-or-switch-case/5715>\
**Category:** Elasticsearch\
**Created:** [October 28, 2011, 10:51am UTC](https://discuss.elastic.co/t/script-not-supporting-nested-if-else-or-switch-case/5715 "2011-10-28T10:51:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![narinder\_izap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narinder_izap/32/803_2.png) [@narinder\_izap](https://discuss.elastic.co/u/narinder_izap)\
**Post date:** [October 28, 2011, 10:51am UTC](https://discuss.elastic.co/t/script-not-supporting-nested-if-else-or-switch-case/5715/1 "2011-10-28T10:51:15Z")

</div>

Hi there,  
I am trying to run the following query, its responding well  
if I am using simple if-else in script, but my requirement is different for  
different fields which requires nested or ladder if-else or switch case,  
None of these if working with script tag. Can you please help me out, what  
is the issue with the query?

{  
"query": {  
"custom\_score": {  
"query": {  
"filtered": {  
"query": {  
"bool": {  
"should": [  
{  
"text": {  
"searchable.title": {  
"type": "phrase\_prefix",  
"query": "k",  
"analyzer": null  
}  
}  
},  
{  
"text": {  
"searchable.code": {  
"type": "phrase\_prefix",  
"query": "k",  
"analyzer": null  
}  
}  
},  
{  
"text": {  
"searchable.zip": {  
"type": "phrase\_prefix",  
"query": "k"  
}  
}  
}  
]  
}  
},  
"filter": {  
"and": [  
{  
"term": {  
"follower\_of\_inverse": "34"  
}  
},  
{  
"or": [  
{  
"prefix": {  
"searchable.title": "k"  
}  
},  
{  
"prefix": {  
"searchable.code": "k"  
}  
},  
{  
"prefix": {  
"searchable.zip": "k"  
}  
}  
]  
}  
]  
}  
}  
},  
"script": {  
"script": "if(doc['subtype'].value=='abc') score_pow(10,3);  
elseif(doc['subtype'].value=='def')\_score_pow(10,0); else \_score"  
}  
}  
},  
"size": 7  
}

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [October 28, 2011, 11:02am UTC](https://discuss.elastic.co/t/script-not-supporting-nested-if-else-or-switch-case/5715/2 "2011-10-28T11:02:40Z")

</div>

On Fri, 2011-10-28 at 03:51 -0700, Narinder Kaur wrote:

> Hi there,  
> I am trying to run the following query, its responding  
> well if I am using simple if-else in script, but my requirement is  
> different for different fields which requires nested or ladder if-else  
> or switch case, None of these if working with script tag. Can you  
> please help me out, what is the issue with the query?

Have a look at the custom filters score query - will probably do what  
you want more efficiently:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

clint

> {  
> "query": {  
> "custom\_score": {  
> "query": {  
> "filtered": {  
> "query": {  
> "bool": {  
> "should": [  
> {  
> "text": {  
> "searchable.title": {  
> "type": "phrase\_prefix",  
> "query": "k",  
> "analyzer": null  
> }  
> }  
> },  
> {  
> "text": {  
> "searchable.code": {  
> "type": "phrase\_prefix",  
> "query": "k",  
> "analyzer": null  
> }  
> }  
> },  
> {  
> "text": {  
> "searchable.zip": {  
> "type": "phrase\_prefix",  
> "query": "k"  
> }  
> }  
> }  
> ]  
> }  
> },  
> "filter": {  
> "and": [  
> {  
> "term": {  
> "follower\_of\_inverse": "34"  
> }  
> },  
> {  
> "or": [  
> {  
> "prefix": {  
> "searchable.title": "k"  
> }  
> },  
> {  
> "prefix": {  
> "searchable.code": "k"  
> }  
> },  
> {  
> "prefix": {  
> "searchable.zip": "k"  
> }  
> }  
> ]  
> }  
> ]  
> }  
> }  
> },  
> "script": {  
> "script": "if(doc['subtype'].value=='abc') score_pow(10,3);  
> elseif(doc['subtype'].value=='def')\_score_pow(10,0); else \_score"  
> }  
> }  
> },  
> "size": 7  
> }

---

<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 6, 2017, 3:50am UTC](https://discuss.elastic.co/t/script-not-supporting-nested-if-else-or-switch-case/5715/3 "2017-07-06T03:50:32Z")

</div>


