Testing multivalue field value existence in painless

I have a multivalue field of type long. Depending on the inclusion of a per-query value in this field, I want to modify the documents score.

I'm trying to achieve this with a function_score and script_score, but I can't figure out how to test if a value is included in the field or not.

Say I have my_field with the values [ 1, 2, 3 ] set. So far I've tried
{ "script_score": { "script": { "inline": { "doc['my_field'].contains( 1 ) ? 1000 : 0" } } } }
{ "script_score": { "script": { "inline": { "doc['my_field'].values.contains( 1 ) ? 1000 : 0" }}}}
{ "script_score": { "script": { "inline": { "( doc['my_field'].value == 1 ) ? 1000 : 0 }}}

and all evaluate to false (at least I get a score of 0 returned.

How do I properly test for the inclusion of a value in a multi value field?

I'm surprised you do not get an error with those examples. The inline key should be taking a string. A json object only works for templates. So I believe, based on how we handle templates, all of your examples will end up creating a string passed to painless that contains curlies (ie a block), and then the only statement inside that block will be a String, which is the last statement, so it will be returned. Looking at the code that should be trying to cast the returned String to a Number and call doubleValue, which should result in a runtime error. Since you don't see that error, something else must be going on (or the error is masked, and 0 is used). Or are you using this within a filter context? That might think there is no score and just return 0.

I think your examples should work. Try something like this:

  "script_score": {
     "script": {
         "inline": "doc['my_field'].contains(1) ? 1000 : 0"

Thanks for the response. Yes, I only used a string there, not a json object. Since the kibana console cut and paste doesn't work for me under firefox, I had to type it out and for some reason just added a json object there.

I've since found another way to do what I was going for. I gave up on using painless for the time being - it's just too painful without a REPL or interactive object inspector. :slight_smile: I'll give it another go once it's out of experimental and the docs are a bit more accessable.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.