# Why can i do an "if" in my script?

**URL:** <https://discuss.elastic.co/t/why-can-i-do-an-if-in-my-script/18605>\
**Category:** Elasticsearch\
**Created:** [July 11, 2014, 3:18pm UTC](https://discuss.elastic.co/t/why-can-i-do-an-if-in-my-script/18605 "2014-07-11T15:18:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joffrey\_Hercule](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@Joffrey\_Hercule](https://discuss.elastic.co/u/Joffrey_Hercule)\
**Post date:** [July 11, 2014, 3:18pm UTC](https://discuss.elastic.co/t/why-can-i-do-an-if-in-my-script/18605/1 "2014-07-11T15:18:12Z")

</div>

Hi all,

i'm trying to test if a value is in an array but i don't know why my if  
clause doesn't work...

doc['users'] is an array of long values in my example.  
So If i do :

{  
"query" : {  
"match\_all" : {}  
},  
"sort" : {  
"\_script" : {  
"script" : "doc['users'].values.contains((long)1)",  
"type" : "string",  
"order" : "desc"  
}  
}  
}

I retrieve "true" or "false" in my result.

But if i try :

{  
"query" : {  
"match\_all" : {}  
},  
"sort" : {  
"\_script" : {  
"script" : "if(doc['users'].values.contains((long)1)){return  
'foo'}",  
"type" : "string",  
"order" : "desc"  
}  
}  
}

I got an exception "SearchPhaseExecutionException":  
org.elasticsearch.index.fielddata.fieldcomparator.StringScriptDataComparator$InnerSource  
I tried with if(doc['users'].values.contains((long)1) == true) too but same  
issue...

Someone knows why ?  
Thanks in advance !

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/bd1e6451-b1d3-4b30-be70-ce141409f9d0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bd1e6451-b1d3-4b30-be70-ce141409f9d0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Joffrey\_Hercule](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@Joffrey\_Hercule](https://discuss.elastic.co/u/Joffrey_Hercule)\
**Post date:** [July 11, 2014, 3:30pm UTC](https://discuss.elastic.co/t/why-can-i-do-an-if-in-my-script/18605/2 "2014-07-11T15:30:56Z")

</div>

I found my issue by myself 🙂  
The "else" is mandatory.

So now it's good !

{  
"query" : {  
"match\_all" : {}  
},  
"sort" : {  
"\_script" : {  
"script" : "if(doc['users'].values.contains((long)1)){return  
'foo'}else{return 'bar'}",  
"type" : "string",  
"order" : "desc"  
}  
}  
}

Le vendredi 11 juillet 2014 17:18:13 UTC+2, Joffrey Hercule a écrit :

> Hi all,
> 
> i'm trying to test if a value is in an array but i don't know why my if  
> clause doesn't work...
> 
> doc['users'] is an array of long values in my example.  
> So If i do :
> 
> {  
> "query" : {  
> "match\_all" : {}  
> },  
> "sort" : {  
> "\_script" : {  
> "script" : "doc['users'].values.contains((long)1)",  
> "type" : "string",  
> "order" : "desc"  
> }  
> }  
> }
> 
> I retrieve "true" or "false" in my result.
> 
> But if i try :
> 
> {  
> "query" : {  
> "match\_all" : {}  
> },  
> "sort" : {  
> "\_script" : {  
> "script" : "if(doc['users'].values.contains((long)1)){return  
> 'foo'}",  
> "type" : "string",  
> "order" : "desc"  
> }  
> }  
> }
> 
> I got an exception "SearchPhaseExecutionException":  
> org.elasticsearch.index.fielddata.fieldcomparator.StringScriptDataComparator$InnerSource  
> I tried with if(doc['users'].values.contains((long)1) == true) too but  
> same issue...
> 
> Someone knows why ?  
> Thanks in advance !

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/e4aa450b-b853-46f4-8470-77fac6df4b5d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e4aa450b-b853-46f4-8470-77fac6df4b5d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:16am UTC](https://discuss.elastic.co/t/why-can-i-do-an-if-in-my-script/18605/3 "2017-07-06T01:16:29Z")

</div>


