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

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/bd1e6451-b1d3-4b30-be70-ce141409f9d0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

I found my issue by myself :slight_smile:
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/e4aa450b-b853-46f4-8470-77fac6df4b5d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.