# Scripted Field matching two values with AND statement

**URL:** <https://discuss.elastic.co/t/scripted-field-matching-two-values-with-and-statement/148215>\
**Category:** Kibana\
**Created:** [September 11, 2018, 8:22pm UTC](https://discuss.elastic.co/t/scripted-field-matching-two-values-with-and-statement/148215 "2018-09-11T20:22:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkelley](https://avatars.discourse-cdn.com/v4/letter/b/e9a140/32.png) [@bkelley](https://discuss.elastic.co/u/bkelley)\
**Post date:** [September 11, 2018, 8:22pm UTC](https://discuss.elastic.co/t/scripted-field-matching-two-values-with-and-statement/148215/1 "2018-09-11T20:22:06Z")

</div>

Hi -

I'm trying to create a **scripted field** in Kibana that checks the value of two fields then returns 0 or 1 depending on whether they match the values shown below.

My script is:

```auto
if (doc['counter.name'].value == '% items OK') && (doc['counter.value'].value == '100') {
   return 0
} else {
   return 1
}

```

For some reason this is failing with a "Courier Fetch: 2 of 131 shards failed". Does anyone know where my problem is?

Thanks,  
Butch

---

<div class="post-metadata">

**Author:** ![bhavyarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bhavyarm/32/22392_2.png) [@bhavyarm](https://discuss.elastic.co/u/bhavyarm)\
**Post date:** [September 11, 2018, 8:40pm UTC](https://discuss.elastic.co/t/scripted-field-matching-two-values-with-and-statement/148215/2 "2018-09-11T20:40:07Z")

</div>

Hi,

Which version of the stack you are on? We have a preview and validate your scripted fields in 6.4.0.

I suspect this might be the problem? **'% items OK'** What's the % in here?

Thanks  
Bhavya

---

<div class="post-metadata">

**Author:** ![bkelley](https://avatars.discourse-cdn.com/v4/letter/b/e9a140/32.png) [@bkelley](https://discuss.elastic.co/u/bkelley)\
**Post date:** [September 11, 2018, 9:59pm UTC](https://discuss.elastic.co/t/scripted-field-matching-two-values-with-and-statement/148215/3 "2018-09-11T21:59:18Z")

</div>

Hi Bhavya -

I'm running 6.2. The '% items OK' is the value of 'counter.name' that I'm trying to match. Do I need to backslash the %?

Thanks for your response,  
Butch

---

<div class="post-metadata">

**Author:** ![bkelley](https://avatars.discourse-cdn.com/v4/letter/b/e9a140/32.png) [@bkelley](https://discuss.elastic.co/u/bkelley)\
**Post date:** [September 13, 2018, 1:24pm UTC](https://discuss.elastic.co/t/scripted-field-matching-two-values-with-and-statement/148215/4 "2018-09-13T13:24:20Z")

</div>

To follow up - this code is now working. I found that this will only work on fields that are listed as "aggregatable".

```auto
if ((doc['counter.name.keyword'].value == '% items OK') && (doc['counter.value.keyword'].value == '100')) { return 100; } else { return 1; }

```

---

<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:** [October 11, 2018, 1:24pm UTC](https://discuss.elastic.co/t/scripted-field-matching-two-values-with-and-statement/148215/5 "2018-10-11T13:24:23Z")

</div>

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