# Testing multivalue field value existence in painless

**URL:** <https://discuss.elastic.co/t/testing-multivalue-field-value-existence-in-painless/90060>\
**Category:** Elasticsearch\
**Created:** [June 20, 2017, 8:35am UTC](https://discuss.elastic.co/t/testing-multivalue-field-value-existence-in-painless/90060 "2017-06-20T08:35:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![srd](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@srd](https://discuss.elastic.co/u/srd)\
**Post date:** [June 20, 2017, 8:35am UTC](https://discuss.elastic.co/t/testing-multivalue-field-value-existence-in-painless/90060/1 "2017-06-20T08:35:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [June 23, 2017, 8:16pm UTC](https://discuss.elastic.co/t/testing-multivalue-field-value-existence-in-painless/90060/2 "2017-06-23T20:16:15Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![srd](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@srd](https://discuss.elastic.co/u/srd)\
**Post date:** [June 27, 2017, 7:39am UTC](https://discuss.elastic.co/t/testing-multivalue-field-value-existence-in-painless/90060/3 "2017-06-27T07:39:08Z")

</div>

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. 🙂 I'll give it another go once it's out of experimental and the docs are a bit more accessable.

---

<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 25, 2017, 7:39am UTC](https://discuss.elastic.co/t/testing-multivalue-field-value-existence-in-painless/90060/4 "2017-07-25T07:39:12Z")

</div>

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