# Custom scoring script -- check if field exists?

**URL:** https://discuss.elastic.co/t/custom-scoring-script-check-if-field-exists/9036
**Category:** Elasticsearch
**Created:** [September 16, 2012, 5:12am UTC](https://discuss.elastic.co/t/custom-scoring-script-check-if-field-exists/9036 "2012-09-16T05:12:43Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Sushant\_Shankar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sushant_shankar/32/2652_2.png) [@Sushant\_Shankar](https://discuss.elastic.co/u/Sushant_Shankar)
#### Post date: [September 16, 2012, 5:12am UTC](https://discuss.elastic.co/t/custom-scoring-script-check-if-field-exists/9036/1 "2012-09-16T05:12:43Z")

</div>

I'm new to using scripting and custom scoring scripts -- any help or tips  
would be appreciated!

I have a field 'categories' like this:

categories: {  
Computers/Internet/File\_Sharing : 3  
Computers/Software/Business : 5  
...  
}

I am constructing a scoring function which is the sum of the desired fields  
through a script.

_Check for Sum of fields \>= threshold_

{  
"min\_score": 5,  
"query": {  
"custom\_score": {  
"query": {  
"match\_all": {}  
},  
"script": "\_score \* (  
doc['categories.Computers/Internet/File\_Sharing'].intValue +  
doc['categories.Computers/Software/Business'].intValue )"  
}  
}  
}

For the above document, the score would be 8 for instance. This works for  
some set of documents, but when I include more documents I get an Exception  
saying the field is not recognized (gist: [https://gist.github.com/3731084](https://gist.github.com/3731084)).  
Is there a way in MVEL to check if the fields  
'categories.Computers/Internet/File\_Sharing' and  
'categories.Computers/Software/Business' exist and only then modify the  
score? Here is my incorrect attempt:

_Incorrect attempt at checking existence of fields_

{  
"min\_score": 15,  
"query": {  
"custom\_score": {  
"query": {  
"match\_all": {}  
},  
"script": "if(doc['categories.Computers/Internet/File\_Sharing'].empty  
== 'false') {return \_score \* (  
doc['categories.Computers/Internet/File\_Sharing'].intValue +  
doc['categories.Computers/Software/Business'].intValue );} else {return  
\_score;}"  
}  
}  
}

Thanks!  
Sushant

--

---

<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:12am UTC](https://discuss.elastic.co/t/custom-scoring-script-check-if-field-exists/9036/2 "2017-07-06T03:12:39Z")

</div>


