# MVEL scripting to return 0 or 1 for a boolean

**URL:** <https://discuss.elastic.co/t/mvel-scripting-to-return-0-or-1-for-a-boolean/17097>\
**Category:** Elasticsearch\
**Created:** [April 19, 2014, 6:57pm UTC](https://discuss.elastic.co/t/mvel-scripting-to-return-0-or-1-for-a-boolean/17097 "2014-04-19T18:57:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chimpsarehungry](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chimpsarehungry](https://discuss.elastic.co/u/chimpsarehungry)\
**Post date:** [April 19, 2014, 6:57pm UTC](https://discuss.elastic.co/t/mvel-scripting-to-return-0-or-1-for-a-boolean/17097/1 "2014-04-19T18:57:56Z")

</div>

Is there a function, or a way to include a boolean in my script like this

(doc['field1'].value == doc['field2'].value) and that in the parentheses  
returns a numeric value 0 or 1 ?

I want to add this to functions and multiply by score in my function here.  
This seems easier than making a filter based on that conditional.

"functions": [  
{  
"script\_score": {  
"script": "log(\_score \* (doc['field1'].value ==  
doc['field2'].value)"  
}  
}  
]

--  
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/22c88d38-b1cd-4228-96bb-58838463c2bb%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/22c88d38-b1cd-4228-96bb-58838463c2bb%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [April 20, 2014, 11:52am UTC](https://discuss.elastic.co/t/mvel-scripting-to-return-0-or-1-for-a-boolean/17097/2 "2014-04-20T11:52:06Z")

</div>

On 19 April 2014 20:57, Shane Neeley [sneeley@molecularmatch.com](mailto:sneeley@molecularmatch.com) wrote:

> "script": "log(\_score \* (doc['field1'].value == doc['field2'].value)"

"script": "log(\_score \* ((doc['field1'].value == doc['field2'].value) ? :  
1 : 0)"

--  
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/CAPt3XKQMsgaOB4gQOddeUt%2BX%2BVBFEcaOX00397mc3v62-nfsxA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPt3XKQMsgaOB4gQOddeUt%2BX%2BVBFEcaOX00397mc3v62-nfsxA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![chimpsarehungry](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chimpsarehungry](https://discuss.elastic.co/u/chimpsarehungry)\
**Post date:** [April 21, 2014, 1:28pm UTC](https://discuss.elastic.co/t/mvel-scripting-to-return-0-or-1-for-a-boolean/17097/3 "2014-04-21T13:28:53Z")

</div>

> Thanks Clinton. Can you help me through this error?

Here is a couple of documents that return on a regular script score.  
"hits": {  
"total": 246,  
"max\_score": 1.293869,  
"hits": [  
{  
"\_index": "intent",  
"\_type": "intent",  
"\_score": 1.293869,  
"\_source": {  
"alias": "RAS",  
"rootTerm": "RAS"  
}  
},  
{  
"\_index": "intent",  
"\_type": "intent",  
"\_score": 0.9466758,  
"\_source": {  
"rootTerm": "3-Methyl-Aspartic Acid",  
"alias": "3MA"  
}  
...]

Then when I add script score, only the first hit should still have a score:  
"script\_score": {  
"script": "log(\_score \* ((doc['alias'].value ==  
doc['rootTerm'].value) ? : 1 : 0))"  
}

But I get this error..

{  
"error": "SearchPhaseExecutionException[Failed to execute phase [query],  
all shards failed; shardFailures {[Cq92Pqk5QYayO\_4af76HjA][intent][0]:  
QueryPhaseExecutionException[[intent][0]: query[function score (term:ras~2,  
functions: [{filter(_:_), function [script[log(\_score \*  
((doc['alias'].value == doc['rootTerm'].value) ? : 1 : 0))], params  
[null]]}])],from[0],size[10]: Query Failed [Failed to execute main query]];  
nested: CompileException[[Error: illegal use of operator:  
TERNARY\_ELSE]\n[Near : {... ((doc['alias'].value == doc['rootTerm'].value)  
? : 1 : 0)) ....}]\n  
^\n[Line: 1, Column: 63]]; }{[Cq92Pqk5QYayO\_4af76HjA][intent][1]:  
QueryPhaseExecutionException[[intent][1]: query[function score (term:ras~2,  
functions: [{filter(_:_), function [script[log(\_score \*  
((doc['alias'].value == doc['rootTerm'].value) ? : 1 : 0))], params  
[null]]}])],from[0],size[10]: Query Failed [Failed to execute main query]];  
nested: CompileException[[Error: illegal use of operator:  
TERNARY\_ELSE]\n[Near : {... ((doc['alias'].value == doc['rootTerm'].value)  
? : 1 : 0)) ....}]\n  
^\n[Line: 1, Column: 63]]; }{[Cq92Pqk5QYayO\_4af76HjA][intent][2]:  
QueryPhaseExecutionException[[intent][2]: query[function score (term:ras~2,  
functions: [{filter(_:_), function [script[log(\_score \*  
((doc['alias'].value == doc['rootTerm'].value) ? : 1 : 0))], params  
[null]]}])],from[0],size[10]: Query Failed [Failed to execute main query]];  
nested: CompileException[[Error: illegal use of operator:  
TERNARY\_ELSE]\n[Near : {... ((doc['alias'].value == doc['rootTerm'].value)  
? : 1 : 0)) ....}]\n  
^\n[Line: 1, Column: 63]]; }{[Cq92Pqk5QYayO\_4af76HjA][intent][3]:  
QueryPhaseExecutionException[[intent][3]: query[function score (term:ras~2,  
functions: [{filter(_:_), function [script[log(\_score \*  
((doc['alias'].value == doc['rootTerm'].value) ? : 1 : 0))], params  
[null]]}])],from[0],size[10]: Query Failed [Failed to execute main query]];  
nested: CompileException[[Error: illegal use of operator:  
TERNARY\_ELSE]\n[Near : {... ((doc['alias'].value == doc['rootTerm'].value)  
? : 1 : 0)) ....}]\n  
^\n[Line: 1, Column: 63]]; }{[Cq92Pqk5QYayO\_4af76HjA][intent][4]:  
QueryPhaseExecutionException[[intent][4]: query[function score (term:ras~2,  
functions: [{filter(_:_), function [script[log(\_score \*  
((doc['alias'].value == doc['rootTerm'].value) ? : 1 : 0))], params  
[null]]}])],from[0],size[10]: Query Failed [Failed to execute main query]];  
nested: CompileException[[Error: illegal use of operator:  
TERNARY\_ELSE]\n[Near : {... ((doc['alias'].value == doc['rootTerm'].value)  
? : 1 : 0)) ....}]\n  
^\n[Line: 1, Column: 63]]; }]",  
"status": 500  
}

--  
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/06ace2ef-0759-478a-99ab-a5232493efba%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/06ace2ef-0759-478a-99ab-a5232493efba%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [April 21, 2014, 9:52pm UTC](https://discuss.elastic.co/t/mvel-scripting-to-return-0-or-1-for-a-boolean/17097/4 "2014-04-21T21:52:55Z")

</div>

Probably just remove the colon after the question mark (typo?):

((doc['alias'].value == doc['rootTerm'].value) ? 1 : 0))

--  
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/ecc3abb6-8663-4c10-8831-713ea38b4263%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ecc3abb6-8663-4c10-8831-713ea38b4263%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [April 21, 2014, 9:53pm UTC](https://discuss.elastic.co/t/mvel-scripting-to-return-0-or-1-for-a-boolean/17097/5 "2014-04-21T21:53:43Z")

</div>

Probably just remove the colon after the question mark (typo?):

((doc['alias'].value == doc['rootTerm'].value) ? 1 : 0)

--  
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/bc45f154-a0b3-4847-83e2-3acb404a73d2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bc45f154-a0b3-4847-83e2-3acb404a73d2%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![chimpsarehungry](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chimpsarehungry](https://discuss.elastic.co/u/chimpsarehungry)\
**Post date:** [April 22, 2014, 2:32pm UTC](https://discuss.elastic.co/t/mvel-scripting-to-return-0-or-1-for-a-boolean/17097/6 "2014-04-22T14:32:02Z")

</div>

Thanks Binh you were right

On Monday, April 21, 2014 4:53:43 PM UTC-5, Binh Ly wrote:

> Probably just remove the colon after the question mark (typo?):
> 
> ((doc['alias'].value == doc['rootTerm'].value) ? 1 : 0)

--  
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/8b269bb4-c54f-4cbf-8c91-3d32bbe3a483%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8b269bb4-c54f-4cbf-8c91-3d32bbe3a483%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:34am UTC](https://discuss.elastic.co/t/mvel-scripting-to-return-0-or-1-for-a-boolean/17097/7 "2017-07-06T01:34:19Z")

</div>


