# Painless If doc\['field'\].value in List

**URL:** <https://discuss.elastic.co/t/painless-if-doc-field-value-in-list/126598>\
**Category:** Elasticsearch\
**Created:** [April 3, 2018, 3:30pm UTC](https://discuss.elastic.co/t/painless-if-doc-field-value-in-list/126598 "2018-04-03T15:30:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MPurcell](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@MPurcell](https://discuss.elastic.co/u/MPurcell)\
**Post date:** [April 3, 2018, 3:30pm UTC](https://discuss.elastic.co/t/painless-if-doc-field-value-in-list/126598/1 "2018-04-03T15:30:37Z")

</div>

Hey quick question. I have a script/painless section in a bool-must section of my code. Basically I want to check if the classification number, which could be = 17, 18, 19, 20, or 21 is either 18, 19, or 20. For some reason, I can't get the or statement in my code to work properly. It only matches, at the most, against the right most term. Here is the part of my query in question.

![image](https://us1.discourse-cdn.com/elastic/original/3X/a/8/a8fe518290a7f9fe7bc2763394b8d201d8611419.png)

The second bool statement works as expected, the first does not. Thanks for any/all help.

---

<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:** [April 3, 2018, 4:08pm UTC](https://discuss.elastic.co/t/painless-if-doc-field-value-in-list/126598/2 "2018-04-03T16:08:59Z")

</div>

Please do not put code in images. Use `</>` in the message editor toolbar.

Doing a bitwise `OR` of the values will not work, as the bits are not disjoint. You should do a linear scan through your values (since there is a very small number of them). For this, List has the `contains` method. Try something like this:

```auto
[18L, 19L, 20L].contains(doc['Columns.Lines.CharacterClassification'].value)

```

Note that the `L` suffix is necessary because numeric fields (non floating point) are exposed as `long` values.

---

<div class="post-metadata">

**Author:** ![MPurcell](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@MPurcell](https://discuss.elastic.co/u/MPurcell)\
**Post date:** [April 3, 2018, 5:13pm UTC](https://discuss.elastic.co/t/painless-if-doc-field-value-in-list/126598/3 "2018-04-03T17:13:17Z")

</div>

@rjernst Will do. And that worked perfectly, with no negative impact on the query speed. Thanks for your help!

---

<div class="post-metadata">

**Author:** ![MPurcell](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@MPurcell](https://discuss.elastic.co/u/MPurcell)\
**Post date:** [April 3, 2018, 7:45pm UTC](https://discuss.elastic.co/t/painless-if-doc-field-value-in-list/126598/4 "2018-04-03T19:45:43Z")

</div>

@rjernst What would be another way to do this - that's as fast or faster - if there are more values to search for and more possible values the field could be? For example searching for all the numbers listed above, [17, 18, 19, 20, 21, 30] in a field that has values 1-100?

---

<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:** [April 4, 2018, 12:14am UTC](https://discuss.elastic.co/t/painless-if-doc-field-value-in-list/126598/5 "2018-04-04T00:14:09Z")

</div>

The faster way would be to index the values, and use a conjunction. I assume the values are already indexed (that would be the default for a numeric field). So instead of doing a script query, add a `filter` clause to your bool query. Inside there, add a `bool` query with a `should` clause containing `match` queries for each of your values.

---

<div class="post-metadata">

**Author:** ![MPurcell](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@MPurcell](https://discuss.elastic.co/u/MPurcell)\
**Post date:** [April 4, 2018, 2:41pm UTC](https://discuss.elastic.co/t/painless-if-doc-field-value-in-list/126598/6 "2018-04-04T14:41:51Z")

</div>

@rjernst I believe this is what you meant by the bool should statement. Is there any particular reason you specified I should nest this 'bool-should' statement in a bool-filter rather then a bool-must? Is the difference purely semantics or is there a speed difference in these two sections?

```
{
            "bool": {
              "should": [
                {"match": {
                  "Columns.Lines.CharacterClassification": {
                    "query": 18
                  }
                }},
                {"match": {
                  "Columns.Lines.CharacterClassification": {
                    "query": 19
                  }
                }},
                {"match": {
                  "Columns.Lines.CharacterClassification": {
                    "query": 23
                  }
                }},
                {"match": {
                  "Columns.Lines.CharacterClassification": {
                    "query": 27
                  }
                }},
                {"match": {
                  "Columns.Lines.CharacterClassification": {
                    "query": 31
                  }
                }}
                ],
                "minimum_should_match": 1
                }}
```

---

<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:** [April 4, 2018, 3:25pm UTC](https://discuss.elastic.co/t/painless-if-doc-field-value-in-list/126598/7 "2018-04-04T15:25:39Z")

</div>

A `filter` will act like a `must`, but without scoring. At least currently, Lucene/Elasticsearch isn't smart enough to automatically move this clause to a filter. Instead, it would return a score of 1.

Also, you can more succinctly specify your match queries, no need for the object based representation (since you do not use additional options):

```auto
"match": {
  "Columns.Lines.CharacterClassification": 27
}

```

---

<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:** [May 2, 2018, 3:25pm UTC](https://discuss.elastic.co/t/painless-if-doc-field-value-in-list/126598/8 "2018-05-02T15:25:44Z")

</div>

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