# Mvel expression support in ES custom scoring

**URL:** <https://discuss.elastic.co/t/mvel-expression-support-in-es-custom-scoring/9460>\
**Category:** Elasticsearch\
**Created:** [October 24, 2012, 5:18pm UTC](https://discuss.elastic.co/t/mvel-expression-support-in-es-custom-scoring/9460 "2012-10-24T17:18:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derry\_O\_Sullivan](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Derry\_O\_Sullivan](https://discuss.elastic.co/u/Derry_O_Sullivan)\
**Post date:** [October 24, 2012, 5:18pm UTC](https://discuss.elastic.co/t/mvel-expression-support-in-es-custom-scoring/9460/1 "2012-10-24T17:18:10Z")

</div>

Hi all,

I'm trying to do a custom score function in MVEL and need to learn how to  
deal with a null value (ideally, the null value would not be there of  
course!) but it's already in the index now:

I want to take my existing score and let's say e.g. divide it by a  
particular document field value(checkVal( if available, 1 if not

From looking at:  
[http://mvel.codehaus.org/MVEL+2.0+Control+Flow](http://mvel.codehaus.org/MVEL+2.0+Control+Flow)

I thought i could just say:  
"script" : "\_score \* (doc['checkVal'].value==nil?1:doc['checkVal'].value)"

The problem is that documents that have a nil/null value seem to be ignored  
and all others use the correct value. (the value gets used in the query  
phase also - that is why i am using doc instead of source)

I've also tried null instead of null and have seen people talking about  
'.?' as a value check also?

Derry

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [October 24, 2012, 5:46pm UTC](https://discuss.elastic.co/t/mvel-expression-support-in-es-custom-scoring/9460/2 "2012-10-24T17:46:45Z")

</div>

If checkVal had the "string" type, doc['checkVal'].value==null?... would  
have worked. For a numeric type, you can do something like:

\_score \* (doc['checkVal'].value==0.0?1:doc['checkVal'].value)

On Wednesday, October 24, 2012 1:18:10 PM UTC-4, Derry O' Sullivan wrote:

> Hi all,
> 
> I'm trying to do a custom score function in MVEL and need to learn how to  
> deal with a null value (ideally, the null value would not be there of  
> course!) but it's already in the index now:
> 
> I want to take my existing score and let's say e.g. divide it by a  
> particular document field value(checkVal( if available, 1 if not
> 
> From looking at:  
> [http://mvel.codehaus.org/MVEL+2.0+Control+Flow](http://mvel.codehaus.org/MVEL+2.0+Control+Flow)
> 
> I thought i could just say:  
> "script" : "\_score \* (doc['checkVal'].value==nil?1:doc['checkVal'].value)"
> 
> The problem is that documents that have a nil/null value seem to be  
> ignored and all others use the correct value. (the value gets used in the  
> query phase also - that is why i am using doc instead of source)
> 
> I've also tried null instead of null and have seen people talking about  
> '.?' as a value check also?
> 
> Derry

--

---

<div class="post-metadata">

**Author:** ![Derry\_O\_Sullivan](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Derry\_O\_Sullivan](https://discuss.elastic.co/u/Derry_O_Sullivan)\
**Post date:** [October 24, 2012, 11:30pm UTC](https://discuss.elastic.co/t/mvel-expression-support-in-es-custom-scoring/9460/3 "2012-10-24T23:30:43Z")

</div>

Thanks for that - one of my fields is an numeric type so i can use the 0.0 check. For a date type, which is where the default null value? Looking at:

> **[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.

[http://joda-time.sourceforge.net/api-release/org/joda/time/MutableDateTime.html](http://joda-time.sourceforge.net/api-release/org/joda/time/MutableDateTime.html)  
and

> **[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.

Can there be a 'default'/null date? (i'm using the standard format, not any custom one...)

On Wednesday, 24 October 2012 18:46:45 UTC+1, Igor Motov wrote:

> If checkVal had the "string" type, doc['checkVal'].value==null?... would have worked. For a numeric type, you can do something like:
> 
> \_score \* (doc['checkVal'].value==0.0?1:doc['checkVal'].value)
> 
> On Wednesday, October 24, 2012 1:18:10 PM UTC-4, Derry O' Sullivan wrote:Hi all,
> 
> I'm trying to do a custom score function in MVEL and need to learn how to deal with a null value (ideally, the null value would not be there of course!) but it's already in the index now:
> 
> I want to take my existing score and let's say e.g. divide it by a particular document field value(checkVal( if available, 1 if not
> 
> From looking at:  
> [http://mvel.codehaus.org/MVEL+2.0+Control+Flow](http://mvel.codehaus.org/MVEL+2.0+Control+Flow)
> 
> I thought i could just say:  
> "script" : "\_score \* (doc['checkVal'].value==nil?1:doc['checkVal'].value)"
> 
> The problem is that documents that have a nil/null value seem to be ignored and all others use the correct value. (the value gets used in the query phase also - that is why i am using doc instead of source)
> 
> I've also tried null instead of null and have seen people talking about '.?' as a value check also?
> 
> Derry

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [October 24, 2012, 11:50pm UTC](https://discuss.elastic.co/t/mvel-expression-support-in-es-custom-scoring/9460/4 "2012-10-24T23:50:54Z")

</div>

The value of a date field has type long (it's equal to UTC milliseconds  
since the epoch). So, you can simply check for 0.

On Wednesday, October 24, 2012 7:30:43 PM UTC-4, Derry O' Sullivan wrote:

> Thanks for that - one of my fields is an numeric type so i can use the 0.0  
> check. For a date type, which is where the default null value? Looking at:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting.html)
> 
> [MutableDateTime (Joda time 2.1 API)](http://joda-time.sourceforge.net/api-release/org/joda/time/MutableDateTime.html)  
> and  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/date-format.html)
> 
> Can there be a 'default'/null date? (i'm using the standard format, not  
> any custom one...)
> 
> On Wednesday, 24 October 2012 18:46:45 UTC+1, Igor Motov wrote:
> 
> > If checkVal had the "string" type, doc['checkVal'].value==null?... would  
> > have worked. For a numeric type, you can do something like:
> > 
> > \_score \* (doc['checkVal'].value==0.0?1:doc['checkVal'].value)
> > 
> > On Wednesday, October 24, 2012 1:18:10 PM UTC-4, Derry O' Sullivan  
> > wrote:Hi all,
> > 
> > I'm trying to do a custom score function in MVEL and need to learn how  
> > to deal with a null value (ideally, the null value would not be there of  
> > course!) but it's already in the index now:
> > 
> > I want to take my existing score and let's say e.g. divide it by a  
> > particular document field value(checkVal( if available, 1 if not
> > 
> > From looking at:  
> > [http://mvel.codehaus.org/MVEL+2.0+Control+Flow](http://mvel.codehaus.org/MVEL+2.0+Control+Flow)
> > 
> > I thought i could just say:  
> > "script" : "\_score
> 
> - (doc['checkVal'].value==nil?1:doc['checkVal'].value)"
> 
> > The problem is that documents that have a nil/null value seem to be  
> > ignored and all others use the correct value. (the value gets used in the  
> > query phase also - that is why i am using doc instead of source)
> > 
> > I've also tried null instead of null and have seen people talking about  
> > '.?' as a value check also?
> > 
> > Derry

--

---

<div class="post-metadata">

**Author:** ![Derry\_O\_Sullivan](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Derry\_O\_Sullivan](https://discuss.elastic.co/u/Derry_O_Sullivan)\
**Post date:** [October 25, 2012, 9:01am UTC](https://discuss.elastic.co/t/mvel-expression-support-in-es-custom-scoring/9460/5 "2012-10-25T09:01:27Z")

</div>

Many thanks Igor, that worked perfectly.

It also helped when i remembered to add that doc field in the mapping to  
one of the indexes 😉

On Thursday, 25 October 2012 00:50:54 UTC+1, Igor Motov wrote:

> The value of a date field has type long (it's equal to UTC milliseconds  
> since the epoch). So, you can simply check for 0.
> 
> On Wednesday, October 24, 2012 7:30:43 PM UTC-4, Derry O' Sullivan wrote:
> 
> > Thanks for that - one of my fields is an numeric type so i can use the  
> > 0.0 check. For a date type, which is where the default null value? Looking  
> > at:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting.html)
> > 
> > [MutableDateTime (Joda time 2.1 API)](http://joda-time.sourceforge.net/api-release/org/joda/time/MutableDateTime.html)  
> > and  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/date-format.html)
> > 
> > Can there be a 'default'/null date? (i'm using the standard format, not  
> > any custom one...)
> > 
> > On Wednesday, 24 October 2012 18:46:45 UTC+1, Igor Motov wrote:
> > 
> > > If checkVal had the "string" type, doc['checkVal'].value==null?...  
> > > would have worked. For a numeric type, you can do something like:
> > > 
> > > \_score \* (doc['checkVal'].value==0.0?1:doc['checkVal'].value)
> > > 
> > > On Wednesday, October 24, 2012 1:18:10 PM UTC-4, Derry O' Sullivan  
> > > wrote:Hi all,
> > > 
> > > I'm trying to do a custom score function in MVEL and need to learn how  
> > > to deal with a null value (ideally, the null value would not be there of  
> > > course!) but it's already in the index now:
> > > 
> > > I want to take my existing score and let's say e.g. divide it by a  
> > > particular document field value(checkVal( if available, 1 if not
> > > 
> > > From looking at:  
> > > [http://mvel.codehaus.org/MVEL+2.0+Control+Flow](http://mvel.codehaus.org/MVEL+2.0+Control+Flow)
> > > 
> > > I thought i could just say:  
> > > "script" : "\_score
> > 
> > - (doc['checkVal'].value==nil?1:doc['checkVal'].value)"
> > 
> > > The problem is that documents that have a nil/null value seem to be  
> > > ignored and all others use the correct value. (the value gets used in the  
> > > query phase also - that is why i am using doc instead of source)
> > > 
> > > I've also tried null instead of null and have seen people talking about  
> > > '.?' as a value check also?
> > > 
> > > Derry

--

---

<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:07am UTC](https://discuss.elastic.co/t/mvel-expression-support-in-es-custom-scoring/9460/6 "2017-07-06T03:07:17Z")

</div>


