Custom scoring script -- check if field exists?


(Sushant Shankar) #1

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).
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

--


(system) #2